QWidget is transparent even if I set the background color
-
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.
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?
-
Hi,
That is strange indeed. Just one question though, why not use QToolButton which already provides handling for an associated menu ?
-
Try setting the autoFillBackground property to true.