Results 1 to 5 of 5

Thread: Possible to set duration of QPushButton Pressed event?

  1. #1
    Join Date
    Jan 2017
    Posts
    54
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Possible to set duration of QPushButton Pressed event?

    I change the background color of a QPushbutton when pressed with the following code:
    Qt Code:
    1. self.ui.SaveBtn.setStyleSheet(
    2. self.ui.SaveBtn.styleSheet() +
    3. 'QPushButton:pressed { background-color: #99ff99; border: 2px solid #097547; }')
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    503
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Possible to set duration of QPushButton Pressed event?

    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

  3. #3
    Join Date
    Jan 2017
    Posts
    54
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Possible to set duration of QPushButton Pressed event?

    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:
    Qt Code:
    1. def showgreensaved(self):
    2. timer = QtCore.QTimer()
    3. timer.start(0)
    4. while timer < 3000:
    5. self.ui.SaveBtn.setText('SAVED')
    6. self.ui.SaveBtn.setStyleSheet(
    7. self.ui.SaveBtn.styleSheet() +
    8. 'QPushButton:pressed { background-color: #99ff99; border: 2px solid #097547; }')
    9. self.ui.SaveBtn.setText('Save')
    10. self.ui.SaveBtn.setStyleSheet(self.buttonstyle)
    To copy to clipboard, switch view to plain text mode 
    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?
    Last edited by dennisvz; 14th May 2019 at 15:27.

  4. #4
    Join Date
    Jan 2017
    Posts
    54
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Possible to set duration of QPushButton Pressed event?

    Solved: I used:
    Qt Code:
    1. self.ui.SaveBtn.setText('SAVED')
    2. self.ui.SaveBtn.setStyleSheet('background-color: #99ff99; border: 2px solid #097547; ')
    3. QTimer.singleShot(1500, self.normalbtn)
    To copy to clipboard, switch view to plain text mode 

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

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Possible to set duration of QPushButton Pressed event?

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 4
    Last Post: 2nd April 2013, 10:38
  2. Replies: 2
    Last Post: 26th October 2011, 16:41
  3. Replies: 2
    Last Post: 12th September 2011, 16:40
  4. Removing pressed effect from a QPushButton
    By Luc4 in forum Qt Programming
    Replies: 3
    Last Post: 14th August 2010, 12:43
  5. Delay Loop until a QPushButton pressed
    By ljshap in forum Newbie
    Replies: 6
    Last Post: 17th January 2009, 17:30

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.