PDA

View Full Version : Why QCursor setPos() dosen't work here?



kar98k
29th May 2007, 13:43
Hi, i'm a novice ,this confused me a lot.
This program's purpose is: When the key_Up pressed the cursor move up 50 pixel.but it doen't move at all.:confused: .
Sorry for my poor English.:p

//**********************************
/*movecurosr.h*/
#ifndef MOVECURSOR_H
#define MOVECURSOR_H

#include<qwidget.h>
#include<qevent.h>
#include<qcursor.h>

class MoveCursor: public QWidget
{
public:
MoveCursor(QWidget *paren=0, const char *name=0);
private:
void keyPressEvent( QKeyEvent *);
QCursor *pCursor;
};

#endif

//**********************************
/*movecurosr.cpp*/
#include"movecurosr.h"

MoveCursor::MoveCursor(QWidget *parent, const char *name): QWidget(parent,name)
{
pCursor = new QCursor;
setFixedSize(640,480);
setFocusPolicy(QWidget::StrongFocus);
}
void MoveCursor::keyPressEvent( QKeyEvent * event)
{
QPoint posUp(0,-50);
QPoint posTmp;
int key = event->key();
if (key == Key_Up)
{
posTmp=pCursor->pos()+posUp;
pCursor->setPos(0,0);
cout<<pCursor->pos().rx()<<" "<<pCursor->pos().ry()<<endl;
}
//.....

}

//**********************************
/*main.cpp*/
#include<qapplication.h>
#include"movecursor.h"

int main(int argc,char **argv)
{
QApplication app(argc, argv, "moveCursor");
MoveCursor *pMoveCursor = new MoveCursor();
pMoveCursor->show();
app.setMainWidget(pMoveCursor);
return(app.exec());

}

wysota
29th May 2007, 13:58
What is the point of creating a new cursor?

high_flyer
29th May 2007, 14:04
wysotas question is well in place - however, your program is not moving the cursor:


if (key == Key_Up)
{
posTmp=pCursor->pos()+posUp;
pCursor->setPos(0,0); //<<<<<-------you always set position 0,0
cout<<pCursor->pos().rx()<<" "<<pCursor->pos().ry()<<endl;
}

ker98k
31st May 2007, 03:11
Thank you ,but it dose not work .
I use a rectagle instand. :)

wysota
31st May 2007, 09:05
Could you tell us what is the effect you want to achieve? Because I think you might be misunderstanding what the QCursor class is for...

high_flyer
31st May 2007, 09:21
Thank you ,but it dose not work .
I use a rectagle instand.
What does not work?
And you use a rectangle for... what exactly?

Gopala Krishna
31st May 2007, 19:35
I'm sorry , i'm not able to understand you properly.
What i think you need is just to move cursor by 50 pixels when up is pressed right ?

Then as pointed above already, change
pCursor->setPos(0,0) to
pCursor->setPos(posTmp);

And i guess by rectangle, you mean to restrict mouse cursor somewhere in a specified rect . Is this what you wanted to convey ?

kar98k
1st June 2007, 12:28
I want to move the cursor to some certain positions when the direction key is pressed.
That's a test program. Sorry for my bad presentation.:o
Now, i use a rectangle with more strike eye color (setCursor(BlankCursor) ),this behavies better.
But how to move the cursor is still unknow to me.
It is appretiated that if any大侠(superior:p ) could help to write an example program.
Thank you all.

wysota
1st June 2007, 12:52
Use the static QCursor::setPos method. And make sure you read its docs first.