?print-pdf
' Created for
self.btn_cancel = qtw.QPushButton('Cancel')
self.btn_cancel.clicked.connect(self.close)
textChanged
signal that sends the text entered into the widget along with the signalsetText()
slot that accepts a string argument. We could connect them like this:
self.line_edit1 = qtw.QLineEdit()
self.line_edit2 = qtw.QLineEdit()
self.line_edit1.textChanged.connect(self.line_edit2.setText)
self.mainLayout = qtw.QVBoxLayout()
self.mainLayout.addWidget(self.line_edit1)
self.mainLayout.addWidget(self.line_edit2)
self.setLayout(self.mainLayout)
line_edit1
) in itline_edit1
that content to be printed in the console.
import sys
from PyQt6 import QtWidgets as qtw
class MainWindow(qtw.QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Set up the main layout
main_layout = qtw.QVBoxLayout(self)
# Create and add the QLineEdit to the layout
self.leTest = qtw.QLineEdit()
main_layout.addWidget(self.leTest)
# Connect the textChanged signal to the custom slot
self.leTest.textChanged.connect(self.print_content)
self.setWindowTitle('Demo')
self.show()
def print_content(self, text):
print(f'Content: {text}')
# Run the application
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec())
self.line_edit1.editingFinished.connect(lambda: print('Edit Done'))
self.line_edit2.returnPressed.connect(self.line_edit1.editingFinished)
self.line_edit.textChanged.connect(self.some_slot)
def some_slot(signal, text, x):
print(text, x)
# TypeError: some_slot() missing 1 required positional argument: 'x'
@qtc.pyqtSlot(str)
def some_slot(*args):
for arg in args:
print(arg)
pyqtSignal()
function.
# create custom signal which will carry a string data type data:
sig_submit = qtc.pyqtSignal(str)
@qtc.pyqtSlot(bool)
def onSubmit(self):
self.sig_submit.emit(self.edit.text())
self.close()
Note that MainWindow must know DataEntryDialog's implementation
import sys
from PyQt6 import QtWidgets as qtw
from PyQt6 import QtCore as qtc
from PyQt6 import QtGui as qtg
class DataEntryDialog(qtw.QDialog):
def __init__(self , *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle('My Form')
self.setGeometry(500,400,200,100)
# ------------------------- create and atach widgets ------------------------- #
self.edit = qtw.QLineEdit()
self.btn_submit = qtw.QPushButton('Submit')
self.setLayout(qtw.QVBoxLayout())
self.layout().addWidget(self.edit)
self.layout().addWidget(self.btn_submit)
class MainWindow(qtw.QWidget):
def __init__(self , *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle('My App')
self.setGeometry(400,300,300,200)
# ------------------------- create and atach widgets ------------------------- #
self.label = qtw.QLabel('Initial Text')
self.btn_change = qtw.QPushButton('change text')
self.main_layout = qtw.QVBoxLayout()
self.main_layout.addWidget(self.label)
self.main_layout.addWidget(self.btn_change)
self.setLayout(self.main_layout)
# ---------------------------------- signals --------------------------------- #
self.btn_change.clicked.connect(self.onChangeClicked)
self.show()
def onChangeClicked(self):
self.dialog = DataEntryDialog(self)
# tightly-coupled approach: we must know dialog's implementation
self.dialog.edit.setText(self.label.text())
self.dialog.btn_submit.clicked.connect(self.on_dialog_text_changed)
self.dialog.show()
def on_dialog_text_changed(self):
self.label.setText(self.dialog.edit.text())
self.dialog.close()
if __name__ == '__main__':
app = qtw.QApplication(sys.argv);
window = MainWindow()
sys.exit(app.exec())
Note that MainWindow don't care about DataEntryDialog's implementation, it just pass and receive data
import sys
from PyQt6 import QtWidgets as qtw
from PyQt6 import QtCore as qtc
from PyQt6 import QtGui as qtg
class DataEntryDialog(qtw.QDialog):
data_submitted = qtc.pyqtSignal(str)
def __init__(self , parent, msg):
super().__init__(parent)
self.setWindowTitle('My Form')
self.setGeometry(500,400,200,100)
# ------------------------- create and atach widgets ------------------------- #
self.edit = qtw.QLineEdit()
self.edit.setText(msg)
self.btn_submit = qtw.QPushButton('Submit')
self.setLayout(qtw.QVBoxLayout())
self.layout().addWidget(self.edit)
self.layout().addWidget(self.btn_submit)
# Connect the button click to emit the custom signal
self.btn_submit.clicked.connect(self.emit_data)
def emit_data(self):
self.data_submitted.emit(self.edit.text())
class MainWindow(qtw.QWidget):
def __init__(self , *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle('My App')
self.setGeometry(400,300,300,200)
# ------------------------- create and atach widgets ------------------------- #
self.label = qtw.QLabel('Initial Text')
self.btn_change = qtw.QPushButton('change text')
self.main_layout = qtw.QVBoxLayout()
self.main_layout.addWidget(self.label)
self.main_layout.addWidget(self.btn_change)
self.setLayout(self.main_layout)
# ---------------------------------- signals --------------------------------- #
self.btn_change.clicked.connect(self.onChangeClicked)
self.show()
def onChangeClicked(self):
self.dialog = DataEntryDialog(parent=self, msg=self.label.text())
# Connect the dialog's custom signal to update the label text in the main window
self.dialog.data_submitted.connect(self.on_dialog_text_changed)
self.dialog.show()
def on_dialog_text_changed(self, msg):
self.label.setText(msg)
self.dialog.close()
if __name__ == '__main__':
app = qtw.QApplication(sys.argv);
window = MainWindow()
sys.exit(app.exec())