Important: Please read the Qt Code of Conduct -

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{
        MainWindow* mainWindow;
        Template::Calculator* calculator;
        void mousePressEvent(QMouseEvent*);
        ConstMenu(MainWindow*, Template::Calculator*);
        virtual ~ConstMenu() = default;
        void showMenu();
    ConstMenu::ConstMenu(MainWindow* window, Template::Calculator* cal) : QWidget(window),
        mainWindow(window), calculator(cal) {
    void ConstMenu::showMenu(){
        this->setStyleSheet("background-color: red;");
        this->move(0, 0);
        this->resize(300, 300);
    void ConstMenu::mousePressEvent(QMouseEvent*){
        std::cout << "Test\n";

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

    (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


    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