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