Walter
Walter

Reputation: 31

PyQT5 & QtabWidget - changing values in different tabs

I've build a small PyQt5 application with a QtabWidget as my CentralWidget. Because I wanted to bring some structure into the code I create new tabs by using different classes. This works pretty fine so far.

class Main_window(QtWidgets.QMainWindow):
"""Main Window"""

def __init__(self, parent=None):
    """Initializer"""
    super(Main_window, self).__init__(parent)
    self.setGeometry(50, 50, 1100, 750)

    # Create menu
    self.qtMenu3()

    self.tabWidget = QtWidgets.QTabWidget()
    # sets the tabWidget as the central widget inside the QMainWindow
    self.setCentralWidget(self.tabWidget)

    self.tab_first = QtWidgets.QWidget()
    self.tabWidget.addTab(FirstTab(), 'First')

    self.tab_second = QtWidgets.QWidget()
    self.tabWidget.addTab(SecondTab(), 'Second')

My SecondTab class looks like this and creates a GroupBox and two QTextEdits

class SecondTab(QWidget):

def __init__(self):
    super().__init__()

    self.initUI()

def initUI(self):

    self.setWindowTitle("Groupbox")
    layout = QGridLayout()
    self.setLayout(layout)

    groupbox = QGroupBox("GroupBox Example")
    # groupbox.setCheckable(True)
    layout.addWidget(groupbox)

    # Layout manager QVBox (vertical)
    vbox = QVBoxLayout()
    groupbox.setLayout(vbox)

    # radiobutton = QRadioButton("Radiobutton 1")
    # vbox.addWidget(radiobutton)

    textEdit_input = QTextEdit()
    vbox.addWidget(textEdit_input)

    textEdit_output = QTextEdit()
    vbox.addWidget(textEdit_output)

The point where I struggle now is that I want to load a txt file and the text should update the empty textEdit_input in my second tab. Because the function should work for multiple tabs I don't want to attach it to my SecondTab class. How can I properly address QTextEdit in my second tab?

Upvotes: 2

Views: 341

Answers (1)

Walter
Walter

Reputation: 31

Thanks for the input musicamante, changed the code according to your suggestions and it works.

    self.tab_first = FirstTab()
    self.tabWidget.addTab(self.tab_first,"Tab 1")
    self.tab_second = SecondTab()
    self.tabWidget.addTab(self.tab_second,"Tab 2")

and with self.tab_first.textEdit_input I can now access the needed fields.

Upvotes: 1

Related Questions