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

Accessing WindowFlags through postEvent causes Application to not exit

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 6.3.0, 6.3.1
    • Active Qt
    • None
    • All

    Description

      When using the "Thread-Safe Event" paradigm:

       

      class QEventExecutor : public QEvent
      {    
      using callback_t = std::function<void()>;
        private:    
      callback_t m_func;
        public:
          QEventExecutor(callback_t &&func) : QEvent(QEvent::None), m_func(std::move(func)) {}
          ~QEventExecutor() override
          {
              m_func();
          }
      };
      
      // Somewhere else in a different thread
      QApplication::postEvent(/*receiver*/, new QEventExecutor([] {/*...*/}));
      

       
      And accessing the WindowFlags, the application will not exit correctly when the last Window is closed.
      This behavior does not occur in Qt 6-2.6.4 downwards, Qt5 is also not affected.

      Here's a full reproduction example (can also be found as an attachment):

      #include <QApplication>
      #include <QMainWindow>
      #include <functional>
      #include <future>
      #include <thread>
      
      class QEventExecutor : public QEvent
      {
          using callback_t = std::function<void()>;
      
        private:
          callback_t m_func;
      
        public:
          QEventExecutor(callback_t &&func) : QEvent(QEvent::None), m_func(std::move(func)) {}
          ~QEventExecutor() override
          {
              m_func();
          }
      };
      
      int main(int argc, char **argv)
      {
          auto QApp = QApplication(argc, argv);
          auto *main = new QMainWindow();
      
          main->setWindowTitle("Bug Report");
          main->setFixedSize(500, 600);
      
          // auto fut_ptr = std::make_shared<std::future<void>>();
          // *fut_ptr = std::async(std::launch::async, [fut_ptr, &main] {
          //     std::this_thread::sleep_for(std::chrono::seconds(2));
          //     QApplication::postEvent(main, new event_callback([&main] {
          //                                 main->setWindowTitle("Works, application exits!");
          //                                 main->close();
          //                             }));
          // });
      
          auto fut_ptr = std::make_shared<std::future<void>>();
          *fut_ptr = std::async(std::launch::async, [fut_ptr, &main] {
              std::this_thread::sleep_for(std::chrono::seconds(2));
              QApplication::postEvent(main, new QEventExecutor([&main] {
                                          //? Doesn't work, window closes, application doesn't exit
                                          main->setWindowFlag(Qt::FramelessWindowHint, true);
                                          main->close();
                                      }));
          });
      
          main->show();
          QApplication::exec();
      }
      

      Attachments

        Activity

          People

            laknoll Lars Knoll
            curve Noah Fynn
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: