Results 1 to 19 of 19

Thread: How to set my QPushbutton's background?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #15
    Join Date
    Apr 2009
    Posts
    21
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to set my QPushbutton's background?

    Here's my code:

    mywindow.h (ui_form.h was generated using Qt Designer, it's huge so I won't put it here)
    Qt Code:
    1. #ifndef MYWINDOW_H
    2. #define MYWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QColor>
    6. #include "ui_form.h"
    7.  
    8. class MyWindow1 : public QMainWindow
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. MyWindow1(const Ui::Form& ui);
    14. ~MyWindow1() {}
    15.  
    16. void setTimer(int ms);
    17.  
    18. public slots:
    19. void blink() ;
    20.  
    21. private:
    22. Ui::Form mUi;
    23. QColor mColor;
    24. };
    25.  
    26. #endif
    To copy to clipboard, switch view to plain text mode 

    mywindow.cpp
    Qt Code:
    1. #include <QPalette>
    2. #include <QIcon>
    3. #include <QtCore/QTimer>
    4. #include "mywindow.h"
    5.  
    6. MyWindow1::MyWindow1(const Ui::Form& ui) : QMainWindow()
    7. {
    8. mUi = ui;
    9. mUi.setupUi(this);
    10. mColor = Qt::red;
    11. }
    12.  
    13. void MyWindow1::setTimer(int ms)
    14. {
    15. if (!mUi.pushButton_114)
    16. printf("setTimer: invalid button\n");
    17.  
    18. QTimer *timer = new QTimer(this);
    19. connect(timer, SIGNAL(timeout()), this, SLOT(blink()));
    20. timer->start(ms);
    21. }
    22.  
    23. void MyWindow1::blink()
    24. {
    25. if (!mUi.pushButton_114)
    26. {
    27. printf("Invalid button\n");
    28. return;
    29. }
    30.  
    31. if (mColor == Qt::green)
    32. mColor = Qt::red;
    33. else
    34. mColor = Qt::green;
    35.  
    36. // DOES NOT WORK
    37. QPalette p = mUi.pushButton_114->palette();
    38. p.setColor(QPalette::Button, mColor);
    39. mUi.pushButton_114->setPalette(p);
    40. mUi.pushButton_114->setAutoFillBackground(true);
    41.  
    42. #if 0
    43. // THIS WORKS
    44. QString style = mUi.pushButton_114->styleSheet();
    45. if (style.contains("green"))
    46. {
    47. mUi.pushButton_114->setStyleSheet(
    48. QString::fromUtf8("background-color: red;"));
    49. }
    50. else
    51. {
    52. mUi.pushButton_114->setStyleSheet(
    53. QString::fromUtf8("background-color: green;"));
    54. }
    55. #endif
    56. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "mywindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7. Ui::Form ui;
    8. MyWindow1 *widget = new MyWindow1(ui);
    9. widget->setTimer(500);
    10. widget->show();
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by cookie1909; 24th April 2009 at 16:45.

Similar Threads

  1. Replies: 5
    Last Post: 21st July 2010, 22:51
  2. Replies: 2
    Last Post: 10th February 2009, 13:12
  3. QLCDNumber with transparent background?
    By PolyVox in forum Qt Programming
    Replies: 2
    Last Post: 20th October 2008, 05:34
  4. background colour
    By kw in forum Qt Programming
    Replies: 6
    Last Post: 11th April 2006, 00:44
  5. Replies: 1
    Last Post: 5th April 2006, 16:44

Tags for this Thread

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.