Issue
I can't figure out why the signals don't work. In PyQt5, this code worked (the difference was that instead of Signal, it was pyqtSignal).
When you click on the button, TextEdit should display the message "connecting to the device", if you replace pyside with pyqt, the code will work as it should
import sys
from PySide6.QtCore import *
from PySide6.QtWidgets import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
if not MainWindow.objectName():
MainWindow.setObjectName(u"MainWindow")
MainWindow.resize(188, 267)
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName(u"centralwidget")
self.pushButton = QPushButton(self.centralwidget)
self.pushButton.setObjectName(u"pushButton")
self.pushButton.setGeometry(QRect(50, 140, 75, 24))
self.textEdit = QTextEdit(self.centralwidget)
self.textEdit.setObjectName(u"textEdit")
self.textEdit.setGeometry(QRect(30, 40, 104, 71))
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
self.pushButton.setText(QCoreApplication.translate("MainWindow", u"PushButton", None))
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self, parent=None)
self.dragPos = None
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.show()
def update_text(value, textEdit):
textEdit.setText(textEdit.toPlainText() + value)
textEdit.verticalScrollBar().setValue(textEdit.verticalScrollBar().maximum())
class account(QThread):
textUpdate = Signal(str, QTextEdit)
def __init__(self):
super().__init__(parent=None)
self.textUpdate.connect(update_text)
def run(self):
print("thread is work")
self.textUpdate.emit("Connect to device\n", ui.textEdit)
if __name__ == "__main__":
app = QApplication()
acc_instance = account()
main = MainWindow()
ui = main.ui
ui.pushButton.clicked.connect(acc_instance.start)
sys.exit(app.exec_())
P.S. I know that override run method is incorrect.
P.S.S Added a small example
Solution
Your code has several problems:
The scope of "ui" is limited, it is not a global variable so you cannot use it in the run method.
Only some data types are registered (which can only be done in C++) so that they can be sent through signals, and that is not the case with QTextEdit. A workaround is to look for a parent class of the QTextEdit that is registered as a QObject or an object.
But in this case I do not see the need to send QTextEdit but only the data and then modify the GUI to set it since that is its task.
class MainWindow(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
@Slot(str)
def update_text(self, value):
self.ui.textEdit.setText(self.ui.textEdit.toPlainText() + value)
self.ui.textEdit.verticalScrollBar().setValue(
self.ui.textEdit.verticalScrollBar().maximum()
)
class Account(QThread):
textUpdate = Signal(str)
def run(self):
print("thread is work")
self.textUpdate.emit("Connect to device\n")
if __name__ == "__main__":
app = QApplication()
main = MainWindow()
acc_instance = Account()
acc_instance.textUpdate.connect(main.update_text)
main.ui.pushButton.clicked.connect(acc_instance.start)
main.show()
sys.exit(app.exec_())
Note: In pyqt6 your initial code doesn't work either.
If you want to send texts to several QTextEdit then it is better to create a key that associates each type of text to a QTextEdit group:
from collections import defaultdict
from functools import cached_property
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self, parent=None)
self.dragPos = None
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.show()
self.register("device_viewer", self.ui.textEdit)
# self.register("another_key", another_textedit)
def register(self, key, textedit):
if not isinstance(textedit, QTextEdit):
raise TypeError(f"{textedit} must be a QTextEdit")
self.registry_viewers[key].append(textedit)
@cached_property
def registry_viewers(self):
return defaultdict(list)
@Slot(str, str)
def update_text(self, key, value):
for textedit in self.registry_viewers[key]:
textedit.setText(textedit.toPlainText() + value)
textedit.verticalScrollBar().setValue(
textedit.verticalScrollBar().maximum()
)
class Account(QThread):
textUpdate = Signal(str, str)
def run(self):
print("thread is work")
self.textUpdate.emit("device_viewer", "Connect to device\n")
# self.textUpdate.emit("another_key", "message")
if __name__ == "__main__":
app = QApplication()
main = MainWindow()
acc_instance = Account()
acc_instance.textUpdate.connect(main.update_text)
main.ui.pushButton.clicked.connect(acc_instance.start)
sys.exit(app.exec_())
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.