4

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

david
  • 168
  • 1
  • 11
  • What are your expectations on the mouse cursor position? What kind of behavior do you expect? – vahancho Nov 12 '14 at 11:15
  • @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 Nov 12 '14 at 11:23
  • But maybe QPoint(467,822) is already the center? – vahancho Nov 12 '14 at 12:03
  • @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 Nov 12 '14 at 13:08
  • I could not understand your problem, you mean that the current cursor position does not update (or needed mouse pressed for work)? – Protomen Nov 12 '14 at 13:18
  • @GuilhermeNascimento He wants to move the cursor, not just update the position. – user1095108 Nov 12 '14 at 13:21
  • The cursor on the screen does not jump to the center of the widget, it behaves as if I've never called QCursor::setPos(). And yes I've to press a mouse button to invoke the mouseMoveEvent slot, because I've not enabled mouse tracking. – david Nov 12 '14 at 13:23
  • Sorry I did not explain right (not explained clearly), I meant `mouseMoveEvent` needed mouse is pressed for work, see: http://stackoverflow.com/questions/1935021/getting-mousemoveevents-in-qt – Protomen Nov 12 '14 at 13:38
  • This should work. Are you using Ubuntu directly or in a virtual machine ? – Leiaz Nov 12 '14 at 17:31

1 Answers1

4

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

david
  • 168
  • 1
  • 11