PDA

View Full Version : Possible to set duration of QPushButton Pressed event?



dennisvz
14th May 2019, 02:27
I change the background color of a QPushbutton when pressed with the following code:


self.ui.SaveBtn.setStyleSheet(
self.ui.SaveBtn.styleSheet() +
'QPushButton:pressed { background-color: #99ff99; border: 2px solid #097547; }')


It works fine - the button changes color as long as I hold the mouse button down. I was wondering if there is a way to make the button remain the new color for a second even after releasing the mouse button? What I want is kinda like setting the duration of a .setToolTipDuration(2000), for example. Even when the button is quickly clicked and released, I would like the new color to remain for a brief time before reverting back to original color.

Thanks

Ginsengelf
14th May 2019, 09:07
Hi, connect a slot to the clicked signal and change the color. Then start a timer that is connected to a slot that resets the color of the button.

Ginsengelf

dennisvz
14th May 2019, 13:00
As a beginner, I looked at the documentation on timers but could not understand the code. Could you provide an example of the code that would set a timer on self.ui.SaveBtn for 2 seconds? thanks

Another alternative:
At the end of the "save" function that is connected to a button labeled "Save" talked about above, I call this function:


def showgreensaved(self):
timer = QtCore.QTimer()
timer.start(0)
while timer < 3000:
self.ui.SaveBtn.setText('SAVED')
self.ui.SaveBtn.setStyleSheet(
self.ui.SaveBtn.styleSheet() +
'QPushButton:pressed { background-color: #99ff99; border: 2px solid #097547; }')
self.ui.SaveBtn.setText('Save')
self.ui.SaveBtn.setStyleSheet(self.buttonstyle)

What I want to happen (the above code does not work):
When the user presses the Save button, it saves the data, then changes the text/ of the button from "Save" to "Saved" and changes the background color -- for a period of about 3 seconds, then changes the button back to the previous text and color(buttonstyle).
Does anyone know how to make the "timer" portion of the code work? or, is there a better way to do what I want?

dennisvz
14th May 2019, 19:54
Solved: I used:


self.ui.SaveBtn.setText('SAVED')
self.ui.SaveBtn.setStyleSheet('background-color: #99ff99; border: 2px solid #097547; ')
QTimer.singleShot(1500, self.normalbtn)


The self.normalbtn is a function that returns the button to original text and color

d_stranz
16th May 2019, 19:20
Solved: I used:

That is the solution I was about to suggest. Your original code shows a misunderstanding of how QTimer works. You do not start a QTimer and then keep checking it until it goes to zero. Instead, you connect a slot to the QTimer's timeout() signal. The timer will count down by itself, and fire the signal when it reaches zero. In the meantime, your application goes about doing whatever it needs to do and reacts only when the slot gets called.

(This is like almost every other GUI widget in Qt - you create it, connect your slots to its signals, and then your app runs in the event loop until one of those signals gets emitted and your slot called).

A QTimer as a member variable in a class can be reused as many times as you want. A single-shot timer works once, so you need to set it up every time you want to use it.