4
votes

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

1
What are your expectations on the mouse cursor position? What kind of behavior do you expect?vahancho
@vahancho I'd like it be positioned at the center of my widget. I know, this seems to be a annoying behaviour but it's only a test-class to demonstrate my problem.david
But maybe QPoint(467,822) is already the center?vahancho
@vahancho Yes, you are right, that is the center, but the problem is that the cursor is not at the center, only the query (QCursor::pos()) returns the center. I've edited my question to make the problem more clear.david
I could not understand your problem, you mean that the current cursor position does not update (or needed mouse pressed for work)?Guilherme Nascimento

1 Answers

4
votes

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