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

Qtabwidget addtab duplicates tab on second click



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

  • Lifetime Qt Champion

    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.


  • Lifetime Qt Champion

    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.



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


Log in to reply