Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-78780

Qt::Popup widget isn't draw when opened for the second time

    XMLWordPrintable

Details

    • Bug
    • Resolution: Duplicate
    • P2: Important
    • None
    • 5.12.4
    • None
    • macOS
    • fccb519809d8e64db0e9cc54852c8612fe59b6c7

    Description

      The problem

      When a QWidget with Qt::WindowFlags of Qt::Popup is made visible for the second time, the content painted in the paintEvent of the popup widget isn't visible on screen, although paintEvent is executed.

      Everything works correctly when Qt::Widget is used instead of Qt::Popup.

      Note that it is a regression issue: everything works correctly in Qt 5.9.2.

      Minimal working example

      The example consists of a main window and a popup widget which is toggled by clicking inside the main window. The background of both widgets are filled with a random color every time the widget is redrawn.

      #include "QMainWindow"
      #include "QApplication"
      #include "QPaintEvent"
      #include "QPainter"
      #include "QDebug"
      
      // A widget that fills its background with a random color
      class CMyWidget : public QWidget
      {
      public:
        explicit CMyWidget(QWidget* parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags()) : QWidget(parent, f) {}
      
      protected:
        virtual void paintEvent(QPaintEvent *event) override
        {
          qDebug () << "PAINT" << this << event->rect();
          QPainter painter(this);
          painter.fillRect(event->rect(), QBrush(QColor::fromRgb(rand())));
        }
      };
      
      class CMainWindow : public QMainWindow
      {
      public:
        CMyWidget *m_pPopup;
      
        CMainWindow ()
        {
          CMyWidget *pCentralWidget = new CMyWidget(this);
          pCentralWidget->setObjectName("CENTRAL");
          setCentralWidget(pCentralWidget);
      
          // When you replace Qt::Popup by Qt::Widget, everything is drawn correctly
          m_pPopup = new CMyWidget(pCentralWidget, Qt::Popup);
          m_pPopup->resize(100, 100);
          m_pPopup->move(200, 100);
        }
      
        // A popup message is toggled when clicked inside the main window
        void mousePressEvent(QMouseEvent * /*event*/)
        {
          update();
          if (m_pPopup->isVisible())
            m_pPopup->hide();
          else
          {
            m_pPopup->show();
            m_pPopup->update();
          }
        }
      };
      
      int main(int argc, char *argv[])
      {
        QApplication a(argc, argv);
        CMainWindow w;
        w.resize(600, 400);
      
        w.show();
      
        return a.exec();
      } 

      Steps to reproduce the issue

      1. Run the example above
      2. Click inside the main window: a popup is shown and filled correctly
      3. Click again in the main window: the popup is correctly hidden
      4. Click for the third time inside the main window: the popup is shown, but its background is not filled, although paintEvent is executed.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            sorvig Morten Sørvig
            m3197d - -
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes