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. The mystery of MouseMoveEvent of frameless QMainWindow
QtWS25 Last Chance

The mystery of MouseMoveEvent of frameless QMainWindow

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 6 Posters 2.7k 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.
  • F Offline
    F Offline
    Faruq
    wrote on last edited by
    #1

    Hi everyone once again, for today is the mystery of the left and right click button of your mouse (Mouse move event). I would like to utilise the left button to move my frameless widget around and the right button to resize it .

    The problem is that at the mainwindow.cpp at the "void MainWindow::mouseMoveEvent(QMouseEvent *event)", I am unable to execute the right click. It will be shown as if I clicked the left button when I am very positive I click the right.

    But if I separate them, they can work at their individual space. Why does this happen and what is the remedy?

    Thank you!

    //mainwindow.cpp
    
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QPalette>
    #include <QDesktopWidget>
    #include <QMenuBar>
    #include <QMouseEvent>
    #include <QDebug>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent,Qt::FramelessWindowHint),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        MainWindow::setAttribute(Qt::WA_TranslucentBackground);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    //THE FOCUS OF THIS POST TOPIC
    
    void MainWindow::mousePressEvent(QMouseEvent *event){
    
        if(event->buttons()== Qt::LeftButton){
        isMouseDown = true;
        isLeftDown = true;
        offset_X_Coordinate = event->x();
        offset_Y_Coordinate = event->y();
        qDebug() << "1st Left mouse clicked";
        }
        else if (event->buttons()== Qt::RightButton){
            isMouseDown = true;
            isRightDown = true;
            offset_X_Coordinate = event->x();
            offset_Y_Coordinate = event->y();
            qDebug() << "1st Right mouse clicked";
            }
    }
    
    //PROBLEM HERE: After click, while during the drag, it recorded as left click only despite clicking the right button of the mouse.
    
    void MainWindow::mouseMoveEvent(QMouseEvent *event){
        if(isMouseDown=true){
        mousePointx = event->globalX();
        mousePointy = event->globalY();
            if (isLeftDown = true){
            move((mousePointx-offset_X_Coordinate),(mousePointy-offset_Y_Coordinate));
            qDebug() << "2nd Left mouse clicked";
            }
            else if (isRightDown = true){
            MainWindow::resize((mousePointx),(mousePointy));
            qDebug() << "2nd Right mouse clicked";
            }
        }
    }
    
    void MainWindow::mouseReleaseEvent(QMouseEvent *event){
        isMouseDown = false;
        isLeftDown = false;
        isRightDown = false;
    }
    
    //mainwindow.h 
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MainWindow;
    class QWidget;
    }
    
    class MainWindow :  public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    
    protected:
        void mouseMoveEvent(QMouseEvent *event);        //For moving
        void mousePressEvent(QMouseEvent *event);       //For moving
        void mouseReleaseEvent(QMouseEvent *event);     //For moving
    
        int mousePointx;
        int mousePointy;
    
    
    private:
        Ui::MainWindow *ui;
    
        bool isMouseDown = false;
        bool isLeftDown = false;
        bool isRightDown = false;
        int offset_X_Coordinate;
        int offset_Y_Coordinate;
    
    };
    
    #endif // MAINWINDOW_H
    
    
    JonBJ 1 Reply Last reply
    0
    • F Faruq

      Hi everyone once again, for today is the mystery of the left and right click button of your mouse (Mouse move event). I would like to utilise the left button to move my frameless widget around and the right button to resize it .

      The problem is that at the mainwindow.cpp at the "void MainWindow::mouseMoveEvent(QMouseEvent *event)", I am unable to execute the right click. It will be shown as if I clicked the left button when I am very positive I click the right.

      But if I separate them, they can work at their individual space. Why does this happen and what is the remedy?

      Thank you!

      //mainwindow.cpp
      
      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QPalette>
      #include <QDesktopWidget>
      #include <QMenuBar>
      #include <QMouseEvent>
      #include <QDebug>
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent,Qt::FramelessWindowHint),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          MainWindow::setAttribute(Qt::WA_TranslucentBackground);
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      //THE FOCUS OF THIS POST TOPIC
      
      void MainWindow::mousePressEvent(QMouseEvent *event){
      
          if(event->buttons()== Qt::LeftButton){
          isMouseDown = true;
          isLeftDown = true;
          offset_X_Coordinate = event->x();
          offset_Y_Coordinate = event->y();
          qDebug() << "1st Left mouse clicked";
          }
          else if (event->buttons()== Qt::RightButton){
              isMouseDown = true;
              isRightDown = true;
              offset_X_Coordinate = event->x();
              offset_Y_Coordinate = event->y();
              qDebug() << "1st Right mouse clicked";
              }
      }
      
      //PROBLEM HERE: After click, while during the drag, it recorded as left click only despite clicking the right button of the mouse.
      
      void MainWindow::mouseMoveEvent(QMouseEvent *event){
          if(isMouseDown=true){
          mousePointx = event->globalX();
          mousePointy = event->globalY();
              if (isLeftDown = true){
              move((mousePointx-offset_X_Coordinate),(mousePointy-offset_Y_Coordinate));
              qDebug() << "2nd Left mouse clicked";
              }
              else if (isRightDown = true){
              MainWindow::resize((mousePointx),(mousePointy));
              qDebug() << "2nd Right mouse clicked";
              }
          }
      }
      
      void MainWindow::mouseReleaseEvent(QMouseEvent *event){
          isMouseDown = false;
          isLeftDown = false;
          isRightDown = false;
      }
      
      //mainwindow.h 
      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      
      namespace Ui {
      class MainWindow;
      class QWidget;
      }
      
      class MainWindow :  public QMainWindow
      {
          Q_OBJECT
      
      public:
          explicit MainWindow(QWidget *parent = 0);
          ~MainWindow();
      
      
      protected:
          void mouseMoveEvent(QMouseEvent *event);        //For moving
          void mousePressEvent(QMouseEvent *event);       //For moving
          void mouseReleaseEvent(QMouseEvent *event);     //For moving
      
          int mousePointx;
          int mousePointy;
      
      
      private:
          Ui::MainWindow *ui;
      
          bool isMouseDown = false;
          bool isLeftDown = false;
          bool isRightDown = false;
          int offset_X_Coordinate;
          int offset_Y_Coordinate;
      
      };
      
      #endif // MAINWINDOW_H
      
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Faruq

          if(isMouseDown=true){
      ...
              if (isLeftDown = true){
      ...
              else if (isRightDown = true){
      ...
          }
      

      Seriously?

      F 1 Reply Last reply
      3
      • JonBJ JonB

        @Faruq

            if(isMouseDown=true){
        ...
                if (isLeftDown = true){
        ...
                else if (isRightDown = true){
        ...
            }
        

        Seriously?

        F Offline
        F Offline
        Faruq
        wrote on last edited by Faruq
        #3

        @JonB Hmmm, what is wrong with that? Is the logic behind it flawed? Should I just delete off isMouseDown ?

        G 1 Reply Last reply
        0
        • Fuel 0F Offline
          Fuel 0F Offline
          Fuel 0
          wrote on last edited by
          #4

          Take a look at it again. Think about Operators.

          And something else. Why you cant check if is Mouse pressed again? Just do it in mouseMoveEvent again. Like

          if (event->buttons == Qt::LeftButton)
          
          else if (event->buttons == Qt::RightButton)
          

          Just dont use additional boolean Variables, because you dont need them there. And check again your Operators.

          1 Reply Last reply
          1
          • F Faruq

            @JonB Hmmm, what is wrong with that? Is the logic behind it flawed? Should I just delete off isMouseDown ?

            G Offline
            G Offline
            Garrett
            wrote on last edited by
            #5

            @Faruq I think JonB is saying to look at your if statements again. You are assigning isMouseDown to be true, not checking if it is true. If you are checking if something is equal to some other value, you need to use the double equal sign "==" not the single equal sign "=".

            F 1 Reply Last reply
            2
            • G Garrett

              @Faruq I think JonB is saying to look at your if statements again. You are assigning isMouseDown to be true, not checking if it is true. If you are checking if something is equal to some other value, you need to use the double equal sign "==" not the single equal sign "=".

              F Offline
              F Offline
              Faruq
              wrote on last edited by
              #6

              @Garrett Hi, thank you all. It only took me a few second to instantly know what goes wrong when I read Garrett post. Really appreciate it, Garrett :)

              JonBJ 1 Reply Last reply
              0
              • F Faruq

                @Garrett Hi, thank you all. It only took me a few second to instantly know what goes wrong when I read Garrett post. Really appreciate it, Garrett :)

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

                @Faruq
                To help you: nearly all compilers I know would warn you on a line like:

                if(isMouseDown=true){
                

                about what you are likely to be doing wrong here.

                If your compiler did warn you, don't ignore warnings! If it did not, you should now ensure your IDE (QT Creator?) has all/most warnings switched on, e.g. if you're using gcc it's the -Wall command-line flag, there will be equivalent for others. I strongly recommend you sort this out going forward...

                J.HilkJ 1 Reply Last reply
                0
                • JonBJ JonB

                  @Faruq
                  To help you: nearly all compilers I know would warn you on a line like:

                  if(isMouseDown=true){
                  

                  about what you are likely to be doing wrong here.

                  If your compiler did warn you, don't ignore warnings! If it did not, you should now ensure your IDE (QT Creator?) has all/most warnings switched on, e.g. if you're using gcc it's the -Wall command-line flag, there will be equivalent for others. I strongly recommend you sort this out going forward...

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  @JonB said in The mystery of MouseMoveEvent of frameless QMainWindow:

                  @Faruq
                  To help you: nearly all compilers I know would warn you on a line like:

                  if(isMouseDown=true){
                  

                  about what you are likely to be doing wrong here.

                  If your compiler did warn you, don't ignore warnings! If it did not, you should now ensure your IDE (QT Creator?) has all/most warnings switched on, e.g. if you're using gcc it's the -Wall command-line flag, there will be equivalent for others. I strongly recommend you sort this out going forward...

                  Well if the op uses MSVC compiler than he will have no warning x)


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  JonBJ 1 Reply Last reply
                  0
                  • J.HilkJ J.Hilk

                    @JonB said in The mystery of MouseMoveEvent of frameless QMainWindow:

                    @Faruq
                    To help you: nearly all compilers I know would warn you on a line like:

                    if(isMouseDown=true){
                    

                    about what you are likely to be doing wrong here.

                    If your compiler did warn you, don't ignore warnings! If it did not, you should now ensure your IDE (QT Creator?) has all/most warnings switched on, e.g. if you're using gcc it's the -Wall command-line flag, there will be equivalent for others. I strongly recommend you sort this out going forward...

                    Well if the op uses MSVC compiler than he will have no warning x)

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

                    @J.Hilk
                    I have been using Visual Studio (outside of Qt) for, umm, 20 years, and I'm sure there must be a similar warning for "possibly unintended assignment inside condition", isn't there...??

                    I Googled across, say, http://www.learncpp.com/cpp-programming/eight-c-programming-mistakes-the-compiler-wont-catch/

                    Running a modern compiler at the highest warning level will cause it to issue a warning when an assignment is used in a conditional statement, or a note that the statement does nothing when an equality test is used instead of an assignment outside of a conditional. This is one issue that is essentially fixable -- if you use the higher warning levels.

                    Or: https://softwareengineering.stackexchange.com/questions/162256/in-c-and-c-what-methods-can-prevent-accidental-use-of-the-assignment-where

                    ?

                    EDIT

                    OK, what about https://msdn.microsoft.com/en-us/library/7hw7c1he.aspx

                    Compiler Warning (level 4) C4706

                    It's in /W4, which I always use, and is equivalent to gcc's -Wall.

                    And BTW MSVC does not accept double-parentheses to suppress this, you have to write:

                    if ((isMouseDown = true) == true)
                    
                    J.HilkJ 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @J.Hilk
                      I have been using Visual Studio (outside of Qt) for, umm, 20 years, and I'm sure there must be a similar warning for "possibly unintended assignment inside condition", isn't there...??

                      I Googled across, say, http://www.learncpp.com/cpp-programming/eight-c-programming-mistakes-the-compiler-wont-catch/

                      Running a modern compiler at the highest warning level will cause it to issue a warning when an assignment is used in a conditional statement, or a note that the statement does nothing when an equality test is used instead of an assignment outside of a conditional. This is one issue that is essentially fixable -- if you use the higher warning levels.

                      Or: https://softwareengineering.stackexchange.com/questions/162256/in-c-and-c-what-methods-can-prevent-accidental-use-of-the-assignment-where

                      ?

                      EDIT

                      OK, what about https://msdn.microsoft.com/en-us/library/7hw7c1he.aspx

                      Compiler Warning (level 4) C4706

                      It's in /W4, which I always use, and is equivalent to gcc's -Wall.

                      And BTW MSVC does not accept double-parentheses to suppress this, you have to write:

                      if ((isMouseDown = true) == true)
                      
                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #10

                      @JonB
                      on higher warning levels this might indeed be true, I did just a quick test with the default settings.

                      int main(int argc, char *argv[])
                      {
                          QApplication a(argc, argv);
                      
                          int ab;
                          if(ab = 2)
                              qDebug() << "True" << ab;
                          else
                              qDebug() << "False" << ab;
                      
                         return a.exec();
                      }
                      

                      and got no warnings with MSVC and a warning with mingw
                      0_1526555924345_d7fe3fda-7318-40f1-8e5e-bf4723ba6140-image.png


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      JonBJ 1 Reply Last reply
                      0
                      • J.HilkJ J.Hilk

                        @JonB
                        on higher warning levels this might indeed be true, I did just a quick test with the default settings.

                        int main(int argc, char *argv[])
                        {
                            QApplication a(argc, argv);
                        
                            int ab;
                            if(ab = 2)
                                qDebug() << "True" << ab;
                            else
                                qDebug() << "False" << ab;
                        
                           return a.exec();
                        }
                        

                        and got no warnings with MSVC and a warning with mingw
                        0_1526555924345_d7fe3fda-7318-40f1-8e5e-bf4723ba6140-image.png

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

                        @J.Hilk
                        See my edits above. For MSVC use /W4 just as you would use gcc -Wall. I always expect to do that, and would advise this OP to do so too.

                        J.HilkJ 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @J.Hilk
                          See my edits above. For MSVC use /W4 just as you would use gcc -Wall. I always expect to do that, and would advise this OP to do so too.

                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #12

                          @JonB
                          mmh, adding

                          QMAKE_CXXFLAGS_WARN_ON -= -W3
                          QMAKE_CXXFLAGS_WARN_ON += -W4
                          

                          or

                          QMAKE_CFLAGS_WARN_ON -= -W3
                          QMAKE_CFLAGS_WARN_ON += -W4
                          

                          still produces no warning.
                          Seems like I have to google how to do change the warning lvl in QtCreator x)


                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          JonBJ 1 Reply Last reply
                          0
                          • J.HilkJ J.Hilk

                            @JonB
                            mmh, adding

                            QMAKE_CXXFLAGS_WARN_ON -= -W3
                            QMAKE_CXXFLAGS_WARN_ON += -W4
                            

                            or

                            QMAKE_CFLAGS_WARN_ON -= -W3
                            QMAKE_CFLAGS_WARN_ON += -W4
                            

                            still produces no warning.
                            Seems like I have to google how to do change the warning lvl in QtCreator x)

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

                            @J.Hilk
                            First check out that you do indeed get that warning with explicit command-line compile of a test program outside of Qt. I was only quoting from what I randomly Googled for....

                            J.HilkJ 1 Reply Last reply
                            0
                            • JonBJ JonB

                              @J.Hilk
                              First check out that you do indeed get that warning with explicit command-line compile of a test program outside of Qt. I was only quoting from what I randomly Googled for....

                              J.HilkJ Offline
                              J.HilkJ Offline
                              J.Hilk
                              Moderators
                              wrote on last edited by
                              #14

                              @JonB
                              I can confirm, w4 catches it :-)

                              0_1526557925007_90985ca6-93a1-4271-b2e0-43f76104365b-image.png


                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              1 Reply Last reply
                              1
                              • S Offline
                                S Offline
                                saber
                                wrote on last edited by saber
                                #15

                                i have a suggestion .
                                use qpoint instead of "offset_X_Coordinate;"& "offset_Y_Coordinate;"

                                1 Reply Last reply
                                1

                                • Login

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