QScrollarea Stylesheet with QWidget - c++

I have a problem with the inheritance system of the parent-child Css while I tried all possibilities with the Id Selector in the Css.
the QScrollarea contains a QWidget type model with the following hierarchy:
Qt hierarchy class
I do not understand even though I change the QWidget (modelWidget) to QFrame, it does not change anything. here is the Css code:
QScrollArea#PreviewModel{
background-color: #282828;
border: none;
}
QWidget#WidgetContents{
background-color: #282828;
}
QWidget#modelWidget{
border: 2px solid #00aaff;
}
Here is what shows me:
Result Css 1
and if I remove the css #WidgetContents it gives me here:
QScrollArea#PreviewModel{
background-color: #282828;
border: none;
}
#modelWidget{
border: 2px solid #00aaff;
}
result:
Result Css 2
the content of modelWidget still influenced by the WidgetContents
a little help please?

Related

How can I set background for a QPushButton's tooltip?

In Qt I set a background image for a QPushButton, then I added a tooltip for this button and the tooltip has the same background image as the button and I couldn't change it with stylesheet, what am I missing?
In my code I have:
button->setStyleSheet("background-image: url(pathToImage);");
button->setToolTip("Click here");
In my style.qss I have:
QToolTip{
background-image: none;
background-color: white;
font-size: 20px;
border: 1px solid black;
}
The font-size and the border works, but the tooltip's background-image is the same as the button's.
I also tried adding another background-image to the tooltip, it didn't worked either.
How can I change the tooltip's background?
You have to specify the QWidget where to apply the property. If you dont do so, it will apply it to all the childrens of the widget.
In your case, to avoid the background image in the tooltip you have to specify that you want to apply that style to a QPushButton widget. The documentation says:
If we want the property to apply only to the QLineEdits that are children (or grandchildren or grand-grandchildren) of a specific dialog, we would rather do this:
myDialog->setStyleSheet("QLineEdit { background-color: yellow }");
In the example you mention, if you want to modify the style of the tooltip and the button, do something like this:
ui->pushButton->setStyleSheet(""
"QPushButton { background-image: url(me.png); }"
"QToolTip { color: #ffffff; background-color: #000000; border: 0px; }");
It will give you something like this
Update:
If you want to apply it to a single object and not the rest of the widgets of the same type, the documentation says:
If we want the property to apply only to one specific QLineEdit, we can give it a name using QObject::setObjectName() and use an ID Selector to refer to it:
myDialog->setStyleSheet("QLineEdit#nameEdit { background-color: yellow }");
So in your case:
ui->pushButton->setObjectName("awesomeButton");
ui->pushButton->setStyleSheet("QPushButton#awesomeButton { background-image: url(me.png); }");
When you set qss with setStyleSheet your stylesheet applies for all children of object. In your case you can avoid this using stylesheet for QPushButton only
button->setStyleSheet("QPushButton {background-image: url(pathToImage);}");

How to stop child QTabWidget to inherit style sheet from parent QTabWidget

I have done below styling on QTabWidget (which has a QTabWidget inside one of it's tab):
QTabBar::tab {
border: 2px solid grey;
}
QTabBar::tab:selected {
border-color: red;
}
After this the tab widget looks like:
I do not want child QTabWidget to inherit style from parent. I know one way to achieve this is by using object name in style sheet but I don't have the object name of QTabBar associated with QTabWidget. Please let me know how I can achieve the desired behavior.
You can use object name on the QTabWidget:
parent_tab_widget->setObjectName("parent_tab_widget");
In style sheet:
#parent_tab_widget > QTabBar::tab {
border: 2px solid grey;
}
#parent_tab_widget > QTabBar::tab:selected {
border-color: red;
}
More info on style sheet selectors in Qt4 here. The answer is a combination of the ID selector with the child selector.

How does view's stylesheet interact with model's ::FontRole in Qt model/view environment?

What happens when font family and size supplied by stylesheet of QTreeView differ from the ones returned by the model assigned to it? So far it seems like model's data overrides stylesheet settings. How do I change font of a view with a custom model assigned to it then?
Or to be more precise: if I know I want to style the view with stylesheet - what do I return from model when ::FontRole is requested?
UPD: just in case I am doing something stupid, here is my stylesheet that I assign to qtreeview:
QTreeView::item:selected
{
color: black;
font-family:"Times New Roman", Times, serif;
}
QTreeView::item:has-children
{
font-family: "Comic Sans MS", cursive, sans-serif;
height: 25px;
border-bottom: 1px solid;
border-bottom-color: green ;
border-top: 1px;
}
After some asking around and googling it turned out that styling text in QTreeView should be done on QTreeView instead of QTreeView::item

QTabBar::setTabButton center button

I'm trying to customize a QTabBar button in Qt/C++ by making it show up as italic and the only way I've found is to use setTabButton and give it a custom widget, which is a QLabel in my case. However, setTabButton's position parameter only has Left or Right as options (See code below). Now it looks weird next to the other tabs which are all centered.
tab_bar->setTabButton(index, QTabBar::RightSide,((QWidget*)(tab_label)));
Note, there is a stylesheet applied to the whole application... not sure if that would cause the issue to be more apparent.
Stylesheet for that label is:
QLabel#GrainButton {
background-color: transparent;
border: 0px solid transparent;
margin: 0px;
padding: 0px;
min-width: 20px;
border: 0px solid transparent;
border-radius: 0px;
}
Any thoughts of how to center this?
You can create a custom widget instead of your QLabel. In this widget you can set a QHBoxLayout with 2 placeholders widgets.
QHBoxLayout:
PlaceHolder( sizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); )
YourLabel
PlaceHolder( sizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); )
This may place your widget in the center.
Inherit QProxyStyle and override drawItemPixmap, the pixmap will shown in the center.

CSS set QTableView background color?

I want to set the background color of all cell in a QTableView Object using css.
something along the lines of...
ui.tableView->setStyleSheet("QTableView { background-color: red; color: yellow");
Is this possible? If so how would I do it?
Change the css attribute to "background-color", and then your example looks good to go.
Reference:
http://doc.qt.nokia.com/4.7-snapshot/stylesheet-reference.html
I think the following piece of qss can do the trick:
QTableView::item {
border: 1px solid #d9d9d9;
border-top-color: transparent;
border-bottom-color: transparent;
}
Most of the qss examples for QTreeView work also for QTableView
http://qt-project.org/doc/qt-4.8/stylesheet-examples.html#customizing-qtreeview