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. QPixmap::grabWindow how to grab mouse cursor ?
Qt 6.11 is out! See what's new in the release blog

QPixmap::grabWindow how to grab mouse cursor ?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 3.0k 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.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote on last edited by
    #1

    Sometimes need mouse cursor to point out the position of the screen.
    But the way below can not grab mouse cursor.

    QPixmap pixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
    

    https://github.com/sonichy

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

      Hi,

      That's not the goal of grabWindow, however what you can do is draw the point of interest on the pixmap you get.

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

      sonichyS 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        That's not the goal of grabWindow, however what you can do is draw the point of interest on the pixmap you get.

        sonichyS Offline
        sonichyS Offline
        sonichy
        wrote on last edited by
        #3

        @SGaist How to convert QCursor to QPixmap ?

        QPixmap pixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
        QPainter painter(&pixmap);
        painter.drawPixmap(500,500,QCursor(Qt::ArrowCursor).pixmap());
        

        https://github.com/sonichy

        mrjjM 1 Reply Last reply
        0
        • sonichyS sonichy

          @SGaist How to convert QCursor to QPixmap ?

          QPixmap pixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
          QPainter painter(&pixmap);
          painter.drawPixmap(500,500,QCursor(Qt::ArrowCursor).pixmap());
          
          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @sonichy
          hi
          I dont think you can return a pixmap for the system cursors.
          Its only if you constructed a custom one using
          QCursor::QCursor(const QPixmap &pixmap, int hotX = -1, int hotY = -1)
          As far as I know.

          sonichyS 1 Reply Last reply
          1
          • mrjjM mrjj

            @sonichy
            hi
            I dont think you can return a pixmap for the system cursors.
            Its only if you constructed a custom one using
            QCursor::QCursor(const QPixmap &pixmap, int hotX = -1, int hotY = -1)
            As far as I know.

            sonichyS Offline
            sonichyS Offline
            sonichy
            wrote on last edited by
            #5

            @mrjj Yes, you are right.

            QPixmap pixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
            QPixmap pixmap_mouse(":/mouse.png");
            QPainter painter(&pixmap);
            painter.drawPixmap(300,200,pixmap_mouse);
            

            But still can not get system mouse position.

            https://github.com/sonichy

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

              Do you mean QCursor::pos ?

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

              sonichyS 1 Reply Last reply
              3
              • SGaistS SGaist

                Do you mean QCursor::pos ?

                sonichyS Offline
                sonichyS Offline
                sonichy
                wrote on last edited by sonichy
                #7

                @SGaist Construct a QWidget to get cursor::pos() !

                #include <QApplication>
                #include <QStandardPaths>
                #include <QWidget>
                #include <QScreen>
                #include <QClipboard>
                #include <QDateTime>
                #include <QPainter>
                
                int main(int argc, char *argv[])
                {
                    QApplication a(argc, argv);
                    QWidget *widget = new QWidget;
                    QScreen *screen = QGuiApplication::primaryScreen();
                    QPixmap pixmap = screen->grabWindow(0);
                    QPixmap pixmap_mouse(":/mouse.png");
                    QPainter painter(&pixmap);
                    painter.drawPixmap(widget->cursor().pos(),pixmap_mouse);
                    QApplication::clipboard()->setPixmap(pixmap, QClipboard::Clipboard);
                    QDateTime time = QDateTime::currentDateTime();
                    QString filename = time.toString("yyyyMMddhhmmss") + ".jpg";
                    QString path = QStandardPaths::standardLocations(QStandardPaths::DesktopLocation).first() + "/" + filename;
                    pixmap.save(path,0,100);
                    return 0;
                }
                
                

                https://github.com/sonichy

                mrjjM 1 Reply Last reply
                0
                • sonichyS sonichy

                  @SGaist Construct a QWidget to get cursor::pos() !

                  #include <QApplication>
                  #include <QStandardPaths>
                  #include <QWidget>
                  #include <QScreen>
                  #include <QClipboard>
                  #include <QDateTime>
                  #include <QPainter>
                  
                  int main(int argc, char *argv[])
                  {
                      QApplication a(argc, argv);
                      QWidget *widget = new QWidget;
                      QScreen *screen = QGuiApplication::primaryScreen();
                      QPixmap pixmap = screen->grabWindow(0);
                      QPixmap pixmap_mouse(":/mouse.png");
                      QPainter painter(&pixmap);
                      painter.drawPixmap(widget->cursor().pos(),pixmap_mouse);
                      QApplication::clipboard()->setPixmap(pixmap, QClipboard::Clipboard);
                      QDateTime time = QDateTime::currentDateTime();
                      QString filename = time.toString("yyyyMMddhhmmss") + ".jpg";
                      QString path = QStandardPaths::standardLocations(QStandardPaths::DesktopLocation).first() + "/" + filename;
                      pixmap.save(path,0,100);
                      return 0;
                  }
                  
                  
                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @sonichy
                  hi
                  Can you try
                  QPoint p = QCursor::pos();
                  that is global mouse pos. not mapped to local coors for a widget.

                  1 Reply Last reply
                  1

                  • Login

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