Search code examples
c++qtqt4qtwidgets

How to sync the tab widget with a push button


I am fairly new and unfamiliar with QT and was wondering how you could sync a pushbutton with the tab widget. What I am trying to accomplish is for the user to able to click a button “next” and be able to be taken to the next tab. It’s turning out to be more difficult than I thought. Please give detailed instructions if able because like I said before, I’m not too familiar with QT.

void MainWindow::on_pushButton_clicked(){
              ui->tabWidget;
           int count = tabWidget.count();
           int currentTab=tabWidget.currentIndex();

    if (currentTab == count - 1){
        //tabWidget->setCurrentIndex(0);
        tabWidget.setCurrentIndex(0);
        tabWidget.setCurrentIndex(currentTab+1);

    }

}

Solution

  • You have to get currently active tab index and increment it. But you also have to decide what if the current tab is the last one. Should you go to the first one or do nothing ? A schematic code would look like this (next tab goes to first if out of range).

    EDIT: I made a quick example myself, and this code works for me.

    void Form::on_pushButton_clicked()
    {
        int count = ui->tabWidget->count();
        int currentTab = ui->tabWidget->currentIndex();
        if (currentTab == count - 1)
            ui->tabWidget->setCurrentIndex(0);
        ui->tabWidget->setCurrentIndex(currentTab+1);
    }