I created a button in Qt and gave it the QSS attribute background-color: gray;, while my external stylesheet has set the QSS attribute of the same button to background-color: blue;. When I run the application the button is gray, even though the style sheet is applied after the QWidget::show() is called and just before QApplication::exec(), as shown below:
MyWidget w;
w.show();
...
app.setStyleSheet("..."); // contents of external stylesheet
return app.exec();
Is it possible to have QApplication::setStyleSheet() override the QSS attributes assigned to a Widget in Qt.
No, it is not possible to override the QSS attributes the way you want, and trust me, you don't want to. It is not the order in which you call setStyleSheet that matters. It is the hierarchy that matters first. The call order matters only on widgets which are situated on the same level of the hierarchy.
The reason is that the widget has its internal style rules defined which override the parent's style thus the application style in your case. It is a hierarchy that is respected. You can look at this in the following way:
Say you have a QWidget with the following child hierarchy:
QWidget
|__QPushButton
|
|__QFrame
| |
| |_QListView
|
|__QProgressBar
Let's say you want to customize the background-color to all the widgets in your hierarchy. If the call to QApplication::setStyleSheet() would overwrite the stylesheet properties for the children, it would be impossible for you to set a custom style for your children. That's why child widget's QSS properties overwrite parent widget's QSS properties.
Look at it like the usual way to look at widgets. QPushButton is shown on top of QWidget. QFrame is shown on top of QWidget. QListView is also shown on top of QWidget. Styles apply the same way.
What I recommend doing is having only one external QSS file in which you define everything you want.
EDIT:
As N1ghtLight pointed out QSS preserves the class inheritance hierarchy so if you set a property for a class all its derived classes will inherit that property. For example if you have the following stylesheet:
QAbstractButton {
background-color: red;
}
QPushButton {
color: blue;
}
All QPushButtons will have the background color red and the text color blue as the QPushButton inherits the background-color property value from QAbstractButton which is its ancestor while QAbstractButtons which are not QPushButtons will have the background color red but the text color will remain unchanged.
The example above used a type selector. You can apply the style to specific objects by using different selector types. You can see different selector types here.
Related
I'm trying to change skin of a program that supports Qt Style CSS, but I don't have access to the source code. I need to change the background color of the groups inside a QWidget,
I tried with:
QWidget {
background-color: #4d4d4d;
}
But it changes the color of the whole window, but I can still see the rectangle semi-transparent (like black 95% opacity). What class do I need to edit to change that particular box?
You are changing the style for the class QWidget which is the base for any Qt widget.
If you want to change the style for the group only, that's the QGroupBox class.
QGroupBox{
background-color: #4d4d4d;
}
Some QSS examples in the doc : https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qgroupbox
If you want to set the style on a specific widget of the application you will need more information from the code.
I have a program that can load plugin modules and these plugin modules I don't have much control over (outside of instructing customers about guidelines on how to create their modules).
I'm, however, trying to create some Style Themes (e.g. 'lightTheme' and 'darkTheme') for my application that I'd also like to propagate to any UI elements that may be created in any plugins.
Here's my problem, in one of the plugins I noticed that a bare QWidget (e.g. QWidget *widget = new QWidget(); was created that has no parent and I'm trying to figure out how to style this window.
Qt in this case treats this view kind of like a QDialog, however in the stylesheet I need to use QWidget to style this view (QDialog doesn't do anything). As you might expect adding style to QWidget (e.g. QWidget { background-color: black; } with cause a whole host of other style changes throughout my program that I don't want.
So what I'm looking for is how to "style a QWidget that doesn't have a parent".
I was expecting to do something like:
parent > QWidget { background-color: black; }
But I can't figure out what to put for the 'parent' since I know this widget has no parent.
Any help would be appreciated.
I have an editable combo box on the input form, which background must change when it receives focus. The following code works perfect for QLineEdit but has no effect on QComboBox.
QLineEdit, QComboBox { background-color: green; }
QLineEdit:focus, QComboBox:focus { background-color: red; }
Is it possible to make QComboBox behaves as expected like QLineEdit using only Qt style sheets?
You may need to do this by subclassing QLineEdit, and installing it into the combo box (with QComboBox::setLineEdit()). Then, override the focusIn() and focusOut() functions of QLineEdit, and set a style sheet with the appropriate background color in those functions.
Another way would be to install an event handler on the combo box, (and/or its associated QLineEdit) and trap focus in/out events, and change the style sheet then.
When adding a QComboBox control in Qt Designer, I get a terrible looking, non-native control:
On digging further, it turns out that two of the parent controls, QParentWindow and QStackedWidget, have style sheets that QComboBox is inheriting. If I delete the custom styles, then I get a native QComboBox like the one on the left.
How can I have QComboBox (and widgets generally) NOT inherit parent styles? Or, how can I create a style for, say, QParentWindow, and do it so that it's local only and does not cascade?
I don't think you can prevent it from cascading. But by using more specific selectors in your stylesheet, maybe you could define properties only for your QParentWindow class or specific object.
you need to define a style and then assign it to that object:
QString settingStyle = " QGroupBox#groupBoxSettings {\
background-color: rgb(248,248,248);\
border: 1px solid rgb(170, 170, 255);\
border-radius: 3px;\
border-color:rgb(170, 170, 255);\
}";
ui->groupBoxSettings->setStyleSheet(settingStyle);
here "groupBoxSettings" is the object name. This way any thing inside the groupbox they'll have their own style.
I have a class that inherits QStandardItem and I put the elements in a QTreeWidget. The class receives notifications from the outside and I want to change the background color of the item based on what happened.
If I do not use stylesheets, it works just fine, like this:
void myClass::onExternalEvent()
{
setBackground(0, QColor(255,0,0)));
}
However, as soon as I put a stylesheet on the QTreeWidget, this has no effect : the stylesheet seems to override the setBackground() call.
So I tried :
void myClass::onExternalEvent()
{
this->setStyleSheet("background-color: red");
}
but this is probably all wrong, it changed the color of some other element on my screen, not sure why.
Does anyone have an idea on how I can alter the background color like with setBackgroundColor but still be able to use stylesheet on my QTreeWidget?
Palettes propagate to the children of a widget, and it's bad to mix and match style-sheet controls and native controls (I do not have a citation for the latter handy, but I have read it in the QT docs somewhere).
That being said, try setting setAutoFillBackground(false) on your QStandardItem derived class.
EDIT: Sorry - also, are you specifying the QTreeWidget in the stylesheet or just setting "background-color:"? If you specify the QTreeWidget only in the stylesheet that might take care of it as well.
QTreeWidget { background-color: white; }
But I think you still have to set the autoFillBackground(false).