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. QMouseEvent::pos() returns different values on different OS

QMouseEvent::pos() returns different values on different OS

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtwidgetsqmouseeventqpainteventposition
3 Posts 2 Posters 430 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
    d.sukhomlinov
    wrote on 24 Nov 2023, 07:47 last edited by
    #1

    The general problem is that, as you can read in the topic, QMouseEvent::pos() has different values on different operating systems. I have created the simplest example possible. This example is based on the "Qt Widgets Application", provided in the "Create New" menu of Qt Creator.

    In general, this is the window with red rectangle. This rectangle have 50 px margins from top, bottom, right al left. This value (I mean, 50) is definitely hardcoded and cannot be changed from the application.

    The (0,0) point of the widget is located in the left-top corner. That means, that when I click on the left-top corner of the red rectangle (a little bit pixelhanting, but that's OK), I anticipate to click to the position with coordinates (50,50). But, for the reason I cannot understand, it is true on Windows only. On Linux I have (51, 51) - one pixel shift - and on macOS (52, 52) - two pixels shift. Let's consider the example itself:

    main.cpp

    #include "mainwindow.h"
    
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
        return a.exec();
    }
    

    mainwindow.cpp

    #include "mainwindow.h"
    
    #include <QDebug>
    
    #include <QPainter>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        setFixedSize(800, 600);
    }
    
    void MainWindow::mousePressEvent(QMouseEvent* me) {
        auto pos = me->pos();
        qDebug() << "pos: " << pos.x() << "," << pos.y();
    }
    
    void MainWindow::paintEvent(QPaintEvent *) {
        QPainter p(this);
        p.fillRect(50, 50, width() - 100, height() - 100, Qt::red);
        p.setPen(Qt::black);
    }
    

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QMouseEvent>
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
    
        void mousePressEvent(QMouseEvent* me) override;
        void paintEvent(QPaintEvent *pe) override;
    };
    #endif // MAINWINDOW_H
    

    incorrect_pos_on_different_os.pro

    QT       += core gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    CONFIG += c++17
    
    CONFIG += console
    
    # You can make your code fail to compile if it uses deprecated APIs.
    # In order to do so, uncomment the following line.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    SOURCES += \
        main.cpp \
        mainwindow.cpp
    
    HEADERS += \
        mainwindow.h
    
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    

    This problem is reproducible on Qt 5.15.2 (that's pretty old, I know, but I cannot upgrade it right now, need to use what I have). I use Windows 10, Ubuntu 20.04 and macOS Monterey.

    I would be glad if someone help me to figure this problem out. Thanks!

    S 1 Reply Last reply 24 Nov 2023, 09:22
    0
    • D d.sukhomlinov
      24 Nov 2023, 07:47

      The general problem is that, as you can read in the topic, QMouseEvent::pos() has different values on different operating systems. I have created the simplest example possible. This example is based on the "Qt Widgets Application", provided in the "Create New" menu of Qt Creator.

      In general, this is the window with red rectangle. This rectangle have 50 px margins from top, bottom, right al left. This value (I mean, 50) is definitely hardcoded and cannot be changed from the application.

      The (0,0) point of the widget is located in the left-top corner. That means, that when I click on the left-top corner of the red rectangle (a little bit pixelhanting, but that's OK), I anticipate to click to the position with coordinates (50,50). But, for the reason I cannot understand, it is true on Windows only. On Linux I have (51, 51) - one pixel shift - and on macOS (52, 52) - two pixels shift. Let's consider the example itself:

      main.cpp

      #include "mainwindow.h"
      
      #include <QApplication>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          MainWindow w;
          w.show();
          return a.exec();
      }
      

      mainwindow.cpp

      #include "mainwindow.h"
      
      #include <QDebug>
      
      #include <QPainter>
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
      {
          setFixedSize(800, 600);
      }
      
      void MainWindow::mousePressEvent(QMouseEvent* me) {
          auto pos = me->pos();
          qDebug() << "pos: " << pos.x() << "," << pos.y();
      }
      
      void MainWindow::paintEvent(QPaintEvent *) {
          QPainter p(this);
          p.fillRect(50, 50, width() - 100, height() - 100, Qt::red);
          p.setPen(Qt::black);
      }
      

      mainwindow.h

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      #include <QMouseEvent>
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          MainWindow(QWidget *parent = nullptr);
      
          void mousePressEvent(QMouseEvent* me) override;
          void paintEvent(QPaintEvent *pe) override;
      };
      #endif // MAINWINDOW_H
      

      incorrect_pos_on_different_os.pro

      QT       += core gui
      
      greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
      
      CONFIG += c++17
      
      CONFIG += console
      
      # You can make your code fail to compile if it uses deprecated APIs.
      # In order to do so, uncomment the following line.
      #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
      
      SOURCES += \
          main.cpp \
          mainwindow.cpp
      
      HEADERS += \
          mainwindow.h
      
      # Default rules for deployment.
      qnx: target.path = /tmp/$${TARGET}/bin
      else: unix:!android: target.path = /opt/$${TARGET}/bin
      !isEmpty(target.path): INSTALLS += target
      

      This problem is reproducible on Qt 5.15.2 (that's pretty old, I know, but I cannot upgrade it right now, need to use what I have). I use Windows 10, Ubuntu 20.04 and macOS Monterey.

      I would be glad if someone help me to figure this problem out. Thanks!

      S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 24 Nov 2023, 09:22 last edited by
      #2

      @d-sukhomlinov try running the app with the same style on all platforms, just to see if this issue is related to styles.

      Something like yourAppName -style "Fusion". Then check your pixels on all platforms and see if they are the same.

      (Z(:^

      D 1 Reply Last reply 27 Nov 2023, 07:06
      1
      • S sierdzio
        24 Nov 2023, 09:22

        @d-sukhomlinov try running the app with the same style on all platforms, just to see if this issue is related to styles.

        Something like yourAppName -style "Fusion". Then check your pixels on all platforms and see if they are the same.

        D Offline
        D Offline
        d.sukhomlinov
        wrote on 27 Nov 2023, 07:06 last edited by
        #3

        @sierdzio said in QMouseEvent::pos() returns different values on different OS:

        No, it is not related to the style selected. I checked on Windows and it has no shift on Windows Vista, Windows and Fusion.

        1 Reply Last reply
        0

        1/3

        24 Nov 2023, 07:47

        • Login

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