سؤال

I want to dynamically created interface for my hello-world QT + C++ program from .ini file.

1 Step I read settings file with QSettings - it is simple.

2 Step I try to draw interface:

//i == 5;
for(int temp=1;temp <= i;temp++){
    QString tName = settings.value("/Level" + QString::number(temp) + "/Name", "").toString();
    QString tImage = settings.value("/Level" + QString::number(temp) + "/Image", "").toString();
    QString Imgpath = QApplication::applicationDirPath() + "/images/" + tImage;
    QPixmap pix(Imgpath);
    tab.addTab(new QLabel(Imgpath, &tab), tName);
}

All looks great - QLabel contains filepath to image, header of TAB contains right NAME from iniFile. BUT! I want to setPixmap() to QLabel and here is problem. new QLabel() query doesn't have any name I can use to set any option. Please help me with that for(){}

هل كانت مفيدة؟

المحلول

You can convert this:

QPixmap pix(Imgpath);
tab.addTab(new QLabel(Imgpath, &tab), tName);

to:

QLabel* label = new QLabel(Imgpath, &tab);
label->setPixmap(QPixmap(Imgpath));
tab.addTab(label, tName);

نصائح أخرى

You can use the Pixmap property:

pixmap : QPixmap

This property holds the label's pixmap.

If no pixmap has been set this will return 0.

Setting the pixmap clears any previous content. The buddy shortcut, if any, is disabled.

So, you would set your pixmap on the label as follows:

//i == 5;
for (int temp = 1; temp <= i; ++temp) {
    QString tName = settings.value("/Level" + QString::number(temp) + "/Name", "").toString();
    QString tImage = settings.value("/Level" + QString::number(temp) + "/Image", "").toString();
    QString Imgpath = QApplication::applicationDirPath() + "/images/" + tImage;
    QPixmap pix(Imgpath);
    QLabel *myLabel = new QLabel(Imgpath, &tab);
    myLabel->setPixmap(pix);
    tab.addTab(myLabel, tName);
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top