PDA

View Full Version : Cannot update mouse Pointer position



srinirao
2nd November 2009, 08:02
Hi,

I have modified scribble example (found at following link) code to capture leaveEvent and keyPressEvent.

http://doc.trolltech.com/4.5/widgets-scribble.html

And whenever this event occurs, within this event handler i am doing QCursor::setPos(x, y) to reposition my mouse pointer to within mainwindow boundary area. And inside keyPressEvent handler i am moving the cursor by 1 pixel when ever the direction keys are pressed. Whenever the leaveEvent occurs because of the keypresses made to move out of the boundary, I am unable to set mouse Pointer position in the same event iteration. However i have to press one extra key to update the mouse pointer.

Also though i have configured setMouseTracking(true) in my constructor, I am unable to get MouseMove Events.

Am i missing something trivial here ?

wysota
3rd November 2009, 09:17
We'd have to see some code.

srinirao
5th November 2009, 07:18
My intention is to mimic Gtk example : http://gtkforums.com/about182.html

I have re-implemented leavemouse and key press event handler in mainwindow class as follows. My intention is to get the mouse pointer move on key presses. But when it hits the boundary the leavemouse event should re-position itself to within the window by adjusting few pixels. I event tried generating mouseMoveEvent from within leave. But it is just creating another mouse move event. I am also unable to get contineous mouseMove event when i re-implement it in MainWindow class.



