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

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."

Related

QWidget background transparent

I have a widget in the main window, the widget has the following styles:
background-color:#ffeeeeee;width:100%;height:100%;
It is set-up to widget layout and controls auto size to fill the geometry of the widget, but the background of the widget is always transparent. What can I do to make it opaque?
This is using Qt5.6 running on RedHat Linux 7.2
NOTE: The syntax #ffeeeeee is specific to Qt, where the first node is the alpha, then red, green and blue octets.
You have too many characters in your color. Change:
background-color: #ffeeeeee;
to:
background-color: #eeeeee;
If you want to use transparency, you'll have to be explicit:
background-color: rgba(...);
With the background-color present and set to any of the following:
background-color:white;width:100%;height:100%;
or
background-color:#ffffff;width:100%;height:100%;
or my original:
background-color:#ffeeeeee;width:100%;height:100%;
The property, autoFillBackground is set to false and the background of the widget is transparent at run-time.
However removing any background-color style and leaving the style as just:
width:100%;height:100%
The property, autoFillBackground is set to true and the widget is opaque.
As far as I can see there is no error in the styles, I believe this may be an error in Qt.
Have you tried using QWidget::setWindowOpacity(1) where 1 = opaque and 0 = transperant.
Also try QGraphicsOpacityEffect.The following is code for setting the 50 percent opacity for a pushbutton but It can be extended to a widget as well.
ui->setupUi(this);
QGraphicsOpacityEffect * effect = new QGraphicsOpacityEffect(ui->pushButton);
effect->setOpacity(0.5);
ui->pushButton->setGraphicsEffect(effect);

Editing Qt style sheet of QCheckBox [duplicate]

I'm unable to change the color of Qcheckbox in QT, can somebody help me with code to change color of check box text label.
I have tried Qpalette.. And im using QT4.7.4 version..
You could use stylesheets.
e.g:
checkBox->setStyleSheet("QCheckBox { color: red }");
For more details check the style sheets in Qt Reference and the stylesheets documentation
This works for me:
QPalette p = myCheckBox->palette();
p.setColor(QPalette::Active, QPalette::WindowText, green);
myCheckBox->setPalette(p);
I ran into this problem using various versions of Qt5 (5.2, 5.4). To do it with style sheets I had to use the Pseudo-States properties: http://doc.qt.io/qt-4.8/stylesheet-reference.html#list-of-pseudo-states
Example:
myCheckbox->setStyleSheet("QCheckBox:unchecked{ color: red; }QCheckBox:checked{ color: red; }");
Setting both states changed the colors for me. It seems there are some oddities like this in the stylesheet and palette system so keep your eyes out for them and try a few different things (i.e. selectors, pseudo-states, etc.)
Looks like for some widgets you have to force using non-system "widget-engine". For checkbox it can be done by setting borders to none. So checkbox's style looks like:
QCheckBox {
border: none;
color: white;
}
Similar behavior is required by other widgets. Some style-properties do not disable native look. For example QPushButton (http://doc.qt.io/qt-4.8/stylesheet-reference.html)

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.

Styling QListWidget item widgets: selected state

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

Qwidget, how to highlight the widget under the cursor

I have created some QLabel type of widgets in QT, and added that to a QToolbar. I want to highlight the particular widget which is under the cursor. I am unable to understand how do I do that. Can somebody please help ? I need this information on QT 4.
Thanks.
You have several possibilities.
First, as you subclass QLabel, you can handle mouse events directly in your class.
Make sure to use QWidget::setMouseTracking() to enable this.
In such scenario you can do whatever you want with your control but you will have to
override paint routine so that your class can draw itself in some specific way.
Unfortunately QLabel does not support "hover" style sheet state so that you cannot
do it easily with styles sheets. However, if you consider subclassing from QPushButton you can have this wonderful feature
so that with the help of CSS you get nice highliting effect.
For more info on style sheets in QT look here.
If subclassing QPushButton is fine for you, then look here.
Just make sure you also use hover state like in this simple example:
QPushButton:hover {
background-color: black;
}
QPushButton:hover {
background-color: white;
}
Example for the mouse events handling can be found here