Why QCursor setPos() dosen't work here?
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());
}
Re: Why QCursor setPos() dosen't work here?
What is the point of creating a new cursor?
Re: Why QCursor setPos() dosen't work here?
wysotas question is well in place - however, your program is not moving the cursor:
Code:
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;
}
Re: Why QCursor setPos() dosen't work here?
Thank you ,but it dose not work .
I use a rectagle instand. :)
Re: Why QCursor setPos() dosen't work here?
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...
Re: Why QCursor setPos() dosen't work here?
Quote:
Thank you ,but it dose not work .
I use a rectagle instand.
What does not work?
And you use a rectangle for... what exactly?
Re: Why QCursor setPos() dosen't work here?
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
Code:
pCursor->setPos(0,0)
to
Code:
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 ?
Re: Why QCursor setPos() dosen't work here?
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.
Re: Why QCursor setPos() dosen't work here?
Use the static QCursor::setPos method. And make sure you read its docs first.