Results 1 to 8 of 8

Thread: Blinking Buttons

  1. #1
    Join Date
    Jul 2009
    Posts
    39
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Blinking Buttons

    I need a blinking button between grey and red with 1 sec breaks. I tried to do this with sleep() function in c++, but it did not work properly, and whole form is locked when sleep() is executed. Do you have any suggestion?

    Thanks.

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Blinking Buttons

    Try using a QTimeLine.

  3. #3
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Blinking Buttons

    You can use a QTimer with interval of 1s and connect the "timeout()" signal with a slot that change background color
    A camel can go 14 days without drink,
    I can't!!!

  4. #4
    Join Date
    Jul 2009
    Posts
    39
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Blinking Buttons

    I used QTimer.

    for initialization

    Qt Code:
    1. QTimer *timer = new QTimer(this);
    2. connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    3. timer->start(1000);
    To copy to clipboard, switch view to plain text mode 


    and for stooping timer
    Qt Code:
    1. timer->stop();
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Blinking Buttons

    You must create a slot (ie. "changeColorSlot()") and connect it to timeout signal

    Qt Code:
    1. void MyButton::changeColorSlot()
    2. {
    3. QColor currentColor = this->palette()->color(QPalette::Button);
    4. QColor newColor;
    5. if (currentColor == this->color1)
    6. newColor = this->color2;
    7. else
    8. newColor = this->color1;
    9.  
    10. this->palette()->setColor(QPalette::Button, newColor);
    11. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by mcosta; 22nd July 2009 at 14:16. Reason: Use new ColorRole
    A camel can go 14 days without drink,
    I can't!!!

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Blinking Buttons

    Hi, just a single optimization suggestion: I would use a int to determinate the state instead of querying the color:
    Qt Code:
    1. private:
    2. QColor m_color1;
    3. QColor m_color2;
    4. int m_buttonState;
    5. //...
    6. void MyButton::changeColorSlot()
    7. {
    8. m_buttonState = ++m_buttonState % 2
    9. this->palette()->setColor(QPalette::Button, (m_buttonState) ? m_color1 : m_color2);
    10. update();
    11. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Blinking Buttons

    I would use a bool
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Blinking Buttons

    Quote Originally Posted by wysota View Post
    I would use a bool
    Even better *shame*

Similar Threads

  1. tool buttons on top of label
    By McKee in forum Qt Programming
    Replies: 5
    Last Post: 2nd November 2008, 17:07
  2. dynamcly allocated buttons
    By MarkoSan in forum Qt Programming
    Replies: 28
    Last Post: 14th January 2008, 01:08
  3. qt3 to qt4 conversion: no toolbar buttons
    By hugo vanwoerkom in forum Qt Programming
    Replies: 9
    Last Post: 25th October 2007, 18:07
  4. Problem with Parent QWidget and Buttons not working
    By VireX in forum Qt Programming
    Replies: 7
    Last Post: 11th May 2007, 23:24
  5. How to get larger radio buttons on XP?
    By Ben.Hines in forum Qt Programming
    Replies: 9
    Last Post: 24th April 2006, 20:00

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.