QWidget is transparent even if I set the background color



  • 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?


  • Lifetime Qt Champion

    Hi,

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



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


  • Lifetime Qt Champion

    Try setting the autoFillBackground property to true.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.