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())

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 last edited by
    #1

    Hi everyone I would like to implement codes so that the event is ignored when I click on the button to maximize the window "Qt::WindowMaximizeButtonHint()" without using "~Qt::WindowMaximizeButtonHint()" i need to ignore the event at first, which means the event is ignored if I click the first two windows when the application starts. How can I do? give me advice. thank you so much!

    1 Reply Last reply
    0
    • D Offline
      D Offline
      Domenico
      wrote on 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?

      JonBJ Cobra91151C 2 Replies Last reply
      0
      • D Domenico

        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?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #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

          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?

          Cobra91151C Offline
          Cobra91151C Offline
          Cobra91151
          wrote on last edited by Cobra91151
          #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!

          JonBJ 1 Reply Last reply
          1
          • Cobra91151C Cobra91151

            @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!

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on 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.

            Cobra91151C 1 Reply Last reply
            1
            • JonBJ JonB

              @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.

              Cobra91151C Offline
              Cobra91151C Offline
              Cobra91151
              wrote on 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

              JonBJ 1 Reply Last reply
              1
              • Cobra91151C Cobra91151

                @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

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on 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.

                Cobra91151C 2 Replies Last reply
                1
                • JonBJ JonB

                  @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.

                  Cobra91151C Offline
                  Cobra91151C Offline
                  Cobra91151
                  wrote on last edited by Cobra91151
                  #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
                  • JonBJ JonB

                    @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.

                    Cobra91151C Offline
                    Cobra91151C Offline
                    Cobra91151
                    wrote on last edited by Cobra91151
                    #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 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!

                      Pl45m4P 1 Reply Last reply
                      0
                      • D Domenico

                        @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!

                        Pl45m4P Offline
                        Pl45m4P Offline
                        Pl45m4
                        wrote on 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

                        • Login

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