Qt6.8.2 Android: Mouse coordinates inside a QDialog window are wrong.
-
I wrote an application where I use a QDialog for authentication. The app first draws some objects on the central widget and then opens the dialog. So far so well. But every click or touch inside the dialog has wrong mouse coordinates. The number of pixels it is wrong depends on the resolution of the display.
On the screenshot below you can see the red point which should be x, y: 0, 0. But it is 22, 109 on a Pixel 9 Pro with API 35. On other devices this point is different, depending on the resolution of the display.
I tried to write a workaround working on all devices by catching the mouse press and correcting this values. But I can't find a way to calculate the x, y values difference. For this particular phone I get the following dimensions:- Notch height: 156 real pixel: By factor 3.0 this is 52.
- Real dimensions: 1280 x 2784 -- Height without notch: 2628
- Using factor 3 to calculate virtual dimensions.
- Available size: 427 x 876
- Virtual size: 427 x 952 (inclusive notch)
The dialog box is located as follows:
- Geometry: left: 0 , top: 0 , width: 280 , height: 144
- top left corner relative to parent: left: 73 , top: 366 --> Not sure if this is correct!
- Title height: 18 --> Allthough on Android there is no title it reports me this height for it!
The dialog box was created with QtCreator and is defined in an UI file.
This problem exists only on Android. On other systems it works as expected.
Does anybody know how I can calculate the difference of reported mouse coordinates to the expected ones? -
Just for information: I implemented a workaround by using a QWidget instead of the QDialog. This works mostly as expected. Only drawback is that I can't make it modal. The background objects are reacting while the Dialog is in forground. But in this special case it doesn't matter.
-
I encountered a similar problem in my effort to port a desktop app (developed on Ubuntu 20 / Qt 6.8.0) to Android. The problem seems to have gone away for me when I removed all the vertical spacers from the layouts within my QDialog.
-
@empty_al_67 Very sorry for my likely wrong remark 6 days ago. I think a better reason for the unexpected behavior is the answer from Google AI for my inquiry "qt c++ android qdialog touch coordinates wrong":
When handling touch events within a QDialog on Android in Qt C++, the reported touch coordinates may be incorrect due to the dialog's position relative to the main window. To obtain the correct coordinates, it's necessary to map the touch event's global position to the dialog's local coordinate system.
So, the workaround I implemented was also from Google AI, using the query "qt C++ force qdialog to have same coordinates as main window", which suggests to get the coordinates of the main window and then move the qdialog to the same coordinates before calling "show()":
#include <QApplication> #include <QMainWindow> #include <QDialog> #include <QPushButton> int main(int argc, char *argv[]) { QApplication a(argc, argv); QMainWindow mainWindow; mainWindow.setGeometry(100, 100, 400, 300); mainWindow.show(); QDialog dialog(&mainWindow); dialog.setWindowTitle("Dialog"); QPushButton btn("Show Dialog", &mainWindow); QObject::connect(&btn, &QPushButton::clicked, [&]() { QPoint mainWinPos = mainWindow.pos(); dialog.move(mainWinPos); dialog.show(); }); mainWindow.setCentralWidget(&btn); btn.show(); return a.exec(); }
This solution worked well so far, getting consistent and correct behavior from two Android handsets I have (running Android 10 and Android 13).
-
@empty_al_67
Thanks for your solution but I think this is not the right way. What if I want to show the dialog in a different position on the screen? :)