Results 1 to 9 of 9

Thread: Why QCursor setPos() dosen't work here?

  1. #1
    Join Date
    May 2007
    Posts
    2
    Thanks
    4
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default 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. .
    Sorry for my poor English.

    //**********************************
    /*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());

    }

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why QCursor setPos() dosen't work here?

    What is the point of creating a new cursor?

  3. The following user says thank you to wysota for this useful post:

    ker98k (30th May 2007)

  4. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Why QCursor setPos() dosen't work here?

    wysotas question is well in place - however, your program is not moving the cursor:
    Qt Code:
    1. if (key == Key_Up)
    2. {
    3. posTmp=pCursor->pos()+posUp;
    4. pCursor->setPos(0,0); //<<<<<-------you always set position 0,0
    5. cout<<pCursor->pos().rx()<<" "<<pCursor->pos().ry()<<endl;
    6. }
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. The following user says thank you to high_flyer for this useful post:

    ker98k (30th May 2007)

  6. #4
    Join Date
    May 2007
    Posts
    1
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Why QCursor setPos() dosen't work here?

    Thank you ,but it dose not work .
    I use a rectagle instand.

  7. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

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

  8. The following user says thank you to wysota for this useful post:

    kar98k (1st June 2007)

  9. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Why QCursor setPos() dosen't work here?

    Thank you ,but it dose not work .
    I use a rectagle instand.
    What does not work?
    And you use a rectangle for... what exactly?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  10. The following user says thank you to high_flyer for this useful post:

    kar98k (1st June 2007)

  11. #7
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default 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
    Qt Code:
    1. pCursor->setPos(0,0)
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. pCursor->setPos(posTmp);
    To copy to clipboard, switch view to plain text mode 

    And i guess by rectangle, you mean to restrict mouse cursor somewhere in a specified rect . Is this what you wanted to convey ?
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  12. The following user says thank you to Gopala Krishna for this useful post:

    kar98k (1st June 2007)

  13. #8
    Join Date
    May 2007
    Posts
    2
    Thanks
    4
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default 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.
    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 ) could help to write an example program.
    Thank you all.

  14. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why QCursor setPos() dosen't work here?

    Use the static QCursor::setPos method. And make sure you read its docs first.

  15. The following user says thank you to wysota for this useful post:

    kar98k (2nd June 2007)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.