Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Troubles adding a new tab in QTabWidget



  • Hi guys,
    I'd really appreciate some help with a problem I'm having adding a new tab to a QTabWidget.

    Basically, that's what's happening:
    I have a software that uses a QTabWidget with a QMdiArea in each tab.
    It can then save the state of the workspace via QSettings, and then restore it when the software is relaunched via the following code:

        targetSettings->beginGroup("tabs");
    
        QString tabRoot;
        QString label;
    
        //analizes all the strings in the settings file to analize each
        for (auto str : targetSettings->childGroups()) {
    
            //restoring tabs
            if(str.contains("tabTitle")) {
    
                //removes the tab created initializing the widget
                ui->tabWidget->clear();
    
                //restores the settings saved in the tabs group
                targetSettings->beginGroup(str);
    
                qDebug() << "found tab key: " << str;
    
                tabRoot = str.left(str.lastIndexOf('|')+1);
                label = str.remove(tabRoot);
    
                //label = targetSettings->value(tramite).toByteArray();
                qDebug() <<"found tab label" << label << "suitable to be added";
    
                foundTabs = true;
    
                int nindex = AddTab(label);
    
                this->restoreSubWindows(nindex,targetSettings);
    
                targetSettings->endGroup();
            }
        }
        targetSettings->endGroup();
    

    ButI'm having problems adding a tab via the AddTab function:

    int workspace::AddTab(QString label) {
        QMdiArea * newMdi = new QMdiArea (this);
        int nindex = ui->tabWidget->addTab(newMdi,label);
    
        connect(this, SIGNAL(tileWidgets(int)),
                newMdi, SLOT(tileSubWindows()));
    
        connect(ui->tabWidget, SIGNAL(tabCloseRequested(int)),
                newMdi, SLOT(deleteLater()));
    
        ui->tabWidget->setCurrentIndex(nindex);
    
        qDebug() << "added tab: " << nindex ;
    
        return nindex;
    }
    

    when this function is called in the above function when launching the software, I can see in the application output that "nindex" is always 0, and in the QTabWidget there's only the latest tab saved in the structure, while all the QMdiAreas exist.
    But when the same AddTab function is called when the software is already running, it works normally, adding a new tab with a new QMdiArea.

    Best regards,
    Simone


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    @simofazz said in Troubles adding a new tab in QTabWidget:

    for (auto str : targetSettings->childGroups()) {

        //restoring tabs
        if(str.contains("tabTitle")) {
    
            //removes the tab created initializing the widget
            ui->tabWidget->clear();
    

    Each time tabTitle is found you delete the content of your tab widget so if it's found more than once, only the last will appear.



  • @SGaist
    Thank you very much. I must have moved that line without ever noticing.


Log in to reply