QT creator, Same pushbutton with different labels

Hey guys, Im doing a cardgame in QT c++. And i would like to use the same pushbutton but with different labels each time it gets clicked. For example.

U click the pushbutton, a card shows in label_1. Then u click the button again and this time a new card shows in label_2.

1
2
3
4
5
6
7
8
9
10

void BjMainWindow::on_pushButton_clicked()
{

    QPixmap pix(theDeck->toString(amountOfCards++).c_str());
    ui->label_1->setPixmap(pix);


}


The next click should be ui->label_2->setPixmap(pix); and so on.

I was trying to this with an array of strings containing the different labels. But the ui wont accept ui actions unless they are called at the right name from the beginning. For example i can't do like this:

1
2
3
4
string x[3] = {"label_1","label_2","label_3"};

ui->x[counter++]->setPixmap(pix);


I am not sure how to make it work.

And why i want different labels is because i want the old card to be showed after the new one shows up.

If anyone have any ideas i would be greatful :D

//It
Can you make an array of label pointers? And then do something like this?
ui->label[0/* or 1 or 2 or 3...*/]->setPixmap(pix);

Edit: Also if you only have three labels and want the counter to go 0, 1, 2, 0, 1, 2, 0, 1, 2,... You can use integer modulus to wrap the counter back around from 3 to 0.
counter = (counter+1) % 3; //increment and wrap around if necessary
Last edited on
kevinkjt2000 is on the right track:

Use a class member container to store pointers to your labels, then access the container in your slot.

MainWindow class member: QList<QLabel *> labelList;

MainWindow ctor:
1
2
3
labelList.append(ui->label_1);
labelList.append(ui->label_2);
. . . 


slot: labelList[counter++]->setPixmap(pix);
Works flawless. Thank you guys!
Topic archived. No new replies allowed.