Results 1 to 7 of 7

Thread: Using QTimer and mouseMoveEvent

  1. #1
    Join Date
    Sep 2013
    Posts
    20
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Using QTimer and mouseMoveEvent

    I am trying to use a QTimer with a mouseMoveEvent to highlight some cards. I do not know what I am doing wrong. If someone can give me some useful hints.

    Qt Code:
    1. //In Constructor
    2. QTimer *timer = new QTimer(this);
    3. connect(timer, SIGNAL(timeout()), this, SLOT(highlightCard()));
    4. timer->start(500);
    5.  
    6. void Canvas::mouseMoveEvent(QMouseEvent *event)
    7. {
    8. if (event->type() == QEvent::MouseMove)
    9. {
    10. x_pos = event->pos().x();
    11. y_pos = event->pos().y();
    12. }
    13. }
    14.  
    15. void Canvas::highlightCard()
    16. {
    17. QGraphicsColorizeEffect *effect = new QGraphicsColorizeEffect(this);
    18. effect->setColor(QPalette::Highlight);
    19.  
    20. for (int i = 0; i < MAXCARDS; i++)
    21. {
    22. if (cardIconPlayer[i]->pos() == QPoint(x_pos, y_pos))
    23. cardIconPlayer[i]->setGraphicsEffect(effect);
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using QTimer and mouseMoveEvent

    What do you expect to happen?

    Do you manage to move the mouse to exactly one of the cardIconPlayer positions in the 500ms after creating the widget?

    Cheers,
    _

  3. #3
    Join Date
    Sep 2013
    Posts
    20
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Using QTimer and mouseMoveEvent

    I am a newbie, so I erroneously thought this code will highlight the card. So yes that is what I thought would happen. If you can point out what I can do to highlight those cardIconPlayer's with other methods, corrections, or further my studies in a certain topic then I would greatly appreciate it.

    I know C++ but not Qt, so this is interesting to me but really new.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Using QTimer and mouseMoveEvent

    Your highlight routine is called once every 500 ms and will do something if the last mouse move event over this widget recorded a position that precisely matches the position of one of your cardIconPlayer objects (whatever they are). That's a precise match on a single pixel position not an area: unlikely to occur, I think you'll agree.

    What type are the cardIconPlayer objects?
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  5. #5
    Join Date
    Sep 2013
    Posts
    20
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Using QTimer and mouseMoveEvent

    cardIconPlayer is a QLabel and this is how I set up the icons using pixmap.

    Qt Code:
    1. void Canvas::setUpPlayerIcons(string *pString)
    2. {
    3. QPixmap qpx;
    4. QSize iconSize;
    5. string str;
    6.  
    7. for (int i = 0; i < MAXCARDS; i++)
    8. {
    9. shadow = new QGraphicsDropShadowEffect(this);
    10. shadow->setBlurRadius(10);
    11.  
    12. cardIconPlayer[i] = new QLabel(cardTable);
    13. str = pString[i];
    14. qpx = QPixmap(str.c_str());
    15. iconSize = qpx.size();
    16. iconSize.scale(190, 276, Qt::KeepAspectRatio);
    17. QPixmap scaledImage = qpx.scaled(iconSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    18. cardIconPlayer[i]->setPixmap(scaledImage);
    19. cardIconPlayer[i]->move(20 + 60 * i, 350);
    20. cardIconPlayer[i]->setGraphicsEffect(shadow);
    21.  
    22. //playerPixmaps.push_back(scaledImage);
    23. }
    24.  
    25. }
    To copy to clipboard, switch view to plain text mode 

    Just to get this straight. Are you saying I should use QRect instead of QPoint.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Using QTimer and mouseMoveEvent

    I would be inclined to make the QLabels look after their own highlighting when the mouse is over them.

    Qt Code:
    1. #include <QApplication>
    2. #include <QWidget>
    3. #include <QVBoxLayout>
    4. #include <QLabel>
    5. #include <QPixmap>
    6. #include <QGraphicsDropShadowEffect>
    7.  
    8. class Label: public QLabel
    9. {
    10. Q_OBJECT
    11. public:
    12. Label(QWidget *p = 0): QLabel(p)
    13. {
    14. QPixmap pixmap(200, 200);
    15. pixmap.fill(QColor("mediumseagreen"));
    16. setPixmap(pixmap);
    17. setFrameStyle(QFrame::Box);
    18.  
    19. QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
    20. shadow->setBlurRadius(10);
    21. shadow->setColor(QColor("orchid"));
    22. setGraphicsEffect(shadow);
    23. }
    24. protected:
    25. void enterEvent(QEvent *event)
    26. {
    27. QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
    28. shadow->setBlurRadius(10);
    29. shadow->setColor(QColor("lightsalmon"));
    30. setGraphicsEffect(shadow);
    31. }
    32. void leaveEvent(QEvent *event)
    33. {
    34. QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
    35. shadow->setBlurRadius(10);
    36. shadow->setColor(QColor("orchid"));
    37. setGraphicsEffect(shadow);
    38. }
    39. };
    40.  
    41. class Widget: public QWidget
    42. {
    43. Q_OBJECT
    44. public:
    45. Widget(QWidget *p = 0): QWidget(p)
    46. {
    47. QVBoxLayout *layout = new QVBoxLayout(this);
    48. layout->addWidget(new Label);
    49. layout->addWidget(new Label);
    50. }
    51. };
    52.  
    53. int main(int argc, char **argv)
    54. {
    55. QApplication app(argc, argv);
    56. Widget w;
    57. w.show();
    58. return app.exec();
    59. }
    60. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to ChrisW67 for this useful post:

    LaTj (3rd December 2013)

  8. #7
    Join Date
    Sep 2013
    Posts
    20
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Using QTimer and mouseMoveEvent

    Thank you very much, this was more than thankful. Sorry for the late reply

Similar Threads

  1. Replies: 1
    Last Post: 25th October 2012, 19:47
  2. Replies: 15
    Last Post: 4th August 2012, 19:11
  3. mouseMoveEvent
    By weixj2003ld in forum Qt Programming
    Replies: 1
    Last Post: 3rd November 2009, 09:04
  4. mouseMoveEvent receiving zero's
    By JohnTh in forum Newbie
    Replies: 2
    Last Post: 11th September 2009, 17:31
  5. About the mouseMoveEvent()
    By bingoking in forum Qt Programming
    Replies: 3
    Last Post: 26th September 2008, 11:56

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
  •  
Qt is a trademark of The Qt Company.