PDA

View Full Version : QPushbutton + QLabel text color



Ashish
2nd February 2007, 23:07
Hi,

I want to change (1) background color for a QPushbutton (2) color of text for QPushbutton (3) background color of Qlabel (4) color of text for QLabel. I have been looking at other threads but have not been able to figure out how to best do this.
I am posting a simple helloworld example for it. Can anyone please guide me how to modify it to do the above things?
Thanks in advance.

Regards,
Ashish


#include <QApplication>
#include <QPushButton>
#include <QColorGroup>
#include <QLabel>
#include <QWidget>
#include <QHBoxLayout>
#include <QObject>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *window=new QWidget();;
QHBoxLayout *myl= new QHBoxLayout();
window->setWindowTitle("my test window");

QPushButton *hello=new QPushButton();
hello->setText("Hello world!");
hello->setFont(QFont("Comic Sans MS",30,QFont::Bold));

QPalette *pal= new QPalette(hello->palette());
//pal->setColor(QPalette(QColor(250,0,0)));
//pal->setColor(QPalette(?????-
//hello->setPalette(QPalette(Qt::red));

QLabel *mylabel=new QLabel();
mylabel->setFont(QFont("Times",20,QFont::Bold));
mylabel->setPalette(QPalette(Qt::red));
mylabel->setText("This is my Label");

myl->addWidget(hello);
myl->addWidget(mylabel);
window->setLayout(myl);

//mylabel->show();
//hello->show();
window->show();
return app.exec();
}

wysota
2nd February 2007, 23:13
Use stylesheets (http://doc.trolltech.com/latest/stylesheets.html)...

Ashish
2nd February 2007, 23:36
Thanks wysota. I am using Qt4.1 which doesn't have the stylesheet functionality.
Is there any other way I can do this?

jpn
2nd February 2007, 23:44
In Qt 4.1 it's impossible to change the background color of a QPushButton through palettes on WinXP. QWindowsXpStyle uses some native theming engine to draw controls such as a button and modifying the palette does not help. Therefore Qt 4.2 offers stylesheets...



QPalette p = label->palette();
p.setColor(Qt::Window, Qt::green); // background
p.setColor(Qt::WindowText, Qt::magenta); // foreground (text)
label->setPalette(p);
label->setAutoFillBackground(true); // child widgets are transparent by default since Qt 4.1

Ashish
3rd February 2007, 00:01
Thanks JPN. I tried this code in my example. It gives me 3 errors.
1) at
p.setColor(Qt::Window, Qt::green); // background
saying that:
error C2664: 'void QPalette::setColor(QPalette::ColorRole,const QColor &)' : cannot convert parameter 1 from 'Qt::WindowType' to 'QPalette::ColorRole'

2) at
p.setColor(Qt::WindowText, Qt::magenta); // foreground (text)
saying that:
error C2039: 'WindowText' : is not a member of 'Qt'

3) at
p.setColor(Qt::WindowText, Qt::magenta); // foreground (text)
saying that:
error C2065: 'WindowText' : undeclared identifier

What am I missing here?

Thanks,
Ashish

jpn
3rd February 2007, 09:13
Sorry, I wrote it on the fly. It should be:


p.setColor(QPalette::Window, Qt::green); // background
p.setColor(QPalette::WindowText, Qt::magenta); // foreground (text)

PS. QPalette, QPalette::ColorRole (http://doc.trolltech.com/4.2/qpalette.html#ColorRole-enum)