Search code examples
c++qtqtguiqlabelqtabwidget

Qt4 QLabel in QTabWidget


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(){}


Solution

  • 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);