Styling QListWidget item widgets: selected state - c++

I have a window which displays tiles, each tile having some set of information. Tiles are arranged in a tabular structure. The way in which this is implemented is, a QListWidget is used to hold tiles and each tile is set as the item widget in QListWidgetItems in QListWidget.
I have styled the tiles using a stylesheet. My problem is, I cannot get a tile highlighted in some way when the tile is selected. If I do not use stylesheets at all, default selected highlighting works. But as soon as I apply styles to tiles, there is no difference in the tile in non selected and selected states.
I tried to do it in following way but it does not work.
.tile
{
/*non selected style*/
}
.tileList::item:selected
.tile
{
/*selected style*/
}
Any idea how I can achieve this?

I solved it in Qt Designer by setting the palette how I wanted it and then put
QListView::item:selected { background: palette(Highlight) }
as the styleSheet. Maybe this helps somebody.
If you want to do it from a central qss, I guess you'll have to remove the ".tile" part from the code in the question.
.tileList::item:selected
.tile <--- remove this line
{
/*selected style*/
}

I could get this done to some extent (not a comprehensive solution), by doing following.
Make tile widget semi transparent.
Set a background color to QListWidgetItem
Set a different background color to QListWidgetItem when selected
Styles:
.titleList::item {
background-color: #fff;
}
.lstSnapQuote::item:selected {
background-color: #5555FF;
}

Related

Qt/C++ - Set background of disabled button?

I have all my buttons disabled in a grid, but for some, I'd like to change the background color. I'm trying:
_fieldButtons[0][0]->setStyleSheet("color: blue; background-color: blue;");
Where
QVector<QVector<QPushButton*> > _fieldButtons;
However, these buttons are all disabled, and only the text color gets changed:
How can I change the background, but not the text? Thank you!
UPDATE
I figured it's not working because the buttons are flat. How can I change flat button colors?
Two options:
Add border: none; to your stylesheet.
Use setAutoFillBackground(true) in conjunction with QPalette::Button
myButton->setFlat(true);
myButton->setAutoFillBackground(true);
QPalette pal = myButton->palette();
pal.setColor(QPalette::Button, QColor(Qt::blue));
myButton->setPalette(pal);
http://doc.qt.io/qt-5/qpushbutton.html#flat-prop
Try this:
myButton->setPalette(QColor("#124e6b"));
simply change the QColor to suit your use.
Or in Qt Creator you can right-click on the widget and select Change Style Sheet, as shown here:
Here you have two "Pseudo-States" in your control. For list of "Pseudo-States" refer below link.
http://doc.qt.io/qt-5/stylesheet-reference.html#list-of-pseudo-states
The first "Pseudo-State" is -- flat
The second "Pseudo-State" is -- disabled
Here you have to club both the states to set the style using "setStyleSheet".
_fieldButtons[0][0]->setStyleSheet(":flat:disabled {background-color: blue; border: none;}");
look for ":hover:enabled" (two different "Pseudo-States" how documentation handled) in the below link to get better idea.
http://doc.qt.io/qt-4.8/stylesheet-syntax.html
To understand, why you we have to give border:none for QPushButton, please look for below information in the first hyperlink in this answer.
"Warning: If you only set a background-color on a QPushButton, the background may not appear unless you set the border property to some value. This is because, by default, the QPushButton draws a native border which completely overlaps the background-color."

QDockWidget::background-color not applied when docked

I have a QDockWidget:
I would like to alert the user to certain events by setting the background color of the title bar.
I have achieved this by setting the style sheet for my DockWidget:
void DockWidget::setCriticalAlert()
{
setStyleSheet("QDockWidget { background-color:red; }");
}
The result is this:
The problem is that the background-color doesn't get applied when the QDockWidget is docked:
How can I get the background color to be applied when the QDockWidget is docked?
This is a bug in Qt.
Issue 10537
Quoting from the linked issue:
The problem is that in QDockWidget::paintEvent, there is a
isFloating() condition before drawing PE_FrameDockWidget. We cannot
jsut remove this condition as it would break the other style (that
does not whish to draw frame when the dockwidget is docked) We cannot
either use PE_Widget to draw the frame as then it goes over the
dockwidget's title The solution is maybe to introduce a new
PE_FrameDockWidgetDocked primitive element. Or some
SH_DockWidget_DrawDockedFrame stylehint to draw the frame in every
cases.
a valid workaround seems to be to set the stylesheet of the parent, and use the class-and-id selector. Forgive the python formatted code but the concept is the same - in this case, 'dock' is a QDockWidget which has been given an object name using setObjectName(), and its parent, the QMainWindow, is 'self':
self.setStyleSheet("QDockWidget#"+str(dock.objectName())+"::title {background-color:red}")
In PyQt5.5, this works at runtime, i.e., can be changed on the fly.
I find a solution like this:
Firstly put a frame behind all the widgets of dockwidget's center widget, as the background.
Then set stylesheet for the frame.
By this way, we could change the background color of dockwidget.
Or you can extend the dockwidget and overwrite the function
void QDockWidget::setWidget(QWidget *widget)
using private/qdockwidget_h. and add a frame as this widget's father.

Item is selected but isn't highlighted

I work with Qt/C++, and I have a QListView to display icons on screen.
I set the QListView::iconMode to display it as icon view. But I cannot see that it is selected(but it is selected) it doesn't highlights. However it works for list mode.
I have this.
listView->setSelectionMode(QListView::SingleSelection);
listView->setSelectionBehavior(QListView::SelectRows);
listView->setFlow(QListView::LeftToRight);
listView->setViewMode(QListView::IconMode);
listView->setWrapping(true);
can you help me?
Documentation of selection rectangle:
This property holds if the selection rectangle should be visible.
If this property is true then the selection rectangle is
visible; otherwise it will be hidden. Note: The selection
rectangle will only be visible if the selection mode is in a mode
where more than one item can be selected; i.e., it will not draw a
selection rectangle if the selection mode is
QAbstractItemView::SingleSelection. By default, this property
is false.
You have to try either:
Manually set the property to true and see if it changes
Drop the single selection mode. It is compulsory? Does QAbstractItemView::ContiguousSelection suits your needs?
I'm archeologist :D
Worked solution:
listView->setStyleSheet(" QListView::item:selected { border: 2px solid red; }");
You can use your own border.

Set a StyleSheet for a whole widget in Qt

I have a custom widget which inherits from QWidget and contains some labels in its layout. I would like to change the background color of the widget and the labels in the widget (this is, everything!) every time I put the mouse over it.
When using *:hover { background: red; } in my custom widget, I only get the contents red when moving the mouse over the labels, but not outside them, between labels, etc. I don't understand this behavior taking into account that I put the StyleSheet in the parent widget.
Any ideas? Many thanks,
You can set the parent's stylesheet which will cascade to children like this:
parent->setStyleSheet("* {background: red}");
For hovering only:
parent->setStyleSheet("*:hover {background: red}");
Check out https://qt-project.org/doc/qt-5.1/qtwidgets/stylesheet-syntax.html
Finally I solved the problem creating a QFrame inside the main QWidget and setting the StyleSheet of that QFrame.

Problem in stylesheet of Qt APP

In my app, i have a section that is top widget, the color of the top widget is gray, and i've been put severl widget on top widget, like QComboBox, QLineEdit and 2 QButton, but i have a problem when i right click on QLineEdit as you seen in below picture, the color of default context of window is gray, or when i open the QComboBox the color of background is gray. I'll set the background color of two these widget to white but doesn't work. So, how can i fix this?
Sample for better understand:
http://0000.4.img98.net/out.php/i52512_problem.png
Please help me
The style sheet propagates to all the child widgets, so you have to limit their range by using the right selectors. Since the context menu is a child of the QLineEdit it is also affected.
// What you have probably done:
myLineEdit->setStyleSheet("background-color: gray");
// What you should have done:
myLineEdit->setStyleSheet("QLineEdit { background-color: gray }");
// What you should do if there might be child widgets of the same type
// but for which you don't want the style to apply:
myLineEdit->setObjectName("myLineEdit");
myLineEdit->setStyleSheet("QLineEdit#myLineEdit { background-color: gray }");
See "The Style Sheet Syntax - Selector Types" for details.