Results 1 to 19 of 19

Thread: How to set my QPushbutton's background?

  1. #1
    Join Date
    Mar 2009
    Location
    Nanjing,China
    Posts
    40
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default How to set my QPushbutton's background?

    the plat: Qt4.3.2,Winxp,Vc6.0
    Hi all:
    I want to set my QPushButton's background when i click it,
    and when i click it,it will be show a QColorDialog,after i select an QColor like Qt::Red,
    I will set the background of that QPushbutton Qt::Red,or select Qt::Blue,I wil set it Qt::blue.

    But now I am failed.When I select some color,and use the QPalette to QPushbutton::setPalette(),but the background is not changed!

    some code like this:
    ---------------------------------------------------------
    MMywidget::listBtn_clicked()
    {
    QPalette palette = listColorBtn->palette();
    QColor listColor = palette.color(QPalette::Button);
    listColor = QColorDialog::getColor(listColor,this,tr("alarm list color set"));

    if(listColor.isValid())
    {
    palette.setColor(QPalette::Button,listColor);
    listColorBtn->setPalette(QPalette(QPalette::Button,listColor) );
    listColorBtn->setAutoFillBackground(true);
    tempCfg.alarmListBGColor = listColor;
    }
    }
    ---------------------------------------------------
    how can I set the background correct?
    thanks

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

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

    try this
    Qt Code:
    1. MMywidget::listBtn_clicked()
    2. {
    3. QPalette palette = listColorBtn->palette();
    4. QColor listColor = palette.color(QPalette::Button);
    5. listColor = QColorDialog::getColor(listColor,this,tr("alarm list color set"));
    6.  
    7. if(listColor.isValid())
    8. {
    9. palette.setColor(QPalette::Button,listColor);
    10. listColorBtn->setPalette(palette);
    11. listColorBtn->setAutoFillBackground(true);
    12. tempCfg.alarmListBGColor = listColor;
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 
    or you can also set background using Qt Style Sheets. have a look at this example
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Mar 2009
    Location
    Nanjing,China
    Posts
    40
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

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

    thanks,it does not work also.

    and if i user the QStyleSheet,can i use the QPushButton->setStyleSheet?
    and if use this function,how can I make the QString styleSheet?
    like this QString styleSheet = QString("QPushButton { background-color: %1; }").arg(listColor.name());
    but some error when I use those code,my application wil be crashed.

    Thanks

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

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

    Quote Originally Posted by cspp View Post
    thanks,it does not work also.
    works fine for me.
    can you post compilable example?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

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

    using QPalette
    Qt Code:
    1. QPalette palette = m_pbUpdateQuery->palette();
    2. QColor listColor = palette.color(QPalette::Button);
    3. listColor = QColorDialog::getColor(listColor,this,tr("alarm list color set"));
    4.  
    5. if(listColor.isValid()) {
    6. palette.setColor(QPalette::Button,listColor);
    7. m_pbUpdateQuery->setPalette(palette);
    8. m_pbUpdateQuery->setAutoFillBackground(true);
    9. }
    To copy to clipboard, switch view to plain text mode 
    using Style sheet
    Qt Code:
    1. QPalette palette = m_pbUpdateQuery->palette();
    2. QColor listColor = palette.color(QPalette::Button);
    3. listColor = QColorDialog::getColor(listColor,this,tr("alarm list color set"));
    4.  
    5. if(listColor.isValid()) {
    6. const QString styleSheet = QString("QPushButton {"
    7. "background-color: %1}").arg(listColor.name());
    8. m_pbUpdateQuery->setStyleSheet(styleSheet);
    9. m_pbUpdateQuery->setAutoFillBackground(true);
    10. }
    To copy to clipboard, switch view to plain text mode 

    both methods works fine.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. #6
    Join Date
    Mar 2009
    Location
    Nanjing,China
    Posts
    40
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

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

    The example:
    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include <QTranslator>
    3. #include <QLocale>
    4. #include <QLibraryInfo>
    5.  
    6. #include "dialog.h"
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QApplication app(argc, argv);
    11.  
    12. QString translatorFileName = QLatin1String("qt_");
    13. translatorFileName += QLocale::system().name();
    14. QTranslator *translator = new QTranslator(&app);
    15. if (translator->load(translatorFileName, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
    16. app.installTranslator(translator);
    17.  
    18. Dialog dialog;
    19. return dialog.exec();
    20. }
    To copy to clipboard, switch view to plain text mode 

    dialog.h
    Qt Code:
    1. #ifndef DIALOG_H
    2. #define DIALOG_H
    3.  
    4. #include <QDialog>
    5.  
    6. class QCheckBox;
    7. class QLabel;
    8.  
    9. class Dialog : public QDialog
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. Dialog(QWidget *parent = 0);
    15.  
    16. private slots:
    17. void setColor();
    18.  
    19. private:
    20. QCheckBox *native;
    21. QPushButton *colorButton;
    22. QLabel *colorLabel;
    23. QErrorMessage *errorMessageDialog;
    24.  
    25. QString openFilesPath;
    26. };
    27.  
    28. #endif
    To copy to clipboard, switch view to plain text mode 

    dialog.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include "dialog.h"
    3.  
    4. Dialog::Dialog(QWidget *parent)
    5. : QDialog(parent)
    6. {
    7. colorLabel = new QLabel;
    8. colorButton = new QPushButton(tr("QColorDialog::get&Color()"));
    9.  
    10. connect(colorButton, SIGNAL(clicked()), this, SLOT(setColor()));
    11.  
    12. QGridLayout *layout = new QGridLayout;
    13. layout->setColumnStretch(1, 1);
    14. layout->setColumnMinimumWidth(1, 250);
    15. layout->addWidget(colorButton, 0, 0);
    16. layout->addWidget(colorLabel, 0, 1);
    17. setLayout(layout);
    18.  
    19. setWindowTitle(tr("Standard Dialogs"));
    20. }
    21.  
    22. void Dialog::setColor()
    23. {
    24. QColor color = QColorDialog::getColor(Qt::green, this);
    25. if (color.isValid()) {
    26. colorLabel->setText(color.name());
    27. colorLabel->setPalette(QPalette(color));
    28. colorLabel->setAutoFillBackground(true);
    29.  
    30. QPalette palette = colorButton->palette();
    31. palette.setColor(QPalette::Button,color);
    32. colorButton->setPalette(palette);
    33. colorButton->setAutoFillBackground(true);
    34. }
    35. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Mar 2009
    Location
    Nanjing,China
    Posts
    40
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

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

    and if I set the add this code:
    Qt Code:
    1. colorButton->setFlat(true);
    To copy to clipboard, switch view to plain text mode 

    the button's background wiil be set.
    but I don't want to set the flat true!

  8. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

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

    still works fine
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  9. #9
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

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

    Quote Originally Posted by cspp View Post
    and if I set the add this code:
    Qt Code:
    1. colorButton->setFlat(true);
    To copy to clipboard, switch view to plain text mode 

    the button's background wiil be set.
    but I don't want to set the flat true!
    I did change flatness of a button, just compiled your example and it works.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  10. #10
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

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

    btw, I tested on Qt 4.5.0, which version of Qt do you use?
    PS. also read this, maybe you already know this.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  11. #11
    Join Date
    Mar 2009
    Location
    Nanjing,China
    Posts
    40
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

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

    QMake version 2.01a
    Using Qt version 4.3.2 in D:\Qt\4.3.2\lib

  12. #12
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

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

    Quote Originally Posted by cspp View Post
    QMake version 2.01a
    Using Qt version 4.3.2 in D:\Qt\4.3.2\lib
    maybe this is a bug in this Qt version, try to compile you code with older Qt version.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  13. #13
    Join Date
    Mar 2009
    Location
    Nanjing,China
    Posts
    40
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

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

    In my example,I use the setStyleSheet,it works well.But in my application,it will be crashed!
    I have no idea

    the crash infomation is:
    Qt has caught an exception thrown from an event handler. Throwing
    exceptions from an event handler is not supported in Qt. You must
    reimplement QApplication::notify() and catch all exceptions there.

    Qt has caught an exception thrown from an event handler. Throwing
    exceptions from an event handler is not supported in Qt. You must
    reimplement QApplication::notify() and catch all exceptions there.
    In my application,I have a framework,it will be load some QLibrary,in the
    Lib,it wil create some widget,like QPushButton,like the listColorBtn.
    and now when I setStyleSheet in listColorBtn,it crashed!

  14. #14
    Join Date
    Mar 2009
    Location
    Nanjing,China
    Posts
    40
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

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

    Quote Originally Posted by spirit View Post
    maybe this is a bug in this Qt version, try to compile you code with older Qt version.
    I compiled my code in QT4.5,
    it works well.

    I think it just some Bug in Qt4.3.2!

  15. #15
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

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

    yup, I don't remember the task number, but it was on the tracker.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  16. #16
    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?

    I followed the exact code to update the QPalette to change the QPushButton's color but it never showed the color. I compiled my code for Qt 4.5.3 on *both* Unix/X11 and Qt-Embedded, none worked I tried Qt Designer as well, color's not shown either. It works with style sheet, but what I'm trying to have a blinking button that changes colors ever 0.5s, and just that eats up 95% CPU consumption! Admittedly we don't have a very powerful one so I was hoping using QPalette would help the performance.

    Anyhow, do you guys have any more ideas? I do have a style sheet applied at the top level Form, but it should be overwritten when I update palette for a particular button, right?

    Thanks in advance.

  17. #17
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

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

    can you show us you code?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  18. #18
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    I've gor a question.
    I use the following codes to change the background color of QLabel, but it doesn't work!
    Qt Code:
    1. QPalette pale = ui->label->palette();
    2. pale.setColor(QPalette::Window, listColor);
    3. ui->label->setAutoFillBackground(true);
    4. ui->label->setText(listColor.name());
    To copy to clipboard, switch view to plain text mode 
    While if I use
    Qt Code:
    1. ui->label->setPalette(QPalette(listColor));
    To copy to clipboard, switch view to plain text mode 
    it works.
    why??

  19. #19
    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.