繁体   English   中英

在qt中创建QLabels的QVector

[英]Creating a QVector of QLabels in qt

我正在尝试创建QLabels的QVector。

我不确定该怎么做。 我已经这样声明了我的QVector: QVector<QLabel> labels

在我的.cpp文件中,我想将每个标签设置为一个像素图。 我应该首先通过for循环初始化所有实例吗?

在我的构造函数中:

 for(int i = 0; i < usrinput; i++)
  {
     labels.append(new QLabel);
     setplayerpiece(i);
  }

我在构造函数之外有一个将每个QLabel设置为图像的函数:

   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"));
  }
}

在初始化为每个实例调用函数setplayerpiece的标签后,是否应该在构造函数中运行另一个for循环? 本质上,我想做的就是为每个玩家分配一张图片。 如果我含糊不清或您需要更多信息,请告诉我。 感谢您的任何帮助。

这种方法怎么样:

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]));
}

如果这是您在设计中想要的,那么所有这些都可以在构造函数内部完成。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM