Changing QPushButton Color when Pressed - c++

Is there a way to change the background color of a QPushButton which is pressed by modifying its QPalette object? I realize it can be done with style sheets but would like a way to do it by modifying the QPalette. Something similar to how one changes the background color of the button when not pressed:
QPushButton myButton;
QPalette p(myButton.palette());
p.setColor(QPalette::Button, QColor("#ffffff"));
myButton.setPalette(p);

Simply add a stylesheet to the qbushbutton itself or to his parent qwidget:
qwidget.setStyleSheet("QPushButton:checked { background-color: red; }")
This will set the background color to red when the QPushButton is checked.

Connect to QPushButton's pressed() signal and update the palette with your desired color and then connect to released() signal and switch back the color to the old color. That should do it.
You may want to run update() after settings the colors or in extreme cases repaint().

Connect to the button's pressed signal and set the palette in the connected slot. You may have to call repaint() to force an immediate update.

you can set stylesheet for button when it is pressed.. here is the Example tells how to set style sheet for button.. but in your case u need to have two different style sheets, one is button pressed and similarly when it is released you should reset with some other stylesheet.

Related

QPushButton with Image not getting highlighted when focused (MAC)

I have a QPushButton with image, i have set focus policy to strong focus but, while focusing that QPushButton it is not getting highlighted(no dot frame or default blue border)
You should take a look at the QPalette(in Qt Designer) of your QPushButton and in particular, the HighLight color which is supposed to handle this effect. Moreover, make sure you didn't define focusPolicy in the stylesheet of a parent of that QPushButton, as that could cause problems, because QPushButton would inherit that property.

Issue changing a QPushButton style in Qt

I have created a QPushButton in Qt without applying any style, so it inherits the style from Windows10, with this result:
Then I wanted to change temporary the color of the button, so I used:
pushButton->setStyleSheet("background-color: rgb(255,220,220)")
getting this result:
Already this result does not satisfy me because also the style is slightly different from the original one. Anyway the next step was that the button had to return to the "normal" style when pressed, so I added this command
pushButton->setStyleSheet("background-color: rgb(240,240,240)")
but the result is different from the starting button:
Can you please give me some advice to better manage the style?
Thanks
Actually when you set background-color alone to QPushButton, The background may not appear unless you set some value for border.
Look here for (List of Stylable Widgets: QPushButton)
http://doc.qt.io/qt-5/stylesheet-reference.html
I think in windows 10 for some reason, you are able to see something without even setting border.
But the recommended way is to set some border value.
So try setting border value as said below, and see if it addresses your requirement:
pushButton->setStyleSheet("background-color: rgb(255,220,220);border: none; ")
In the above said link you can find below information:
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.
Here are some snippets you may find quite similar and helpful.
I had an Update button which I turned into red Cancel button. Once the update action is finished or cancel is pressed, I restored the original color and text.
// Global variables to save off button state
QPalette update_btn_palette_restore;
QString update_btn_text_restore;
....
// Update button is pressed.
// Save the palette and text.
update_btn_palette_restore = _ui->update_button->palette ();
update_btn_text_restore = _ui->update_button->text ();
// Change the color palette and text
QPalette p=palette();
p.setBrush(QPalette::Button,Qt::red);
_ui->update_button->setPalette(p);
_ui->update_button->setText ("Cancel");
....
// Handler for when either cancel is pressed or update has finished
if(! update_btn_text_restore.isEmpty ()) {
_ui->update_button->setText (update_btn_text_restore);
_ui->update_button->setPalette(update_btn_palette_restore);
}

QDockWidget::background-color not applied when docked

