PDA

View Full Version : How to set my QPushbutton's background?



cspp
10th April 2009, 08:02
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

spirit
10th April 2009, 08:06
try 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(palette);
listColorBtn->setAutoFillBackground(true);
tempCfg.alarmListBGColor = listColor;
}
}

or you can also set background using Qt Style Sheets (http://doc.trolltech.com/4.5/stylesheet.html). have a look at this example (http://doc.trolltech.com/4.5/stylesheet-examples.html#customizing-qpushbutton)

cspp
10th April 2009, 08:20
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

spirit
10th April 2009, 08:23
thanks,it does not work also.
works fine for me.
can you post compilable example?

spirit
10th April 2009, 08:28
using QPalette


QPalette palette = m_pbUpdateQuery->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);
m_pbUpdateQuery->setPalette(palette);
m_pbUpdateQuery->setAutoFillBackground(true);
}

using Style sheet


QPalette palette = m_pbUpdateQuery->palette();
QColor listColor = palette.color(QPalette::Button);
listColor = QColorDialog::getColor(listColor,this,tr("alarm list color set"));

if(listColor.isValid()) {
const QString styleSheet = QString("QPushButton {"
"background-color: %1}").arg(listColor.name());
m_pbUpdateQuery->setStyleSheet(styleSheet);
m_pbUpdateQuery->setAutoFillBackground(true);
}


both methods works fine.

cspp
10th April 2009, 08:37
The example:
main.cpp


#include <QApplication>
#include <QTranslator>
#include <QLocale>
#include <QLibraryInfo>

#include "dialog.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QString translatorFileName = QLatin1String("qt_");
translatorFileName += QLocale::system().name();
QTranslator *translator = new QTranslator(&app);
if (translator->load(translatorFileName, QLibraryInfo::location(QLibraryInfo::TranslationsP ath)))
app.installTranslator(translator);

Dialog dialog;
return dialog.exec();
}


dialog.h


#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

class QCheckBox;
class QLabel;
class QErrorMessage;

class Dialog : public QDialog
{
Q_OBJECT

public:
Dialog(QWidget *parent = 0);

private slots:
void setColor();

private:
QCheckBox *native;
QPushButton *colorButton;
QLabel *colorLabel;
QErrorMessage *errorMessageDialog;

QString openFilesPath;
};

#endif


dialog.cpp


#include <QtGui>
#include "dialog.h"

Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
colorLabel = new QLabel;
colorButton = new QPushButton(tr("QColorDialog::get&Color()"));

connect(colorButton, SIGNAL(clicked()), this, SLOT(setColor()));

QGridLayout *layout = new QGridLayout;
layout->setColumnStretch(1, 1);
layout->setColumnMinimumWidth(1, 250);
layout->addWidget(colorButton, 0, 0);
layout->addWidget(colorLabel, 0, 1);
setLayout(layout);

setWindowTitle(tr("Standard Dialogs"));
}

void Dialog::setColor()
{
QColor color = QColorDialog::getColor(Qt::green, this);
if (color.isValid()) {
colorLabel->setText(color.name());
colorLabel->setPalette(QPalette(color));
colorLabel->setAutoFillBackground(true);

QPalette palette = colorButton->palette();
palette.setColor(QPalette::Button,color);
colorButton->setPalette(palette);
colorButton->setAutoFillBackground(true);
}
}

cspp
10th April 2009, 08:42
and if I set the add this code:


colorButton->setFlat(true);


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

spirit
10th April 2009, 08:44
still works fine :)

spirit
10th April 2009, 08:45
and if I set the add this code:


colorButton->setFlat(true);


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

I did change flatness of a button, just compiled your example and it works. :)

spirit
10th April 2009, 08:48
btw, I tested on Qt 4.5.0, which version of Qt do you use?
PS. also read this (http://www.qtcentre.org/forum/p-solving-some-errors-post98762/postcount15.html), maybe you already know this. :)

cspp
10th April 2009, 08:51
QMake version 2.01a
Using Qt version 4.3.2 in D:\Qt\4.3.2\lib

spirit
10th April 2009, 08:55
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.

cspp
10th April 2009, 08:55
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!

cspp
10th April 2009, 08:59
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!
:crying:

spirit
10th April 2009, 09:10
yup, I don't remember the task number, but it was on the tracker. :)

cookie1909
24th April 2009, 00:42
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 :p 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.

spirit
24th April 2009, 07:19
can you show us you code?

sophister
24th April 2009, 12:05
I've gor a question.
I use the following codes to change the background color of QLabel, but it doesn't work!

QPalette pale = ui->label->palette();
pale.setColor(QPalette::Window, listColor);
ui->label->setAutoFillBackground(true);
ui->label->setText(listColor.name());
While if I use

ui->label->setPalette(QPalette(listColor));
it works.
why??

cookie1909
24th April 2009, 16:23
Here's my code:

mywindow.h (ui_form.h was generated using Qt Designer, it's huge so I won't put it here)

#ifndef MYWINDOW_H
#define MYWINDOW_H

#include <QMainWindow>
#include <QColor>
#include "ui_form.h"

class MyWindow1 : public QMainWindow
{
Q_OBJECT

public:
MyWindow1(const Ui::Form& ui);
~MyWindow1() {}

void setTimer(int ms);

public slots:
void blink() ;

private:
Ui::Form mUi;
QColor mColor;
};

#endif


mywindow.cpp


#include <QPalette>
#include <QIcon>
#include <QtCore/QTimer>
#include "mywindow.h"

MyWindow1::MyWindow1(const Ui::Form& ui) : QMainWindow()
{
mUi = ui;
mUi.setupUi(this);
mColor = Qt::red;
}

void MyWindow1::setTimer(int ms)
{
if (!mUi.pushButton_114)
printf("setTimer: invalid button\n");

QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(blink()));
timer->start(ms);
}

void MyWindow1::blink()
{
if (!mUi.pushButton_114)
{
printf("Invalid button\n");
return;
}

if (mColor == Qt::green)
mColor = Qt::red;
else
mColor = Qt::green;

// DOES NOT WORK
QPalette p = mUi.pushButton_114->palette();
p.setColor(QPalette::Button, mColor);
mUi.pushButton_114->setPalette(p);
mUi.pushButton_114->setAutoFillBackground(true);

#if 0
// THIS WORKS
QString style = mUi.pushButton_114->styleSheet();
if (style.contains("green"))
{
mUi.pushButton_114->setStyleSheet(
QString::fromUtf8("background-color: red;"));
}
else
{
mUi.pushButton_114->setStyleSheet(
QString::fromUtf8("background-color: green;"));
}
#endif
}


main.cpp

#include <QApplication>
#include "mywindow.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Ui::Form ui;
MyWindow1 *widget = new MyWindow1(ui);
widget->setTimer(500);
widget->show();
return app.exec();
}