PDA

View Full Version : Time Delay for show Color in PushButton



yrandom
27th April 2017, 21:35
Hi
I make a Programm using C++ and Netbeans with a lot of pushbutton

I would like to do something like this
When i click the button, it should change to the Position i want and Change the border style or maybe Background to red or whatever and after maybe 300ms it should Change back to normal
The part with changing Position worked for me.

I used to Google but I didn found any solution. I try something like this but itsnot work




void VerschiebeSpiel::buttonClicked(QWidget* _button){
// Clicked button identifire with QSignalMapper
QPushButton* b = dynamic_cast<QPushButton*>(_button); //cast widget to pushbutton
int index = layout->indexOf(_button); // find out the index of button in gridlayout
int row, col,ax,bx;
if(index != -1){
layout->getItemPosition(index,&row,&col,&ax,&bx); // get the koordinate of this item
}

// Set border color
//b->setStyleSheet("border:2px solid #ff0000"); or set Background color
b->setStyleSheet("background-color:green");

//make delay time here to Show this Color for only a specific amoun of time like Sleep(MS) in Windows.h
// TODO
//QThread:usleep() seem not work, QTimer i think also dont work

b->setStyleSheet(""); // set stylesheet back

}
}

Any Solution for this?

Thanks

d_stranz
28th April 2017, 19:22
Changing the position of a button when you click it sounds like a very weird GUI, like one of these tricks that says "Click here" and then the button moves away when you try. I bet it will be very annoying to your users.

To change the color, you should implement a single-shot QTimer in your button click slot that uses the Functor version of QTimer::singleShot(). To make this easier, you could create a Functor that stores the pointer to the button as well as the default color. When the timeout occurs, the Functor's operator() gets executed and in it you reset the button back to the default state.

Instead of setting the style sheet, you could also simply use QPalette and store that in the Functor; to change the button's background color, you use the QPalette::Button color role.