Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QWidget is transparent even if I set the background color

QWidget is transparent even if I set the background color

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 724 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aaron Kim
    wrote on last edited by
    #1

    0_1533477708024_capture.png

    I want to make a menu (yellow square) which is shown up if I press the button. Because there is a layout, I have to set its parent to MainWindow and set its coordinate.

    I made a snippet to test it.

    class ConstMenu : public QWidget{
        Q_OBJECT
    
    private:
        MainWindow* mainWindow;
        Template::Calculator* calculator;
        void mousePressEvent(QMouseEvent*);
    
    public:
        ConstMenu(MainWindow*, Template::Calculator*);
        virtual ~ConstMenu() = default;
        void showMenu();
    };
    ConstMenu::ConstMenu(MainWindow* window, Template::Calculator* cal) : QWidget(window),
        mainWindow(window), calculator(cal) {
        this->showMenu();
    }
    
    
    void ConstMenu::showMenu(){
        this->setStyleSheet("background-color: red;");
        this->move(0, 0);
        this->resize(300, 300);
        this->show();
    }
    
    void ConstMenu::mousePressEvent(QMouseEvent*){
        std::cout << "Test\n";
    }
    

    However, nothing is shown.
    0_1533478037919_capture2.PNG
    The strange thing is, there is definitely the ConstMenu widget because I checked it with mousePressEvent.
    It is there, but transparent.

    0_1533478145801_capture3.gif
    (You can see buttons at the bottom are highlighted when mouse hovers them, unlike other buttons at the top. This is because the ConstMenu widget covers them)

    I have never experienced such odd behavior. Any guess?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      That is strange indeed. Just one question though, why not use QToolButton which already provides handling for an associated menu ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • A Offline
        A Offline
        Aaron Kim
        wrote on last edited by Aaron Kim
        #3

        @SGaist ConstMenu class is not a button, but a panel on which buttons are
        There will be buttons on the yellow square

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Try setting the autoFillBackground property to true.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved