Results 1 to 6 of 6

Thread: long mouse press event

  1. #1
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default long mouse press event

    Hi, if there is any way i can detect a mouse long press event in qt. Because i want to keep drawing a different circle on the mainwindow while my mouse is down. thanks)

  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: long mouse press event

    Intercept mousePressEvent and start a timer there. Intercept a mouseMoveEvent or mouseReleaseEvent (depending on what you want to do) and check the timer. If it exceeds a predefined threshold, you have a long press, otherwise you have a short press.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: long mouse press event

    thanks, but i need to have a long press that can press as long time as i want. can a timer do that?

  4. #4
    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: long mouse press event

    The timer doesn't care. It just measures time. If I understand you right and you want to keep increasing the circle diameter as the user is keeping the button down, you can start a timer to some short interval like 25ms and each time it fires, increase the diameter a bit. When the button is released, stop the timer and draw the final circle.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: long mouse press event

    thanks, but i need to draw the circle while i am pressing the left button not release the button. Can you teach me how?

  6. #6
    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: long mouse press event

    Quote Originally Posted by tonylin0826 View Post
    thanks, but i need to draw the circle while i am pressing the left button not release the button.
    That's what I meant. You are drawing the circle while the button is pressed but when it is released, you need to make your item permanent.

    Here is a simple example that uses QGraphicsView:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class View : public QGraphicsView {
    4. Q_OBJECT
    5. public:
    6. View() : QGraphicsView() {
    7. current = 0;
    8. radius = 0;
    9. t.setInterval(10);
    10. connect(&t, SIGNAL(timeout()), this, SLOT(incDiameter()));
    11. }
    12.  
    13. void mousePressEvent(QMouseEvent *me) {
    14. current = new QGraphicsEllipseItem;
    15. radius = 5;
    16. QRect r;
    17. r.setWidth(2*radius);
    18. r.setHeight(2*radius);
    19. r.moveCenter(mapToScene(me->pos()).toPoint());
    20. current->setRect(r);
    21. scene()->addItem(current);
    22. t.start();
    23. }
    24. void mouseReleaseEvent(QMouseEvent *me) {
    25. t.stop();
    26. current = 0;
    27. }
    28. private slots:
    29. void incDiameter() {
    30. QRect r;
    31. radius++;
    32. r.setWidth(2*radius);
    33. r.setHeight(2*radius);
    34. r.moveCenter(current->rect().center().toPoint());
    35. current->setRect(r);
    36. }
    37. private:
    38. QTimer t;
    39. int radius;
    40. };
    41.  
    42. #include "main.moc"
    43.  
    44.  
    45. int main(int argc, char **argv) {
    46. QApplication app(argc, argv);
    47. View view;
    48. view.setScene(new QGraphicsScene(0,0,600,400));
    49. view.show();
    50. return app.exec();
    51. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Long press event
    By alfah in forum Qt Programming
    Replies: 5
    Last Post: 17th August 2011, 14:26
  2. QGraphicsview and mouse press event
    By eva2002 in forum Qt Programming
    Replies: 6
    Last Post: 26th January 2010, 06:04
  3. Mouse press event in a QGraphicsScene
    By Lykurg in forum Qt Programming
    Replies: 3
    Last Post: 19th June 2009, 11:28
  4. Mouse press event detection
    By A.H.M. Mahfuzur Rahman in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2009, 14:08
  5. Checking for key press on mouse event
    By Cruz in forum Newbie
    Replies: 1
    Last Post: 24th January 2009, 19:18

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.