I have a QDockWidget:
I would like to alert the user to certain events by setting the background color of the title bar.
I have achieved this by setting the style sheet for my DockWidget:
void DockWidget::setCriticalAlert()
{
setStyleSheet("QDockWidget { background-color:red; }");
}
The result is this:
The problem is that the background-color doesn't get applied when the QDockWidget is docked:
How can I get the background color to be applied when the QDockWidget is docked?
This is a bug in Qt.
Issue 10537
Quoting from the linked issue:
The problem is that in QDockWidget::paintEvent, there is a
isFloating() condition before drawing PE_FrameDockWidget. We cannot
jsut remove this condition as it would break the other style (that
does not whish to draw frame when the dockwidget is docked) We cannot
either use PE_Widget to draw the frame as then it goes over the
dockwidget's title The solution is maybe to introduce a new
PE_FrameDockWidgetDocked primitive element. Or some
SH_DockWidget_DrawDockedFrame stylehint to draw the frame in every
cases.
a valid workaround seems to be to set the stylesheet of the parent, and use the class-and-id selector. Forgive the python formatted code but the concept is the same - in this case, 'dock' is a QDockWidget which has been given an object name using setObjectName(), and its parent, the QMainWindow, is 'self':
self.setStyleSheet("QDockWidget#"+str(dock.objectName())+"::title {background-color:red}")
In PyQt5.5, this works at runtime, i.e., can be changed on the fly.
I find a solution like this:
Firstly put a frame behind all the widgets of dockwidget's center widget, as the background.
Then set stylesheet for the frame.
By this way, we could change the background color of dockwidget.
Or you can extend the dockwidget and overwrite the function
void QDockWidget::setWidget(QWidget *widget)
using private/qdockwidget_h. and add a frame as this widget's father.

Putting a line on a QPushButton

I am creating a program that draws graphs; I want to be able to have a button that a user can press to choose the line color and style. I want to be able to visually show what the current selection is.
Currently, I know that I can do that using two seperate widgets, a QPush button, and then a widget I make myself that just draws a line across it using QPen.
I would like to turn these two widgets into a single widget. I want this widget to be a pushable "button" that the user presses and I can get a signal out of to run a routine that sets a new QPen.
Is this functionality built in? Or would I need to create a new widget that re-implements either QPushButton or QActionButton? Or should I just make my widget which listens to mouseclick events on it and create a signal slot from there?
You could use a QLabel, set the style sheet, and use that as your line on the graph. You could use the bounds of a QGroupBox to define x and y axis.
Maybe something like this:
yourQLabel.setStyleSheet("QWidget {background-color: rgb(255, 0, 0); color:white; border: 1px solid black;}"); // red with black border
Then you can set the height, width and position of the QLabel based on values for your graph. Of course this will only work if lines on your graphs are rectangular. If they aren't, then you'll probably have to use something other than QLabel.
Use the setGeometry method of QLabel to set this.
yourQLabel.setGeometry(x, y, width, height);
As far as your button, you can do something similar with that if you want. A QPushButton also has the setGeometry and setStyleSheet methods.
If you want to tell your button to do something on click, assuming you have a QPushButton object called myButton, add this to your header file (.h):
class YourClass
{
Q_OBJECT
//...
private slots:
void on_myButton_clicked();
}
Then in your source file (.cpp):
void YourClass::on_myButton_clicked()
{
// stuff your button does on click
}

How can you modify a Qt stylesheet?

How can I modify an existing stylesheet?
For example: if I want to create buttons, which when pressed each modify a single aspect of the stylesheet. One button can insert a margin-left attribute of 10. Another button can make the background colors blue. Lastly, another button can round the corners. The trick here though, is that I dont want to store all the variables and rebuild the style sheet on each button press. I would like to have a simple this->setStyleSheet(this->getStylesheet()+"margin-left: 10px:") for example.
Is there any way to do this?
Here is the code in main.cpp
QWidget wdg;
QHBoxLayout hlay;
wdg.setStyleSheet("border:2px solid rgb(74, 74, 74);");
QPushButton btn;
btn.setStyleSheet("border-radius:5px;");
btn.setText("Hello");
QPushButton btn2;
btn2.setStyleSheet("background-color: rgb(190, 190, 190);");
btn2.setText("Hello");
hlay.addWidget(&btn);
hlay.addWidget(&btn2);
qDebug()<<btn.styleSheet();
wdg.setLayout(&hlay);
wdg.show();
setting and getting style sheet works with QString and so you can use + operator.