PDA

View Full Version : QtDesigner and Stylesheets



abrou
4th September 2012, 23:54
Hello,

I am trying to apply a style sheet stored in a file to my application. In particular, I would like to add padding to my QCombo-boxes, as such:


QComboBox {
padding: 3px;
color: red;
}

Note that I have added the red for testing purposes. I have several QComboBoxes, created in Qt Designer. If I apply the style sheet to the entire application (using
qApp()->setStyleSheet(styleSheet)), the text turns red but does not add the padding. This means that I am correctly reading in the text file and applying it, but I am somehow not setting the padding property properly.

Again, I tried setting the style sheet in the source code file that sets up the *.ui file.
This works:

this->setStyleSheet("QComboBox { color: red; }");

This doesn't:

this->setStyleSheet("QComboBox { padding: 3px; }");

I can successfully pad them by adding a line of code for each:

ui->ComboBox->setStyleSheet("QComboBox { padding: 3 px; }");

I can successfully change the style sheet in Qt Designer.

However, both of the solutions above must be done for each combo box, which is tedious and (I think) unnecessary. Any help would be greatly appreciated. Thanks,

AB

edit: I am using Qt Designer with Qt Creator on a 64 bit Windows 7 machine. I am using Qt version 4.7.4 (32 bit).

viulskiez
5th September 2012, 08:06
int main(int argc , char* argv[])
{
QApplication app(argc, argv);
app.setStyleSheet("QComboBox{padding:3px;color:red;}");

MainWidget mainWidget;
mainWidget.show();

return app.exec();
}
The codes above should apply stylesheets to all QComboBox instances in the application. If not, try creating new widget with combobox within it to make sure you don't have inline stylesheet replacing the initial stylesheets apllied.

abrou
5th September 2012, 18:55
Thanks for your reply. I tested the exact code you provided with the same results. Red text but no padding. I added a new ComboBox to a new widget and it worked as expeted; it was red and padded. From what you've said, I have an inline stylesheet replacing my style sheet. As far as I can tell, I haven't added it myself, but is it possible I inadvertently changed the style sheet using Qt Designer? I played around with the style sheet in Designer, but deleted it because I wanted to have a single style sheet for my entire application.

Thanks,

AB

viulskiez
5th September 2012, 19:09
As far as I can tell, I haven't added it myself, but is it possible I inadvertently changed the style sheet using Qt Designer?
Of course it's possible. If necessary, you can modify .ui file manually with text editor (not Qt Designer) to make sure the inline style sheet have been properly removed.

abrou
5th September 2012, 19:23
Thanks. I assumed that if QT Designer didn't display anything in the StyleSheet textbox that it wasn't adding anything to the *.ui file. Bad assumption on my part. I'll delve into it now. Thanks again.

Edit: As you suggested, I had played with the style sheets in Qt Designer. I had deleted them, but they remained in the *.ui which I could delete manually. This is now solved.