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. how to ignore the event when I click on the button to maximize the window (windowMaximizeButtonHint())
QtWS25 Last Chance

how to ignore the event when I click on the button to maximize the window (windowMaximizeButtonHint())

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 3.6k Views
  • 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
    Domenico
    wrote on 2 Jul 2019, 15:38 last edited by
    #2

    I tried with:

    MainWindow.h

    virtual void changeEvent(QEvent* event);

    MainWindow.cpp

    void MainWindow::changeEvent(QEvent *event)
    {
    if(event->type() == QEvent::WindowStateChange)
    {
    hide();
    event->ignore();
    }
    return QMainWindow::changeEvent(event);
    }

    I know that the button event is not square Qt::WindowMaximizeButtonHint (). but with codes as indicated above if I click on that button the window disappears or rather the application disappears. why? what's wrong?

    J C 2 Replies Last reply 2 Jul 2019, 18:29
    0
    • D Domenico
      2 Jul 2019, 15:38

      I tried with:

      MainWindow.h

      virtual void changeEvent(QEvent* event);

      MainWindow.cpp

      void MainWindow::changeEvent(QEvent *event)
      {
      if(event->type() == QEvent::WindowStateChange)
      {
      hide();
      event->ignore();
      }
      return QMainWindow::changeEvent(event);
      }

      I know that the button event is not square Qt::WindowMaximizeButtonHint (). but with codes as indicated above if I click on that button the window disappears or rather the application disappears. why? what's wrong?

      J Offline
      J Offline
      JonB
      wrote on 2 Jul 2019, 18:29 last edited by JonB 7 Feb 2019, 18:33
      #3

      @Domenico
      First: I think when you hide() the main window, and it's the only window, that causes application exit. Why did you put that in anway, you just want to try to ignore() the event?

      Second: I happened to be looking at something similar last week. See https://forum.qt.io/topic/104184/is-it-possible-to-intercept-maximizing-the-main-window. In my case I wanted to intercept the maximize window button to not do maximization and instead something of my own. The conclusion I came to was: this event only seems to be signalled after the native windowing system has done what is supposed to be done when the maximize button is clicked. You can react to it yourself in the slot, but you cannot stop the window maximizing. That was my conclusion. I left it open because I still didn't hear some definitive statement as to whether this is correct by one of the experts here.

      1 Reply Last reply
      1
      • D Domenico
        2 Jul 2019, 15:38

        I tried with:

        MainWindow.h

        virtual void changeEvent(QEvent* event);

        MainWindow.cpp

        void MainWindow::changeEvent(QEvent *event)
        {
        if(event->type() == QEvent::WindowStateChange)
        {
        hide();
        event->ignore();
        }
        return QMainWindow::changeEvent(event);
        }

        I know that the button event is not square Qt::WindowMaximizeButtonHint (). but with codes as indicated above if I click on that button the window disappears or rather the application disappears. why? what's wrong?

        C Offline
        C Offline
        Cobra91151
        wrote on 2 Jul 2019, 18:53 last edited by Cobra91151 7 Feb 2019, 18:56
        #4

        @Domenico @JonB

        Hello! First of all, you can set for example: setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);, so your dialog no longer have maximize button. But If you really want to intercept the maximize event here is my code:

        bool Test::event(QEvent *event)
        {
            if (event->type() == QEvent::WindowStateChange) {
                if (this->isMaximized()) {
                    this->showNormal();
                }
            }
        
            return QWidget::event(event);
        }
        

        It's a bit tricky, but it handles such task. Also, there is option using Win API intercepting SW_SIZE, SW_SIZING messages using Qt bool QWidget::winEvent(MSG * message, long * result): https://doc.qt.io/archives/qt-4.8/qwidget.html#winEvent

        Happy coding!

        J 1 Reply Last reply 2 Jul 2019, 20:06
        1
        • C Cobra91151
          2 Jul 2019, 18:53

          @Domenico @JonB

          Hello! First of all, you can set for example: setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);, so your dialog no longer have maximize button. But If you really want to intercept the maximize event here is my code:

          bool Test::event(QEvent *event)
          {
              if (event->type() == QEvent::WindowStateChange) {
                  if (this->isMaximized()) {
                      this->showNormal();
                  }
              }
          
              return QWidget::event(event);
          }
          

          It's a bit tricky, but it handles such task. Also, there is option using Win API intercepting SW_SIZE, SW_SIZING messages using Qt bool QWidget::winEvent(MSG * message, long * result): https://doc.qt.io/archives/qt-4.8/qwidget.html#winEvent

          Happy coding!

          J Offline
          J Offline
          JonB
          wrote on 2 Jul 2019, 20:06 last edited by
          #5

          @Cobra91151
          In my case (as per https://forum.qt.io/topic/104184/is-it-possible-to-intercept-maximizing-the-main-window), as I said there the code you show results in the window first being maximized by the OS window system, which the user sees, followed by it being restored to normal size. That is actually the kind of thing I ended up having to use, but it is not what I would call "intercepting" or "ignoring" the maximize button.

          C 1 Reply Last reply 2 Jul 2019, 20:13
          1
          • J JonB
            2 Jul 2019, 20:06

            @Cobra91151
            In my case (as per https://forum.qt.io/topic/104184/is-it-possible-to-intercept-maximizing-the-main-window), as I said there the code you show results in the window first being maximized by the OS window system, which the user sees, followed by it being restored to normal size. That is actually the kind of thing I ended up having to use, but it is not what I would call "intercepting" or "ignoring" the maximize button.

            C Offline
            C Offline
            Cobra91151
            wrote on 2 Jul 2019, 20:13 last edited by
            #6

            @JonB

            Try to use winEvent it will help to actually intercept any OS message including maximize. Win API docs: https://docs.microsoft.com/en-us/windows/desktop/winmsg/wm-size
            https://docs.microsoft.com/en-us/windows/desktop/winmsg/wm-sizing

            J 1 Reply Last reply 2 Jul 2019, 20:15
            1
            • C Cobra91151
              2 Jul 2019, 20:13

              @JonB

              Try to use winEvent it will help to actually intercept any OS message including maximize. Win API docs: https://docs.microsoft.com/en-us/windows/desktop/winmsg/wm-size
              https://docs.microsoft.com/en-us/windows/desktop/winmsg/wm-sizing

              J Offline
              J Offline
              JonB
              wrote on 2 Jul 2019, 20:15 last edited by
              #7

              @Cobra91151
              How will that help me given that I use Qt for platform independence? I support Windows & Linux, like many people.... The OP has never mentioned Windows-only.

              C 2 Replies Last reply 2 Jul 2019, 20:26
              1
              • J JonB
                2 Jul 2019, 20:15

                @Cobra91151
                How will that help me given that I use Qt for platform independence? I support Windows & Linux, like many people.... The OP has never mentioned Windows-only.

                C Offline
                C Offline
                Cobra91151
                wrote on 2 Jul 2019, 20:26 last edited by Cobra91151 7 Feb 2019, 21:07
                #8

                @JonB

                It is not possible to make it working using Qt for platform independence. Because it doesn't have such feature built in. The only easy way is to use platform specific code and add checks for different OS, such as Windows and Linux. You can also try to write portable lib/code by yourself omitting Qt.

                By the way, Qt framework works the same, it has platform specific code and checks which one to use behind the scene :)

                Also, I would like to mention new Qt method:

                bool QWidget::nativeEvent(const QByteArray &eventType, void *message, long *result)
                

                Docs: https://doc.qt.io/qt-5/qwidget.html#nativeEvent

                1 Reply Last reply
                1
                • J JonB
                  2 Jul 2019, 20:15

                  @Cobra91151
                  How will that help me given that I use Qt for platform independence? I support Windows & Linux, like many people.... The OP has never mentioned Windows-only.

                  C Offline
                  C Offline
                  Cobra91151
                  wrote on 2 Jul 2019, 21:33 last edited by Cobra91151 7 Feb 2019, 21:33
                  #9

                  @JonB @Domenico

                  So here is with nativeEvent and platform checks for Windows and Linux.

                  bool Test::nativeEvent(const QByteArray &eventType, void *message, long *) {
                      qDebug() << eventType;
                      MSG *msg = static_cast<MSG *>(message);
                  
                  #ifdef __linux__
                      //Linux code here
                  #elif _WIN32
                      if (msg->message == WM_SYSCOMMAND) {
                          if (msg->wParam == SC_MAXIMIZE) {
                              QMessageBox::information(this, "Test", "Maximizing...", QMessageBox::Ok);
                              //Add your code here
                              return true;
                          }
                      }
                  #endif
                  
                      return false;
                  }
                  

                  Screenshot:

                  0_1562103188783_max_example.gif

                  1 Reply Last reply
                  4
                  • D Offline
                    D Offline
                    Domenico
                    wrote on 3 Jul 2019, 07:05 last edited by
                    #10

                    @JonB @Cobra91151
                    thanks a lot to everyone!! worked with the method:

                    bool QWidget::nativeEvent(const QByteArray &eventType, void *message, long *result)

                    With this method it does not allow to enlarge the window when I click on maximizes! is the code I meant. also the code:

                    bool Test::event(QEvent *event)
                    {
                    if (event->type() == QEvent::WindowStateChange) {
                    if (this->isMaximized()) {
                    this->showNormal();
                    }
                    }

                    return QWidget::event(event);
                    

                    }

                    They are fine but it is not ignored if you click the button before it enlarges and immediately afterwards it returns to normal, it is not what I meant. Thanks again!

                    P 1 Reply Last reply 3 Jul 2019, 10:23
                    0
                    • D Domenico
                      3 Jul 2019, 07:05

                      @JonB @Cobra91151
                      thanks a lot to everyone!! worked with the method:

                      bool QWidget::nativeEvent(const QByteArray &eventType, void *message, long *result)

                      With this method it does not allow to enlarge the window when I click on maximizes! is the code I meant. also the code:

                      bool Test::event(QEvent *event)
                      {
                      if (event->type() == QEvent::WindowStateChange) {
                      if (this->isMaximized()) {
                      this->showNormal();
                      }
                      }

                      return QWidget::event(event);
                      

                      }

                      They are fine but it is not ignored if you click the button before it enlarges and immediately afterwards it returns to normal, it is not what I meant. Thanks again!

                      P Offline
                      P Offline
                      Pl45m4
                      wrote on 3 Jul 2019, 10:23 last edited by
                      #11

                      @Domenico said in how to ignore the event when I click on the button to maximize the window (windowMaximizeButtonHint()):

                      bool Test::event(QEvent *event)
                      {
                      if (event->type() == QEvent::WindowStateChange) {
                      if (this->isMaximized()) {
                      this->showNormal();
                      }
                      }
                      return QWidget::event(event);

                      }
                      They are fine but it is not ignored if you click the button before it enlarges and immediately afterwards it returns to normal, it is not what I meant.

                      This is the normal behavior for this code. The QEvent can not prevent the OS from maximizing the window. So your window will maximize first, before the event sets it back to normal size.


                      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                      ~E. W. Dijkstra

                      1 Reply Last reply
                      2

                      11/11

                      3 Jul 2019, 10:23

                      • Login

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