Frederik Petri
Frederik Petri

Reputation: 481

PyQt5 QTabWidget: How to switch between tabs contained in classes and in the same window?

I would like to switch tabs contained in their own classes when pushing a button (next/prior tabs). However, I cannot seem to do it. If I use self.tabs.show() I open a new window. However, I just want it to switch in the same window. Can you help me? Example code below.

from PyQt5.QtWidgets import QPushButton, QTabWidget, QApplication, QDialog, QVBoxLayout, QWidget
import sys

class Tab(QDialog):
    def __init__(self):
        super().__init__()

        vbox = QVBoxLayout()
        self.tabWidget = QTabWidget()
        self.tabWidget.addTab(TabContact(), "Contact Details")
        self.tabWidget.addTab(TabPeronsalDetails(), "Personal Details")
        vbox.addWidget(self.tabWidget)
        self.setLayout(vbox)


class TabContact(QWidget):
    def __init__(self):
        super().__init__()
        pushButton = QPushButton("Go to next")
        pushButton.clicked.connect(self.nextTab)

        vbox = QVBoxLayout()
        vbox.addWidget(pushButton)

        self.setLayout(vbox)

    def nextTab(self):
        self.tabs = Tab()
        self.tabs.tabWidget.setCurrentIndex(0)


class TabPeronsalDetails(QWidget):
    def __init__(self):
        super().__init__()

        pushButton_2 = QPushButton("Go to prior")
        pushButton_2.clicked.connect(self.priorTab)

        vbox = QVBoxLayout()
        vbox.addWidget(pushButton_2)

        self.setLayout(vbox)

    def priorTab(self):
        self.tabs = Tab()
        self.tabs.tabWidget.setCurrentIndex(0)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    tabdialog = Tab()
    tabdialog.show()
    app.exec()

Upvotes: 0

Views: 6115

Answers (1)

Heike
Heike

Reputation: 24420

You shouldn't try to change tabs from within TabContact or TabPeronsalDetails. This should be done from within Tab instead. In order for Tab to get access to the navigation buttons in TabContact and TabPeronsalDetails you need to make these buttons instance variables i.e.

class TabContact(QWidget):
    def __init__(self):
        super().__init__()
        self.pushButton = QPushButton("Go to next")

        vbox = QVBoxLayout()
        vbox.addWidget(self.pushButton)

        self.setLayout(vbox)

class TabPeronsalDetails(QWidget):
    def __init__(self):
        super().__init__()
        self.pushButton_2 = QPushButton("Go to prior")

        vbox = QVBoxLayout()
        vbox.addWidget(self.pushButton_2)

        self.setLayout(vbox)

In Tab you can then connect an appropriate slot to the clicked signals of the buttons by doing something like

class Tab(QDialog):
    def __init__(self):
        super().__init__()

        vbox = QVBoxLayout()
        self.tabWidget = QTabWidget()

        contact = TabContact()
        personal_details = TabPeronsalDetails()

        self.tabWidget.addTab(contact, "Contact Details")
        self.tabWidget.addTab(personal_details, "Personal Details")

        vbox.addWidget(self.tabWidget)
        self.setLayout(vbox)

        # connect slots to signals
        contact.pushButton.clicked.connect(self.next_tab)
        personal_details.pushButton_2.clicked.connect(self.prev_tab)

    def next_tab(self):
        cur_index = self.tabWidget.currentIndex()
        if cur_index < len(self.tabWidget)-1:
            self.tabWidget.setCurrentIndex(cur_index+1)

    def prev_tab(self):
        cur_index = self.tabWidget.currentIndex()
        if cur_index > 0:
            self.tabWidget.setCurrentIndex(cur_index-1)

Upvotes: 2

Related Questions