Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved QWidget is transparent even if I set the background color

    General and Desktop
    2
    4
    499
    Loading More Posts
    • 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
      Aaron Kim last edited by

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 1
        • A
          Aaron Kim last edited by Aaron Kim

          @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 Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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 Reply Quote 1
            • First post
              Last post