void MainWindow::keyPressEvent(QKeyEvent *ke)
{
int dx = 0;
int dy = 0;
int offset = 1;
printf("%s: Got key %d\n", __FUNCTION__, ke->key());
if ( ke->isAutoRepeat() )
offset = 5;
if ( ke->key() == Qt::Key_Left )
dx = -offset;
else if ( ke->key() == Qt::Key_Right)
dx = offset;
else if ( ke->key() == Qt::Key_Up)
dy = -offset;
else if ( ke->key() == Qt::Key_Down)
dy = offset;
else if ( ke->key() == Qt::Key_Q)
{
printf("%s: Got Quit key \n", __FUNCTION__);
exit(1);
}
if ( dx != 0 || dy != 0 )
{
const QRect rect = scribbleArea->contentsRect();
const QPoint pos = scribbleArea->mapFromGlobal(QCursor::pos());
printf("%s: Got Rect as (%d,%d), top=%d, bottom=%d, width=%d, height=%d \n", __FUNCTION__,
rect.x(), rect.y(), rect.top(), rect.bottom(), rect.width(), rect.height());
printf("%s: Got position as global(%d,%d), QCursor(%d,%d) \n", __FUNCTION__,
pos.x(), pos.y(), QCursor::pos().x(), QCursor::pos().y());
int x = pos.x() + dx;
int y = pos.y() + dy;
#if 1
QCursor::setPos(scribbleArea->mapToGlobal(QPoint(x, y)));
update();
#else
QMouseEvent event1(QEvent::MouseMove, QPoint(x,y), (Qt::MouseButton)0, (Qt::MouseButtons)0, 0);
QApplication::sendEvent(this, &event1);
#endif
}
}
void MainWindow::leaveEvent(QEvent *event)
{
const QRect rect = scribbleArea->contentsRect();
const QPoint pos = scribbleArea->mapFromGlobal(QCursor::pos());
/*
printf("%s: Got Rect as (%d,%d), top=%d, bottom=%d, width=%d, height=%d \n", __FUNCTION__,
rect.x(), rect.y(), rect.top(), rect.bottom(), rect.width(), rect.height());
printf("%s: Got position as global(%d,%d), QCursor(%d,%d) \n", __FUNCTION__,
pos.x(), pos.y(), QCursor::pos().x(), QCursor::pos().y());
*/
int x = pos.x();
int y = pos.y();
if(pos.y() < rect.top())
{
printf("%s: Moving Up \n", __FUNCTION__);
y = rect.top() + 1;
}
else if(pos.y() > rect.bottom())
{
printf("%s: Moving Down \n", __FUNCTION__);
y = rect.bottom() -1;
}
else if(pos.x() < rect.left())
{
printf("%s: Moving Left \n", __FUNCTION__);
x = rect.left() + 1;
}
else if(pos.x() > rect.right())
{
printf("%s: Moving Right \n", __FUNCTION__);
x = rect.right() - 1;
}
else
{
printf("%s: Invalid Leave \n", __FUNCTION__);
}
#if 0
QCursor::setPos(scribbleArea->mapToGlobal(QPoint(100, 100)));
#else
QCursor::setPos(scribbleArea->mapToGlobal(QPoint(x, y)));
#endif
QApplication::processEvents();
}
srinirao@srinirao:~> wc -l mainwindow.cpp
83 mainwindow.cpp
srinirao@srinirao:~> cat mainwindow.cpp
void MainWindow::keyPressEvent(QKeyEvent *ke)
{
int dx = 0;
int dy = 0;
int offset = 1;
printf("%s: Got key %d\n", __FUNCTION__, ke->key());
if ( ke->isAutoRepeat() )
offset = 5;
if ( ke->key() == Qt::Key_Left )
dx = -offset;
else if ( ke->key() == Qt::Key_Right)
dx = offset;
else if ( ke->key() == Qt::Key_Up)
dy = -offset;
else if ( ke->key() == Qt::Key_Down)
dy = offset;
else if ( ke->key() == Qt::Key_Q)
{
printf("%s: Got Quit key \n", __FUNCTION__);
exit(1);
}
if ( dx != 0 || dy != 0 )
{
const QRect rect = scribbleArea->contentsRect();
const QPoint pos = scribbleArea->mapFromGlobal(QCursor::pos());
printf("%s: Got Rect as (%d,%d), top=%d, bottom=%d, width=%d, height=%d \n", __FUNCTION__,
rect.x(), rect.y(), rect.top(), rect.bottom(), rect.width(), rect.height());
printf("%s: Got position as global(%d,%d), QCursor(%d,%d) \n", __FUNCTION__,
pos.x(), pos.y(), QCursor::pos().x(), QCursor::pos().y());
int x = pos.x() + dx;
int y = pos.y() + dy;
#if 1
QCursor::setPos(scribbleArea->mapToGlobal(QPoint(x, y)));
update();
#else
QMouseEvent event1(QEvent::MouseMove, QPoint(x,y), (Qt::MouseButton)0, (Qt::MouseButtons)0, 0);
QApplication::sendEvent(this, &event1);
#endif
}
}
void MainWindow::leaveEvent(QEvent *event)
{
const QRect rect = scribbleArea->contentsRect();
const QPoint pos = scribbleArea->mapFromGlobal(QCursor::pos());
/*
printf("%s: Got Rect as (%d,%d), top=%d, bottom=%d, width=%d, height=%d \n", __FUNCTION__,
rect.x(), rect.y(), rect.top(), rect.bottom(), rect.width(), rect.height());
printf("%s: Got position as global(%d,%d), QCursor(%d,%d) \n", __FUNCTION__,
pos.x(), pos.y(), QCursor::pos().x(), QCursor::pos().y());
*/
int x = pos.x();
int y = pos.y();
if(pos.y() < rect.top())
{
printf("%s: Moving Up \n", __FUNCTION__);
y = rect.top() + 1;
}
else if(pos.y() > rect.bottom())
{
printf("%s: Moving Down \n", __FUNCTION__);
y = rect.bottom() -1;
}
else if(pos.x() < rect.left())
{
printf("%s: Moving Left \n", __FUNCTION__);
x = rect.left() + 1;
}
else if(pos.x() > rect.right())
{
printf("%s: Moving Right \n", __FUNCTION__);
x = rect.right() - 1;
}
else
{
printf("%s: Invalid Leave \n", __FUNCTION__);
}
#if 0
QCursor::setPos(scribbleArea->mapToGlobal(QPoint(100, 100)));
#else
QCursor::setPos(scribbleArea->mapToGlobal(QPoint(x, y)));
#endif
QApplication::processEvents();
}

srinirao
12th November 2009, 09:09
I found something here, The QCursor::setPos() is not working for me in my platform. Let us say i will set the cursor position to some fixed co-ordinates, it doesnot position the cursor to that point. But the same example seems to work fine on i386 machine.


QCursor::setPos(mapToGlobal(QPoint(100, 100)));


after following the code, it is finally calling :


void QWSDisplay::setCursorPosition(int x, int y)

This does not seem to be working.