Making QPushButton background constant - qt

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

Related

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);

QwtPlot not inhereting stylesheet in QtCreator

I'm using a QwtPlot to draw some data in a fairly heavily styled application. I'm using QtCreator as an IDE, and load one stylesheet for the whole program at the start to keep a consistent look and feel across the whole thing using
qApp->setStyleSheet(style);
All the built in controls obey the style I set here, except for my QwtPlot. Currently all I'm doing in the style sheet is trying to set the foreground colour to white with
QwtPlot
{
color: white;
}
But it doesn't work. However, if I specifically chose "set style sheet" on the widget the designer in QT creator, and just put
color: white;
it works.
It seems to me as if the plot isn't inhereting the top level style sheet, or for some reason my naming/syntax is wrong for selecting the plot (though the few references I've found to styling a QwtPlot online do use the same selector as me).
Can anyone help?

Change descendant QLabel properties on parent pseudo-state

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 {}

Changing focusrectangle color in flex

I have a flex app, in which I need to change the color of the focus rectangle of a textinput if the input is empty. This used to work when I was writing inside a mx:script tag, but now I'm writing a new component (an AS3 class that inherits from VBox) myself. And now it isn't working anymore. I used to run the following statement to change color:
txtName.setStyle("themeColor", "#ff0000");
txtName.focusManager.getFocus().drawFocus(true);
Is it something related to the mxml? How can I fix that?
Which SDK version are you using? My guess is that you are using 4.x since this is no longer working for you. In versions prior to 4.0, the focus color was derived from the "themeColor". Not so in 4.0+, which now uses "focusColor" for the focus color (naturally).

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