Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

The mystery of MouseMoveEvent of frameless QMainWindow



  • 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
    
    


  • @Faruq

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

    Seriously?



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



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



  • @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 "=".



  • @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 :)



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


  • Moderators

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



  • @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)
    

  • Moderators

    @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



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


  • Moderators

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



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


  • Moderators

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

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



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


Log in to reply