Search code examples
c++qtubuntu-12.04qcursor

Qt QCursor::setPos() does nothing


I've created a minimal class to demonstrate my problem. I'm trying to set the cursor position, but it shows no effect. In my example class I try to center the cursor to the widget. Here is my class:

class testWidget : public QWidget
{
    Q_OBJECT
public:
    testWidget();
protected:
    virtual void mouseMoveEvent(QMouseEvent* event);
};

And here is the implementation:

testWidget::testWidget()
{
    setMinimumSize(800,600);
    show();
}

void testWidget::mouseMoveEvent(QMouseEvent *event)
{
    QPoint before(mapFromGlobal(QCursor::pos()));
    QPoint center = mapToGlobal(QPoint(width()/2,height()/2));
    QCursor::setPos(center);
    qDebug()<<"Before:"<<before<<"After:"<<mapFromGlobal(QCursor::pos());
}

When moving the mouse cursor while pressing a mouse button I get the following output (exmaple):

Before: QPoint(754,48) After: QPoint(400,300)

This means before I called QCursor::setPos(center) the cursor is at the position 754;48 which is in the top-right corner of the widget. After I set the cursor-position with QCursor::setPosition(center) the cursor should be at the center of the widget, which it is not, the cursor stays in the top-right corner. And to my further confusion, QCursor::pos() returns the center of the widget, even though the cursor is not at the center.

Any hints would be much appreciated.

Thank you for your time...


Solution

  • @Leiaz You are right, I've been working in a virtual machine. When running the program on my host system (which is also ubuntu) it works as expected. Thank you for your help.

    It even works in my VM now, after I've disabled the mouse integration.

    Thanks all.