The way you are using qstylesheets is more for static properties. If you intend to change properties on the fly, I would look into Customizing Using Dynamic Properties.
Customizing Using Dynamic Properties
There are many situations where we need to present a form that has
mandatory fields. To indicate to the user that the field is mandatory,
one effective (albeit esthetically dubious) solution is to use yellow
as the background color for those fields. It turns out this is very
easy to implement using Qt Style Sheets. First, we would use the
following application-wide style sheet:
*[mandatoryField="true"] { background-color: yellow }
This means that every widget whose mandatoryField Qt property is set
to true would have a yellow background.
Then, for each mandatory field widget, we would simply create a
mandatoryField property on the fly and set it to true. For example:
QLineEdit *nameEdit = new QLineEdit(this);
nameEdit->setProperty("mandatoryField", true);
QLineEdit *emailEdit = new QLineEdit(this);
emailEdit->setProperty("mandatoryField", true);
QSpinBox *ageSpinBox = new QSpinBox(this);
ageSpinBox->setProperty("mandatoryField", true);
It appears to be more friendly for swapping values frequently. This is explained well for Qt 4.7, but it appears to still be available in Qt 4.3: The Style Sheet Syntax: Selector Types
So basically, instead of adding to the list of q stylesheets set for your app over and over again, you should make some of the styles dependent on a property, and set that property, then unset the stylesheet and then re-set it. I believe this can be done with the unpolish
and polish
commands (see Styles and Style Aware Widgets).
EDIT: Below is an example of using this technique. There is a mainwindow.ui with a QPushButton on it.
main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTimerEvent>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void timerEvent(QTimerEvent *);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QString stylesheet =
"*[mandatoryField="true"] { background-color: yellow }"
"*[mandatoryField="false"] { background-color: gray }"
"QPushButton[otherField="true"] { border: none; }"
"QPushButton[otherField="false"] { border-color: navy; }";
ui->pushButton->setProperty("mandatoryField", true);
ui->pushButton->setProperty("otherField", true);
ui->pushButton->setStyleSheet(stylesheet);
this->startTimer(1000);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::timerEvent(QTimerEvent *)
{
static int count = 0;
if(count % 2)
{
bool previousMandatoryFieldValue = ui->pushButton->property("mandatoryField").toBool();
ui->pushButton->setProperty("mandatoryField", !previousMandatoryFieldValue);
qDebug() << "mf" << previousMandatoryFieldValue;
}
else
{
bool previousOtherFieldValue = ui->pushButton->property("otherField").toBool();
ui->pushButton->setProperty("otherField", !previousOtherFieldValue);
qDebug() << "of" << previousOtherFieldValue;
}
ui->pushButton->style()->unpolish(ui->pushButton);
ui->pushButton->style()->polish(ui->pushButton);
this->update();
count++;
}