Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. showEvent Linux vs. Windows11
Forum Updated to NodeBB v4.3 + New Features

showEvent Linux vs. Windows11

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 364 Views 2 Watching
  • 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.
  • D Offline
    D Offline
    Der_andere_Sven
    wrote on last edited by
    #1

    Greetings together,

    I write my coder under Ubuntu and port it to Windows 11 from time to time. Now I recognized, that the showEvent of the main windows is not executed reliably under Windows. Sometimes the code holds at a break point in showEvent, sometimes it doesn't. So as a workaround I called it from the constructor. But this is ugly. Can anyone tell the reason or what I did wrong ? Can't I assume, that the showEvent is called each time a window is shown ? (making showEvent public does not help)
    Thank you very much in advance.

    from my .h file...

    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    private:
        void showEvent(QShowEvent* event);
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    };
    

    from my .cpp file...

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        showEvent(nullptr);//<-- not necessary under Linux
    }
    
    MainWindow::~MainWindow()
    {
    }
    
    void MainWindow::showEvent(QShowEvent *event) {
        TestWidget testWidget=TestWidget();
        testWidget.exec();
    }
    
    JonBJ 1 Reply Last reply
    0
    • D Der_andere_Sven

      Greetings together,

      I write my coder under Ubuntu and port it to Windows 11 from time to time. Now I recognized, that the showEvent of the main windows is not executed reliably under Windows. Sometimes the code holds at a break point in showEvent, sometimes it doesn't. So as a workaround I called it from the constructor. But this is ugly. Can anyone tell the reason or what I did wrong ? Can't I assume, that the showEvent is called each time a window is shown ? (making showEvent public does not help)
      Thank you very much in advance.

      from my .h file...

      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      private:
          void showEvent(QShowEvent* event);
      public:
          MainWindow(QWidget *parent = nullptr);
          ~MainWindow();
      };
      

      from my .cpp file...

      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
      {
          showEvent(nullptr);//<-- not necessary under Linux
      }
      
      MainWindow::~MainWindow()
      {
      }
      
      void MainWindow::showEvent(QShowEvent *event) {
          TestWidget testWidget=TestWidget();
          testWidget.exec();
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Der_andere_Sven said in showEvent Linux vs. Windows11:

      private:
          void showEvent(QShowEvent* event);
      

      What do you think this does? Does your compiler not give you a warning for this line?

      Can't I assume, that the showEvent is called each time a window is shown ?

      Which showEvent()? Yours should never be called by the Qt system.

      UPDATE
      You are intending to override QWidget::showEvent(). But you do not use the override qualifier. I thought that C++ (like, say, C#) would then treat your declaration as a brand new, unrelated method with the same name as the base one. But apparently override is only a "compiler hint", your does still override QWidget::showEvent(). So ignore above, but please mark your override methods with override, so that we are all sure, e.g.:

      >     protected:
      >         virtual void showEvent(QShowEvent* event) override;
      

      Please do that now, so we are certain where we are.

      So far as know, QWidget::showEvent() should be called the same whether Linux or Windows. Just try (e.g. from main()) something like:

      QApplication a(argc, argv);
      MainWindow mw = new MainWindow;
      mw->show();
      return a.exec();
      

      Your void MainWindow::showEvent(QShowEvent *event) override should be called. It should call the base implementation, like:

      void MainWindow::showEvent(QShowEvent *event)
      {
          qDebug() << "MainWindow::showEvent";
          QMainWindow::showEvent(event);
      }
      

      Try that on both platforms.

      Note that you must have the a.exec(), else it won't get shown. showEvent() does not get called on the line where you go mw->show(). That "marks" the widget to be shown. The event loop causes it to actually be shown, and it is at that instant (i.e. when the window is actually visually shown) that showEvent() gets called.

      If, using the code, above you still find showEvent() is not called under Windows, then please report the exact version of Qt you are using.

      1 Reply Last reply
      1
      • D Offline
        D Offline
        Der_andere_Sven
        wrote on last edited by
        #3

        Hallo Jon,

        yeah, adding override seems to work. Thank you very much.

        Another thing: In the past I've already wondered what will prevent me from misspelling methods like "showEvent". But override will do it. The compiler will complain if there is nothing to override.

        best regards
        Sven

        JonBJ 1 Reply Last reply
        0
        • D Der_andere_Sven

          Hallo Jon,

          yeah, adding override seems to work. Thank you very much.

          Another thing: In the past I've already wondered what will prevent me from misspelling methods like "showEvent". But override will do it. The compiler will complain if there is nothing to override.

          best regards
          Sven

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Der_andere_Sven said in showEvent Linux vs. Windows11:

          Another thing: In the past I've already wondered what will prevent me from misspelling methods like "showEvent". But override will do it. The compiler will complain if there is nothing to override.

          That is precisely why you should always use override when you want to override! If you have misspelled the method name that would make it error.

          yeah, adding override seems to work. Thank you very much.

          I am surprised at this. You can see from what I crossed out in my previous post I discovered that in C++ my understanding is that your original

          private:
              void showEvent(QShowEvent* event);
          

          should nonetheless have found & overridden the base method even though you did not specify override. override in C++ turns out to be only a hint, not a code-changing thing. See e.g. https://stackoverflow.com/questions/41505335/how-to-make-showevent-get-called or https://www.reddit.com/r/cpp_questions/comments/kp3v7w/is_override_keyword_required/. So (unless those have been changed in more recent C++) I am not sure how just adding override could have altered any behaviour....

          1 Reply Last reply
          1
          • Kent-DorfmanK Offline
            Kent-DorfmanK Offline
            Kent-Dorfman
            wrote on last edited by
            #5

            On a more philosophical level, don't look for workarounds for things that "should work". Strive to understand what you did wrong in the first place that made you look for a work-around. showEvent should work reliably on all Qt supported platforms. If it doesnt' then I'm fairly certain then you aren't using it correctly...and for what its worth...you don't do events directly. You call slots that indirectly trigger events. You should rarely if ever have to screw with showEvent. The Q7 way is QWidget::setVisible() or QWidget::show().

            I light my way forward with the fires of all the bridges I've burned behind me.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved