QCursor::pos() only works correctly if I change the Qt Creator theme.
-
I have created a mouseMoveEvent that should return the global coordinates of the cursor
void MainWindow::mouseMoveEvent(QMouseEvent *event) { QPoint globalPos = event->pos(); globalPos = QWidget::mapToGlobal(globalPos); ui->labelGlobal->setText(QString("%1 : %2").arg(globalPos.x()).arg(globalPos.y())); }
, but if I move the cursor to the left-top corner of the screen, I get negative coordinates, even though the coordinates should be 0:0.
If I change the Qt Creator theme, everything works correctly, until I restart Qt Creator for the first time. -
@Christian-Ehrlicher I disable Wayland in /etc/gdm3/custom.conf and reboot Ubuntu, everything worked correctly.
Thanks for the help!
-
Hi and welcome to devnet,
What do you mean by "Qt Creator theme" ?
Which version of Qt are you using ?
On which platform ? -
@Punkcake said in QCursor::pos() only works correctly if I change the Qt Creator theme.:
Qt Creator 11.0.3 Based on Qt 6.4.3
That's not your Qt version you are using to create your app, that's what was used to build QtCreator itself.
QWidget::mapToGlobal(globalPos);
Does changing it to
this->mapToGlobal(globalPos);
make any difference?
-
class MyMainWindow : public QMainWindow { public: void mouseMoveEvent(QMouseEvent *ev) override { QMainWindow::mouseMoveEvent(ev); const auto globalPos = mapToGlobal(ev->pos()); qDebug() << globalPos << ev->globalPosition() << ev->pos(); } };
Works fine for me, even though the mapped and the global pos from the event differ sometimes (maybe due to rounding errors).
-
I think the problem is not in the code
-
@Punkcake But you should still output QMouseEvent::globalPosition() and check it with the calculated one. I doubt it's a Qt problem but maybe within your window manager - what window manager do you use?
-
@Christian-Ehrlicher I output QMouseEvent::globalPosition() and got the following values:
QPoint(-111,-76) QPointF(-111,-76) QPoint(-111,-76)
QPoint(-112,-76) QPointF(-112,-76) QPoint(-112,-76)
QPoint(-113,-76) QPointF(-113,-76) QPoint(-113,-76)
QPoint(-114,-76) QPointF(-114,-76) QPoint(-114,-76)
QPoint(-115,-76) QPointF(-115,-76) QPoint(-115,-76)
QPoint(-116,-76) QPointF(-116,-76) QPoint(-116,-76)How can I find out which window manager I'm using?
-
@Punkcake said in QCursor::pos() only works correctly if I change the Qt Creator theme.:
Maybe the problem is related to virtualization.
Don't know - works fine with VirtualBox here. I think it's the window manager
How can I find out which window manager I'm using?
You should know if you run Gnome or KDE or whatever.
-
@Christian-Ehrlicher I disable Wayland in /etc/gdm3/custom.conf and reboot Ubuntu, everything worked correctly.
Thanks for the help!
-