Prevent selected items in unfocused table widget from becoming gray - c++

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.

Related

How apply different stylesheet to a QComboBox and its placeholder?

I want to distinguish the placeholder text of a QComboBox using QStyleSheet by drawing the placeholder with a different color.
With Qt6, it's easy to set the placeholder text to a combo box from code if the combo box is editable:
if(someCondition)
myComboBox->lineEdit()->setPlaceholder("Some placeholder");
else
myComboBox->lineEdit()->setPlaceholder("Some other placeholder");
So far so good, but if I use a custom stylesheet, so the default grayish placeholder is gone, and it is drawn with the color property. I tried to filter by some property based on this question, but I was not successful.
This is the relevant part of the dark-theme stylesheet:
auto styleSheet = "QWidget {color: white; background-color: #505050}"
"QComboBox[text=\"\"] { color: #808080 }";
myComboBox->setStyleSheet(styleSheet);
Currently, this is the result, with white letters:
And this is the expected with a sightly gray color:
Also, I tried to filter to the QComboBox, to the QComboBox[currentText=\"\"] but no success.
To draw the placeholder with darker pen, you have to modify the stylesheet and set to the lineEdit of the QComboBox.
auto styleSheet = "QWidget {color: white; background-color: #505050}"
"QLineEdit[text=\"\"] { color: #808080 }";
myComboBox->setStyleSheet(styleSheet);
myComboBox->lineEdit()->setStyleSheet(styleSheet);
QComboBox has no option to handle the placeholder text, because that text is handled by the line edit it has. Therefore, to alter the look and feel of the placeholder, you must use it's QLineEdit. Note, that several properties not make any effect on the line edit, like the background-color or border to say some, because those are handled by the QComboBox.
Also, if it's not automatically updated, you need to connect the change of the text with the stylesheet update. In the owning widget, connect the text change signal to the update:
connect(ui->myComboBox->lineEdit(), &QLineEdit::textChanged, this
[&]{ style()->polish(ui->myComboBox->lineEdit()); });

How to change Qmenubar item hover effect color in qt?

I have a QMenuBar in window, and background color is white, so when action is selected, or mouse is point at action, text color becomes white.
How can i change hover effect color? I tried to change from pallete by changing selected text color, but that doesn't worked
QMenuBar::setStyleSheet()
will do the trick.
You can fully customize the layout of your component. Be aware of using setStyleSheet means you'll completely override the style of the component with your stylesheet.
QString style = "QMenuBar::item:selected { background: white; } QMenuBar::item:pressed { background: white; }"
menuBar.setStyleSheet(style);

QMenuBar items style in inactive window

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.

Disable initial animation state in css?

Say I have a button like this:
<button>Some Text</button>
And by default the stylesheet has the text as black.
I then have this css:
button {
transition: color 0.15s ease-out;
color: rgba(255,255,255,0.75);
}
button:hover {
transition: color 0.15s ease-out;
color: #fff;
}
The goal is to subtly fade from a grayish off-white, to white, on hover, and then back again to normal button appearance on hover off.
The problem is when i firstly load the page, the black initial color of button is overwritten by the transition that i applied on button, and one can briefly see black text fading to off-white.
How do I make it so that the initial state just loads without animation? Or more precisely, is there a way to achieve something like :hover-off to control the animation only when the hover state turns off?
Sorry i have less reputation to comment. Your animation does not work, please explain clearly what you want to do?
are you trying to achieve this.
button:hover {
transition: 0.5s ease-out;
color : white;
}
If you have to load some JavaScript, you can put it in the <head> after your CSS. Kind of a hack, but it works.
HTML parsing is blocked by <script> tags that initiate requests for external resources. I guess CSS animations won't start until the <body> has been fully parsed and that's the reason this works.

How to change right-arrow image in Qt's QMenu for selected items with a CSS stylesheet?

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.