I'm trying to programmatically make a QToolButton highlighted when it gains focus. I would like it to be highlighted exactly like when it is selected by mouse hover. Is this possible?
Thank you!
Not sure how familiar you are with stylesheets but this one ought to get you started:
QPushButton:hover { background-color: rgb(224, 255, 0); }
QPushButton:focus { background-color: green; }
Related
Whenever a widget changes it's state, it is supposed to change its style. I have successfully implemented the signalizing, but I can't figure out how to change a combobox' style really.
The function that changes a widget's style:
{
static Glib::RefPtr<Gtk::CssProvider> css{nullptr};
if(!css) {
css = Gtk::CssProvider::create();
css->load_from_data(
"button {background-color: #fff9ed;}\
button:hover {background-color: #fff9ed;}\
button:active {background-color: #fff9ed;}\
//changing background-color: to color: does not make any difference
combobox {color: #fg_color;background-color: #fff9ed;}\
.entry {color: #fff9ed;}\
checkbutton {background-color: #fff9ed;}\
checkbutton:checked {background-color: #fff9ed;}");
}
return css;
}
This results in making a really tiny frame over the combobox. Here is the output:
Before:
After:
One can clearly see, that the background changes (see the arrowed, orange tips on the corners). But that's not useful, as the actual box covers 99% of the background. I haven't figured out how to change styles of the atual combobox button, which would solve my problem as well.
I found a solution for my problem.
I assume 'padding' for comboboxes were at 0px, so i added 'padding: 2px;' in the css string.
Background:
I have a Qt application consisting of multiple dialogs and windows. I am trying to condense all styles into a single stylesheet which I want to load in my main.cpp with the aim of it being applied to my whole application (as I understand one should do). The application should not change styles from this point onwards.
I want to do this as styles can get mixed up, etc if one does this or something simliar for each widget in the QtCreator designer.
Problem:
I will explain a specific problem I face with an example from my application.
I have a couple of variations of simple dialogs, QLabel title, QLabel message (sometimes a QLabel hint), and either a single 'ok' QPushButton or 2 QPushButtons 'positive' & 'negative'.
For my theme, I would like to set styles for specific buttons, labels, etc which I got from here.
Example CSS:
QPushButton {
font-size: 10pt
color: #111
}
QPushButton[objectName="btnPostive"][objectName="btnProceed"] {
font-size: 1pt
color: #ccc
}
QLabel {
font-size: 9pt
color: #111
}
QLabel[objectName="lblTitle"] {
font-size: 9pt
color: #111
}
Please note, the [objectName="btnPostive"][objectName="btnProceed"] is intended to apply the style to buttons with the objectName's btnPostive and btnProceed however, it does not.
Is what I intend doing considered best practice, and a sub question (preferred example too) applying the same style to a select group of widgets, how should one best this.
If you want to control the applied stylesheet based on the object name, you can use the ID Selector QPushButton#objectName, see the full documentation. You separate multiple objects with the same stylesheet with a ,
Your stylesheet should then be this. Please note that you also need a ; at the end of each line.
QPushButton {
font-size: 10pt;
color: #111;
}
QPushButton#btnPositive, QPushButton#btnProceed {
font-size: 1pt;
color: #ccc;
}
QLabel {
font-size: 9pt;
color: #111;
}
QLabel#lblTitle {
font-size: 9pt;
color: #00f;
}
When I select items in QTableWidget, it looks like this:
But when I focus other widget, it looks like this:
I want the items to remain blue as long as they're selected. I tried to enforce it using QSS (CSS):
QListWidget::item:selected:active, QListWidget::item:selected:!active {
background: blue;
}
QListWidget::item:selected {
background: blue;
}
Didn't help. What can I do to prevent selected items from becoming gray?
Turns out that background: is not the correct property to change selection background. This is correct:
QTableView::item:selected:!active
{
/*selection-background-color: #3399ff;*/
selection-background-color: #93CAFF;
/** doesnt work **/
color: white;
}
The text color setting still doesn't work, but it's better than nothing.
How to disable changing menuItems look in QMenuBar when window loses focus?
Now, when window has focus, menu items are clearly visible, but when it loses focus, items are gray, looks like disabled. I want them to look normal all the time.
My platform is Qt4 on Windows7.
Some simple screenshot of menu item on active and inactive window:
Use QStylesheets and leverage the states of your QMenuItems.
http://www.qtcentre.org/threads/37560-QPushButton-different-stylesheets-for-focus-pressed-released-combinations
QPushButton{ background-color: blue; }
QPushButton:disabled{ background-color: yellow; }
QPushButton:pressed{ background-color: orange; }
QPushButton:focus:pressed{ background-color: black; }
QPushButton:focus{ background-color: green; }
QPushButton:hover{ background-color: red; }
QPushButton:checked{ background-color: pink; }
http://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qmenubar
The other option if you want to ignore stylesheets, you could try the palette.
http://doc.qt.io/qt-5/qpalette.html#details
The color groups:
The Active group is used for the window that has keyboard focus.
The Inactive group is used for other windows.
The Disabled group is used for widgets (not windows) that are disabled for some reason.
So you should be able to get the copy of the palette for your QMenuItem, copy the active palette into the inactive palette, and then call setPalette on your QMenuItem. Tada, now it always looks active.
Hope that helps.
I have a Qt QMenu in my app, consisting of two levels (the top level of submenus, then each submenu containing actions), and we have a custom dark-grey style whereby the menu background is grey, the text color is white, and the right arrow is white. When a submenu item is highlighted (mouse-over), the item background is white, the text is black, and I want the right arrow to switch to a black image as well. I'm using a CSS stylesheet to do this. However, I can't seem to find the right syntax to set an alternate right-arrow image for the item selected state. My CSS looks like this:
QMenu
{
background-color: rgb( 24, 24, 24 );
color: white;
}
QMenu::item:selected
{
background-color: white;
color: black;
}
QMenu::right-arrow
{
image: url(Resources/MenuRight.png);
}
I've tried the following additions after the above code (where MenuRightSelected.png is an inverted-color image of MenuRight.png):
QMenu::right-arrow:selected
{
image: url(Resources/MenuRightSelected.png);
}
and
QMenu::item::right-arrow:selected
{
image: url(Resources/MenuRightSelected.png);
}
Neither of these affect the displayed QMenu. Does anyone know if it is possible to do what I'm after, and if so, how?
Possibly you only have to change the order of the items in your css file. I once heard that Qt parses the css files from upwards. Items further up overwrite the behaviour of items further down.