1

I am trying to get input from a user in one tab then show it in the second tab. I could not find a question or an example about how to do that. This is an example of the code that I want to use, How can I show the data from Tab(1) Qlabel in QTextEdit box in Tab(2), I am a beginner in pyqt5 and not sure how this would work:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class TabWidget(QDialog):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('Tab Widget Application')

        tabwidget = QTabWidget()
        tabwidget.addTab(FirstTab(), 'First Tab')
        tabwidget.addTab(SecondTab(),'Second Tab')

        vbox=QVBoxLayout()
        vbox.addWidget(tabwidget)

        self.setLayout(vbox)
class FirstTab(QWidget):
    def __init__(self):
        super().__init__()
        self.nameLabel = QLabel(self)
        self.nameLabel.setText('Name:')
        self.line = QLineEdit(self)
        self.line.move(80, 20)
        self.line.resize(200, 32)
        self.nameLabel.move(20, 20)
        self.btn=QPushButton('switch',self)
        self.btn.move(80, 50)
        self.btn.clicked.connect(lambda: SecondTab.display(SecondTab(),self.nameLabel.text()))
class SecondTab(QWidget):
    def __init__(self):
        super().__init__()
        self.layout = QVBoxLayout()
        self.editor=QTextEdit()
        self.layout.addWidget(self.editor)
        self.setLayout(self.layout)


    def display(self,text):
        self.editor.setText(text)

if __name__ == '__main__':
    app=QApplication(sys.argv)
    tabwidget = TabWidget()
    tabwidget.resize(500,500)
    tabwidget.show()
    app.exec()

1 Answer 1

3

In order to track changes amongst child widgets, you'll need a main "controller".
Your QTabWidget will suffice, as long as you implement it in the correct way:

class TabWidget(QDialog):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('Tab Widget Application')

        # if the target widget of the layout is provided as an init argument, the
        # layout will be automatically set to it
        vbox = QVBoxLayout(self)

        tabwidget = QTabWidget()
        vbox.addWidget(tabwidget)

        firstTab = FirstTab()
        tabwidget.addTab(firstTab, 'First Tab')
        secondTab = SecondTab()
        tabwidget.addTab(secondTab,'Second Tab')

        firstTab.line.textChanged.connect(secondTab.editor.setPlainText)
        firstTab.btn.clicked.connect(lambda: tabwidget.setCurrentWidget(secondTab))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.