PDA

View Full Version : Problems with simple Qt Style Sheet



Qn00b
14th October 2010, 19:31
Hello

I have a very simple problem with the setStyleSheet() function. I need to apply a simple style to a label.
If I make my form only with code (without using Qt Designer) I can't apply my style. An example:


Form::Form(QWidget *parent) : QWidget(parent) {
this->myLabel = new QLabel(this);
this->myLabel->move(0,0);
this->myLabel->resize(300, 100);
this->setStyleSheet("#myLabel{background-color: rgb(0, 0, 255);}");
}


It doesn't work, but if I make my form with Qt Designer it works:



Form::Form(QWidget *parent) : QWidget(parent), ui(new Ui::ClientFrame) {
ui->setupUi(this);
this->setStyleSheet("#label {background-color: rgb(0, 0, 255);}");
}


Someone can help me to fix the first code please??

Thanks!!

Alnitak
14th October 2010, 19:47
the problem is that there is no objects named "myLabel" yet.

add this
this->myLabel->setObjectName(QString::fromUtf8("myLabel"));

Qn00b
14th October 2010, 22:26
Thank you very much for your help, I have solved my problem :D

Lykurg
14th October 2010, 22:33
and since you set the style sheet only to your label, you don't have to specify any scope and could simply write
this->setStyleSheet("background-color: rgb(0, 0, 255)");