So the counter for each buttton separately. I suggest using QMap <QPushButtton *, int>. The service would look something like this.
QMap<QPushButton*,int> counter_list;//defined as MainWindow variable
void MainWindow::on_pushButton_clicked()
{
QPushButton *ptr
= dynamic_cast<QPushButton
*>
(sender
());
if(ptr == nullptr)
return;
int counter = 1;
if(!counter_list.contains(ptr))
counter = counter_list.value(ptr);
if(counter==1)
{
ptr->setStyleSheet("QPushButton { background-color: grey; }\n"
"QPushButton:enabled { background-color: rgb(200,0,0,100); }\n");
counter=counter+1;
}
else
{
if(counter==2)
{
ptr->setStyleSheet("QPushButton { background-color: grey; }\n"
"QPushButton:enabled { background-color: rgb(0,200,0,100); }\n");
counter=counter+1;
}
else
{
ptr-> setStyleSheet("QPushButton { }\n"
"QPushButton:enabled { }\n");
counter=counter-2;
}
}
counter_list.insert(ptr,counter);
}
QMap<QPushButton*,int> counter_list;//defined as MainWindow variable
void MainWindow::on_pushButton_clicked()
{
QPushButton *ptr = dynamic_cast<QPushButton*>(sender());
if(ptr == nullptr)
return;
int counter = 1;
if(!counter_list.contains(ptr))
counter = counter_list.value(ptr);
if(counter==1)
{
ptr->setStyleSheet("QPushButton { background-color: grey; }\n"
"QPushButton:enabled { background-color: rgb(200,0,0,100); }\n");
counter=counter+1;
}
else
{
if(counter==2)
{
ptr->setStyleSheet("QPushButton { background-color: grey; }\n"
"QPushButton:enabled { background-color: rgb(0,200,0,100); }\n");
counter=counter+1;
}
else
{
ptr-> setStyleSheet("QPushButton { }\n"
"QPushButton:enabled { }\n");
counter=counter-2;
}
}
counter_list.insert(ptr,counter);
}
To copy to clipboard, switch view to plain text mode
Bookmarks