Reputation: 5
I am trying to create a QVector of QLabels.
I am not sure how to go about this. I have declared my QVector like this: QVector<QLabel> labels
inside my .cpp file I want to set each label to a pixmap. Should I initialize all the instances through a for loop first?
Inside my constructor:
for(int i = 0; i < usrinput; i++)
{
labels.append(new QLabel);
setplayerpiece(i);
}
I have a function outside of the constructor that sets each QLabel to an image:
void CentralWidget::setplayerpiece(int tk)
{
if (p[tk]->setpiece() == 0)
{
labels[tk]->setPixmap(QPixmap(":/images/hat.png"));
}
else if (p[tk]->setpiece() == 1)
{
labels[tk]->setPixmap(QPixmap(":/images/car.png"));
}
else if (p[tk]->setpiece() == 2)
{
labels[tk]->setPixmap(QPixmap(":/images/shoe.png"));
}
else if (p[tk]->setpiece() == 3)
{
labels[tk]->setPixmap(QPixmap(":/images/spaceship.png"));
}
else if (p[tk]->setpiece() == 4)
{
labels[tk]->setPixmap(QPixmap(":/images/basketball.png"));
}
else if (p[tk]->setpiece() == 5)
{
labels[tk]->setPixmap(QPixmap(":/images/ring.png"));
}
}
Should i run another for loop in the constructor after I initialize labels that calls the function setplayerpiece for each instance? Essentially what I want to do is assign each player an image. If I was vague or you need more information please let me know. Thank you for any help.
Upvotes: 0
Views: 2197
Reputation: 365
How about this approach:
QVector<QString> playerIconPath;
playerIconPath.append(":/images/hat.png");
playerIconPath.append(":/images/car.png");
playerIconPath.append(":/images/shoe.png");
playerIconPath.append(":/images/spaceship.png");
playerIconPath.append(":/images/basketball.png");
playerIconPath.append(":/images/ring.png");
QVector<QLabel*> labels
for(int i = 0; i < playerIconPath.size(); i++)
{
labels.append(new QLabel);
labels[i]->setPixmap(QPixMap(playerIconPath[i]));
}
All this can be done inside the constructor if that's what you would like in your design.
Upvotes: 1