Results 1 to 2 of 2

Thread: Changing QPushButton text colour with mouseMoveEvent

  1. #1
    Join Date
    Jun 2008
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Changing QPushButton text colour with mouseMoveEvent

    Hi
    In the first place excuse my english I am Slovak

    So, I am trying to change text colour on my QPushButton when my mouse is over it. Actually it is not a QPushButton but a class based on it becouse i want only to show the text not a frame. The first colour is blue. The second colour is green and it is sets when mouse is over the button. The third colour is orange and it is sets when the button is clicked. This working but when I move the mouse over it and then away the text colour is still green but I want to chenge it back to blue. This is my code:

    Qt Code:
    1. #include "cudlik.h"
    2. #include <QPainter>
    3. #include <QMouseEvent>
    4. #include <QPushButton>
    5.  
    6. Cudlik::Cudlik(const QString & text, QPushButton *parent)
    7. : QPushButton(parent)
    8. {
    9. setText(text);
    10. farba=(QColor(37,123,218));
    11. setMouseTracking(true);
    12. }
    13.  
    14. Cudlik::~Cudlik()
    15. {
    16.  
    17. }
    18.  
    19. void Cudlik::paintEvent(QPaintEvent *event)
    20. {
    21. painter = new QPainter(this);
    22. painter->setRenderHint(QPainter::Antialiasing);
    23. painter->setPen(farba);
    24. painter->setBrush(QBrush(farba));
    25. painter->drawText(10,10,text());
    26. delete painter;
    27. }
    28.  
    29. void Cudlik::mouseMoveEvent(QMouseEvent* event)
    30. {
    31. QPoint kurzor = event->globalPos();
    32. QPoint napis = pos();
    33. if (kurzor.x()<napis.x() || kurzor.x()>napis.x()+width() || kurzor.y()<napis.y() || kurzor.y()>napis.y()+height())
    34. {
    35. farba=(QColor(37,123,218));
    36. //update();
    37. }
    38. else
    39. {
    40. farba=(QColor(108,203,52));
    41. update();
    42. }
    43. }
    44.  
    45. void Cudlik::mousePressEvent(QMouseEvent* event)
    46. {
    47. farba=QColor(238,105,17);
    48. move(x()+2,y()+4);
    49. update();
    50. }
    51.  
    52. void Cudlik::mouseReleaseEvent(QMouseEvent* event)
    53. {
    54. move(x()-2,y()-4);
    55. emit clicked();
    56. update();
    57. }
    To copy to clipboard, switch view to plain text mode 

    I tried to solve it by the if condition but it doesnt work. I think that the mouseMoveEvent is emited only when cursor is over the widget so this condition is useless. And I dont know how to solve this problem. Please help.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Changing QPushButton text colour with mouseMoveEvent

    You could do it with QWidget::enterEvent() and QWidget::leaveEvent() but I'd recommend taking a look at style sheets.
    J-P Nurmi

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Changing text colour of QLineEdit qss file
    By phillip_Qt in forum Qt Programming
    Replies: 2
    Last Post: 4th April 2008, 05:28
  3. Changing colour thru qss file
    By phillip_Qt in forum Qt Programming
    Replies: 3
    Last Post: 27th March 2008, 05:36

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.