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. Qtabwidget addtab duplicates tab on second click
QtWS25 Last Chance

Qtabwidget addtab duplicates tab on second click

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 382 Views
  • 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 Offline
    U Offline
    Ucn_
    wrote on last edited by
    #1

    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
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • U Offline
        U Offline
        Ucn_
        wrote on last edited by
        #3

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

        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