Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Troubles adding a new tab in QTabWidget
Forum Updated to NodeBB v4.3 + New Features

Troubles adding a new tab in QTabWidget

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 409 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    simofazz
    wrote on last edited by
    #1

    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

    SGaistS 1 Reply Last reply
    0
    • S simofazz

      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

      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      2
      • SGaistS SGaist

        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.

        S Offline
        S Offline
        simofazz
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved