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. QCursor::pos() only works correctly if I change the Qt Creator theme.
Forum Updated to NodeBB v4.3 + New Features

QCursor::pos() only works correctly if I change the Qt Creator theme.

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 614 Views 2 Watching
  • 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.
  • P Offline
    P Offline
    Punkcake
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      Punkcake
      wrote on last edited by
      #12

      @Christian-Ehrlicher I disable Wayland in /etc/gdm3/custom.conf and reboot Ubuntu, everything worked correctly.

      Thanks for the help!

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi and welcome to devnet,

        What do you mean by "Qt Creator theme" ?

        Which version of Qt are you using ?
        On which platform ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        P 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi and welcome to devnet,

          What do you mean by "Qt Creator theme" ?

          Which version of Qt are you using ?
          On which platform ?

          P Offline
          P Offline
          Punkcake
          wrote on last edited by
          #3

          @SGaist Hi, I use Qt Creator 11.0.3 Based on Qt 6.4.3 (GCC 10.3.1 20210422 (Red Hat 10.3.1-1), x86_64) on Ubuntu 22.04.
          I mean theme in Preferences->Environment->Interface

          Pl45m4P 1 Reply Last reply
          0
          • P Punkcake

            @SGaist Hi, I use Qt Creator 11.0.3 Based on Qt 6.4.3 (GCC 10.3.1 20210422 (Red Hat 10.3.1-1), x86_64) on Ubuntu 22.04.
            I mean theme in Preferences->Environment->Interface

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by
            #4

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


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            P 1 Reply Last reply
            2
            • Pl45m4P Pl45m4

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

              P Offline
              P Offline
              Punkcake
              wrote on last edited by
              #5

              @Pl45m4 I use Qt 6.6.0. I change code and no any difference.

              Christian EhrlicherC 1 Reply Last reply
              0
              • P Punkcake

                @Pl45m4 I use Qt 6.6.0. I change code and no any difference.

                Christian EhrlicherC Online
                Christian EhrlicherC Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #6
                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).

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                P 1 Reply Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher
                  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).

                  P Offline
                  P Offline
                  Punkcake
                  wrote on last edited by
                  #7

                  I think the problem is not in the code

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • P Punkcake

                    I think the problem is not in the code

                    Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

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

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    P 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

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

                      P Offline
                      P Offline
                      Punkcake
                      wrote on last edited by
                      #9

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

                      P 1 Reply Last reply
                      0
                      • P Punkcake

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

                        P Offline
                        P Offline
                        Punkcake
                        wrote on last edited by
                        #10

                        Maybe the problem is related to virtualization.

                        Christian EhrlicherC 1 Reply Last reply
                        0
                        • P Punkcake

                          Maybe the problem is related to virtualization.

                          Christian EhrlicherC Online
                          Christian EhrlicherC Online
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #11

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

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          1 Reply Last reply
                          1
                          • P Offline
                            P Offline
                            Punkcake
                            wrote on last edited by
                            #12

                            @Christian-Ehrlicher I disable Wayland in /etc/gdm3/custom.conf and reboot Ubuntu, everything worked correctly.

                            Thanks for the help!

                            1 Reply Last reply
                            0
                            • P Punkcake has marked this topic as solved on

                            • Login

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