If LineEdit = X, close X tabs from QTabWidget



  • Hi! I've tried this code:
    @ if (ui->lineEdit->text() = "1")
    ui->tabWidget->removeTab(1);
    ui->tabWidget->removeTab(2);
    ui->tabWidget->removeTab(3);
    ui->tabWidget->removeTab(1);
    ui->tabWidget->removeTab(2);
    ui->tabWidget->removeTab(3);@

    but it does not seem to work... Here's the error:
    @could not convert 'QLineEdit::text() const().QString::operator=(((const char*)"1"))' from 'QString' to 'bool'@

    What did I do wrong?



  • You need to use "==" and not "=" in your if condition.



  • Thanks.. bad mistake. :-S
    I've got another one, would be great if you know a solution for this one, too:
    @if (ui->lineEdit->text() == "1")
    ui->tabWidget->removeTab(1);
    ui->tabWidget->removeTab(2);
    ui->tabWidget->removeTab(3);
    ui->tabWidget->removeTab(1);
    ui->tabWidget->removeTab(2);
    ui->tabWidget->removeTab(3);

        if (ui->lineEdit->text() == "2")
            ui->tabWidget->insertTab(1, ui->tab_2, "#2");
            ui->tabWidget->removeTab(2);
            ui->tabWidget->removeTab(3);
            ui->tabWidget->insertTab(1, ui->tab_2, "#2");
            ui->tabWidget->removeTab(2);
            ui->tabWidget->removeTab(3);
    
            if (ui->lineEdit->text() == "3")
                ui->tabWidget->insertTab(1, ui->tab_2, "#2");
                ui->tabWidget->insertTab(2, ui->tab_3, "#3");
                ui->tabWidget->removeTab(3);
                ui->tabWidget->insertTab(1, ui->tab_2, "#2");
                ui->tabWidget->insertTab(2, ui->tab_3, "#3");
                ui->tabWidget->removeTab(3);
    
                if (ui->lineEdit->text() == "4")
                    ui->tabWidget->insertTab(1, ui->tab_2, "#2");
                    ui->tabWidget->insertTab(2, ui->tab_3, "#3");
                    ui->tabWidget->insertTab(3, ui->tab_4, "#4");
                    ui->tabWidget->insertTab(1, ui->tab_2, "#2");
                    ui->tabWidget->insertTab(2, ui->tab_3, "#3");
                    ui->tabWidget->insertTab(3, ui->tab_4, "#4");@
    

    When I now insert 2, 3 or 4 in my QLineEdit, all 4 tabs will be loaded. When I insert 1 again, nothing happens (all 4 tabs stay loaded). Why?



  • First of all you need to keep multiple statements inside { } brackets otherwise only the first statement after the condition will work and other will be executed in sequential order.

    Secondly, What are you trying to achieve ? Where are you putting the above lines of code? You need to provide more information.

    You can use signal and slots to achieve your goal.



  • I'm trying to put a QLineEdit in my application. The users can put in a number between 1 and 4 to choose, how many tabs (with QTableWidgets inside) should be opened.

    But with the brackets it worked. Thanks! (I should learn some basics of C++/Qt, huh? :-S)



  • also instead of the multiple if clauses you could use "switch/case":http://en.cppreference.com/w/cpp/language/switch .


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.