[Solved]Load qss as global.
-
Is the below the correct way to load a style sheet globally? I am trying to make things a bit more efficient than:
@middleIntText -> setStyleSheet("QLineEdit { border: 1px solid gray;border-radius: 5px;padding: 0 8px;selection-background-color:darkgray;height:40px;font-size:15px;}");@I thought the following would work at loading QLineEdit the one time for all QLineEdit widgets:
qss file:
@QLineEdit { border: 1px solid gray;
border-radius: 5px;
padding: 0 8px;
selection-background-color:darkgray;
height:40px;
font-size:15px;}@cpp file:
@QApplication a(argc, argv);
QFile stylesheet("formStyle.qss");
stylesheet.open(QFile::ReadOnly);
QString setSheet = QLatin1String(stylesheet.readAll());
a.setStyleSheet(setSheet);@Perhaps this is right and I am doing something else wrong?
-
Don't know if that would help, but you can try setting the style sheet on your topmost widget (usually a QMainWindow) with ::setStyleSheet(). Settings are then propagated down the meta object hierarchy.
-
That makes great sense. Need to get some sleep I think cause everything I tried on your suggestion was a no go though. I tried applying in the cardUI class and to QMainWindow and to it in main with cardUI widget.
Like:
@QMainWindow::setStyleSheet(setSheet);@
and
@cardUI::setStyleSheet(setSheet);@ -
You have to set it on an existing object, not a class itself, but you probably know that. Yeah, get some sleep, that usually helps in problem solving :) Your code looks good, I don't know why it's not working. Maybe check your QSS file's encoding (with/ without BOM, etc.)?
-
Ah, one thing. Be sure to check if the QFile::open() was successful (it returns a handy bool for that). Maybe it's just that the file is not where you point to :)
-
[quote author="weblife" date="1343372467"]That makes great sense. Need to get some sleep I think cause everything I tried on your suggestion was a no go though. I tried applying in the cardUI class and to QMainWindow and to it in main with cardUI widget.
[/quote]Applying global stylesheets to QApplication instance is fine but make sure that your qss is correct. Here is a working example source that I had to use recently for my project following the official "Qt Style Sheets examples":http://qt-project.org/doc/qt-4.8/stylesheet-examples.html#customizing-qcheckbox :
@
QString sCheckBoxStyle = "QCheckBox::indicator { width: 30px; height: 30px; }";
sCheckBoxStyle += " QCheckBox::indicator:unchecked { image: url(:/images/checkbox_unchecked.png); }";
sCheckBoxStyle += "QCheckBox::indicator:unchecked:hover { image: url(:/images/checkbox_unchecked.png); }";
sCheckBoxStyle += "QCheckBox::indicator:unchecked:pressed { image: url(:/images/checkbox_unchecked.png); }";
sCheckBoxStyle += " QCheckBox::indicator:checked { image: url(:/images/checkbox_checked.png); }";
sCheckBoxStyle += "QCheckBox::indicator:checked:hover { image: url(:/images/checkbox_checked.png); }";
sCheckBoxStyle += "QCheckBox::indicator:checked:pressed { image: url(:/images/checkbox_checked.png); }";
sCheckBoxStyle += "QCheckBox::indicator:indeterminate:hover { image: url(:/images/checkbox_checked.png); }";
sCheckBoxStyle += "QCheckBox::indicator:indeterminate:pressed { image: url(:/images/checkbox_checked.png); }";app->setStyleSheet(sCheckBoxStyle);
@
-
[quote author="weblife" date="1343398537"]It was the target object. Told you I was tired. In my UI class was centralized on QWidget.
@QWidget::setStyleSheet(setSheet);@[/quote]
I am happy that you have found and fixed your issue. Well done :)