Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Qtabwidget addtab duplicates tab on second click

    General and Desktop
    2
    3
    192
    Loading More Posts
    • 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.
    • U
      Ucn_ last edited by

      Hello
      I have two buttons with two slots, on click I add a new tab to my qtabwidget, this works. However, It only works the way I want if I have one button, I can click multiple times it won't duplicate the tab, but If I click the second button which creates a second tab, then click the first button it duplicates the tab. If I come back to second it duplicates too. And it keeps duplicating. I would like to know how I can prevent from duplicating on second click. Thanks
      my code:

      bool MainWindow::control_tab(QString addst){
      
          bool kontrol = false;
      
          for(int i=0; i < ui->open_tools_tab->count(); i++)
          {
              if(addst == ui->open_tools_tab->tabText(i))
              {
      
                  kontrol = true;
              }
      
              else if (addst != ui->open_tools_tab->tabText(i))
      
              {
      
                  kontrol = false;
      
              }
      
          }
          return kontrol;
      }
      
      void MainWindow::on_tB_add_students_clicked()
      {
          addst = "Add student";
      
          if (control_tab(addst) == true)
              {
                   for(int i=0; i < ui->open_tools_tab->count(); i++)
                   {
                       if(addst == ui->open_tools_tab->tabText(i))
                       {
      
                          ui->open_tools_tab->setCurrentIndex(i);
      
                       }}
                   ui->tab_menu->setCurrentIndex(1);
      
          }
      
              else
                       {
      
                          ui->open_tools_tab->addTab(new QWidget(), icon, addst);
      
                      }
      }
      
      
      void MainWindow::on_tB_add_teachers_clicked()
      {
          addst = "Add teacher";
      
          if (control_tab(addst) == true)
              {
                   for(int i=0; i < ui->open_tools_tab->count(); i++)
                   {
                       if(addst == ui->open_tools_tab->tabText(i))
                       {
      
                          ui->open_tools_tab->setCurrentIndex(i);
      
                       }}
                   ui->tab_menu->setCurrentIndex(1);
      
          }
      
              else
                       {
      
                          ui->open_tools_tab->addTab(new QWidget(), icon, addst);
      
                      }
      }
      
      1 Reply Last reply Reply Quote 0
      • Christian Ehrlicher
        Christian Ehrlicher Lifetime Qt Champion last edited by

        What are you trying to achieve? Only adding the tab once? Then you should fix your control_tab() function to return true once the tab with the given name is found.

        Qt has to stay free or it will die.

        1 Reply Last reply Reply Quote 1
        • Christian Ehrlicher
          Christian Ehrlicher Lifetime Qt Champion last edited by

          What are you trying to achieve? Only adding the tab once? Then you should fix your control_tab() function to return true once the tab with the given name is found.

          Qt has to stay free or it will die.

          1 Reply Last reply Reply Quote 1
          • U
            Ucn_ last edited by

            Christian Ehrlicher, Thanks. Removing the "else if" and returning "true" in the control_tab() fixed.

            1 Reply Last reply Reply Quote 0
            • First post
              Last post