Change descendant QLabel properties on parent pseudo-state - qt

I've read the documentation thoroughly and searched the familiar domains like google and stackoverflow for quite some time now. Unfortunately all this without any solutions to be found. So I'm hoping that someone out here might be my savior..
The Situation:
What I am using: PyQt4, Python 2.6, Windows 7
I'm trying to style a QLabel (using a CSS file which I import in my program). The QLabel is in a QListWidgetItem.
So the structure I have is as follows:
QListWidget
+QListWidgetItem
+QLabel
So according to the documentation the way to access the QLabel would be as follows. No problem here.
QListWidget::item QLabel
{
background-color:#000
}
The Problem:
However, I would like the QLabel styling to ONLY change when I hover the QListWidgetItem. Using the following code, the pseudo-state just gets ignored for some reason. The background-color thus gets applied to the QLabels, but it isn't respecting the pseudo state.
QListWidget::item:hover QLabel
{
background-color:#000
}
This situation persists when I run my application stand-alone.
What isn't the solution:
Setting the QLabel pseudo-state to hover won't work because the QListWidgetItem is taller than the QLabel.
Main Question:
Is there a way to change the style properties of the QLabel when, and only when, the QListWidgetItem is hovered and how can this be achieved?
The goal here is to do it ONLY through the css (or qss if you prefer), which is a separate file that gets imported into the program.
Sources:
qss documentation: http://doc.qt.digia.com/4.6/stylesheet-syntax.html

You can't do this at least in Qt 4.7 (did not tested Qt5)
here is good explanation: How to set color of child QLabels while hover parent QFrame with QSS?
it seems that Qt css does not support pseudo-states for descendant elements but original CSS does:
http://codepen.io/anon/pen/olwHs
You should write some code to support such style. Use Enter/LeaveEvent to handle hover and then set style in your code via setStyleSheet()
You may also use dynamic property for that to leave styles in .ui
QLabel[hover="true"] {}
and then set and reset this value in code
Also you may use dynamic property for main widget
[hover="true"] QLabel {}

Related

Making QPushButton background constant

I use C++/Qt 5.12, Windows 7 OS, Visual Studio 2017.
I'd like checkable QPushButton background to ignore checked/pressed state. I'd like to have a default background in a QPushButton instance, but only font color should be changed if the user checked the button. How can I achieve this effect?
You can use QSS (CSS with Qt's flavor) to customize QWidgets:
https://doc.qt.io/qt-5/stylesheet-syntax.html
I would recommend creating an application-wide QSS that you load at startup, and use QApplication::setStyleSheet(...). But you can use Qt Designer (right click on a specific control), or plain C++
myButton->setStyleSheet("QPushButton { background: yellow; }");
You may need to redefine border to have it visually applied, and then margins to have a correct button size, but it is fairly easy. Try experimenting from Qt Designer.
You can find a comprehensive reference of all selectors and attributes available here: https://doc.qt.io/qt-5/stylesheet-reference.html

Why can't I set the color of a PushButton?

I've just started my adventure with Qt. After installation of QtCreator 3.6.0, the project compiled without any problems, but when I try to change PushButton colour through GUI (palette), nothing happens.
Similarly, when I substitute my own class for just added to work place (containers) widget, there's no file to swap in options. Where's the problem?
You can use CSS styles. Add the following CSS style to styleSheet of your button.
QPushButton{
background-color:yellow
}
Click on the styleSheet property button (...). Then select a menu item "Add gradient/background-color". You can try default cover or create another one.
More detailed info here http://doc.qt.io/qt-4.8/style-reference.html
#Domino Jachas, Warning: Some styles do not use the palette for all drawing, for instance, if they make use of native theme engines, according to the Qt Docs.
QPalette palette = ui->pushButton->palette();
palette.setColor(QPalette::ButtonText, Qt::red); // It's ok
palette.setColor(QPalette::Button, Qt::yellow); // but, not ok. Use theme.
ui->pushButton->setPalette(palette);

Unable to set "style property" in GTK3

I am trying to set widgets' style properties (coding with C on windows) but it doesn't work:
I tried putting in in the css file
* {
-GtkButton-child-displacement-y: 20;
}
and it didn't work, I also tried setting it inside the xml file adding this property to a GtkButton
<property name='child-displacement-y'>20</property>
an it doesn't work (invalide property) I suppose style properties can't be set along with properties
So, what am I doing wrong? why the css file ignores widget specific style properties ?
(GTK+ version 3.18 on windows)
GtkButton::child-displacement-y is now ignored as of GTK+ 3.20. Your CSS won't work anymore anyway. It may have already been broken in 3.18. Sorry. :/ The documentation suggests using CSS margins and padding instead.
That being said, otherwise your CSS is correct. I presume you simply are not loading it correctly. You can't just drop a random .css file and expect it to work; you need to explicitly load it using GtkCssProvider into a GtkStyleContext (either one that belongs to a widget or to the default GdkScreen to have it apply to all widgets). (If you are instead modifying the theme file directly, then see the first paragraph.)

How do you exclude all QToolTips from the Qt global stylesheet selector?

I have a global stylesheet selector (the '*' block) which changes font properties for all Qt widgets in the application. However, that also affects QToolTip - not desired. How do you exclude QToolTip from being affected by the global selector? I couldn't find a stylesheet operator for exclusion.
I can of course not use the global selector and copy the shared styles for every possible widget type. Obviously this is cumbersome, not scalable, clutters the QSS and may not provide full coverage. I'm looking for a better solution.
Add this line:
QToolTip{}
Should reset the QToolTip style to default.
QToolTip uses font from widget. You apply another font to widget so this font applies also to tooltip. To avoid this try to use html tooltip:
ui->lineEdit->setToolTip("<font face =\"aharoni\">test</font>");
ui->lineEdit->setText("test");
Result with aharoni font (for better visual comparison):
with *{font-family:consolas} stylesheet.

QTreeView stylesheet customization problem with scrollbar

There are two QTreeViews in the screenshot below. For the one on the right, I've add a css customization:
setStyleSheet(
"background-color: #EAF5FF;"
"alternate-background-color: #D5EAFF;"
);
Notice however, the scrollbar appearance has changed. It went to the "windows" style, not the "windowsxp" style (which is the default, since I'm developing under Xp).
How can I use the above style settings without changing the scrollbar appearance?
I'm using QT 4.5 and Windows XP.
Do you need one of the abilities the css customization gives you, beyond what you can do directly? For changing the colors, you can do that directly with the palette of the widget, which should preserve your style.
Also, remember that style changes are inherited, so if any widget containing the tree on the right has a different style than any one containing the tree on the left, that may cause the changes as well.
Beyond that, I would think that this appears to be a Qt bug, if indeed the only difference is the css style sheet.
The advice of going the QPalette route worked out. Here is the solution:
#if 0 // this causes the problem
setStyleSheet(
"background-color: #EAF5FF;"
"alternate-background-color: #D5EAFF;"
);
#else // this works correctly
QPalette p = palette();
p.setColor(QPalette::Base, QColor(qRgb(0xEA, 0xF5, 0xFF)));
p.setColor(QPalette::AlternateBase, QColor(qRgb(0xD5, 0xEA, 0xFF)));
setPalette(p);
#endif
Both methods should work according to the docs, so I'd say its a Qt bug.
Edit: After working with this new method over the past few days, I've noticed there may be a performance improvement also.

Resources