在qt中创建qlabel的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"));
}
}
我应该在构造函数中运行另一个for循环后,我初始化标签调用函数setplayerpiece为每个实例?本质上我想做的是给每个玩家分配一张图片。如果我说得不清楚,或者你需要更多的信息,请告诉我。谢谢你的帮助。
这个方法如何:
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]));
}
相关文章:
- 将 Qvector<uint8_t> 转换为 QString
- 正确清洁 QVector
- 在 QVector<std::unique_ptr 上使用 std::find<Type>>
- 如何使用新运算符跟踪在循环中创建的 QLabel
- 除了构造函数之外,还创建 QLabel
- Qt - QVector 和模型视图 - 从列表视图获取自定义类的最佳方法是什么?
- 阅读 QML 中结构C++ QVector 的 QVector
- 在 QLabel 中显示视频
- Qt GUI使用Qfiledialogbox和qlabel显示视频,这是我的代码
- QDataStream read to QVector
- 如何在C++中将自定义文本设置为 QLabel?
- 如何使用鼠标单击选择多个 QLabel 对象?
- 在特定时间内更改 QLabel 的背景颜色
- 调用QVector::保留即使不知道确切的元素数量?
- C++Qt QtConcurrent::filtered从std::shared_ptr的QVector减少
- 如何创建可以包含不同小部件的QVector或其他容器
- QT - 如何从 QVector<QComboBox* 获取信号"currentIndexChanged">
- 使用 QVector 创建图形磁贴(QCache 之前的基础知识)
- 如何将QLabel放入切换按钮Qt c ++中
- 在qt中创建qlabel的QVector