Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QPixmap::grabWindow how to grab mouse cursor ?



  • 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());
    

  • Lifetime Qt Champion

    Hi,

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



  • @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());
    

  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

    Do you mean QCursor::pos ?



  • @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;
    }
    
    

  • Lifetime Qt Champion

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


Log in to reply