Thread operation question

Maurizio Berti maurizio.berti at gmail.com
Fri Jul 4 21:29:36 BST 2025


Il giorno ven 4 lug 2025 alle ore 17:09 Luca Bertolotti <
luca72.bertolotti at gmail.com> ha scritto:

> Sorry i have calle thread.start ,but i never copy tho the original message
>
> i can do as follow:
>
> Class Read(QObject):
>     letto = pyqtSignal(str)
>     finished = pyqtSignal(str)
>     def __init__(self, parent=None):
>         super().__init__(parent=None)
>
>         self.timer_leggo_x_y =QTimer()
>         self.timer_leggo_x_y.timeout.connect(self.leggo_x_y)
>         self.timer_leggo_x_y.start(250)
>      def run(self):
>        variable = self.reader_v.read()
>        self.letto.emit(variable)
>        self.finished.emit()
>
>
>
> class Form(QWidget, Ui_Form):
>     """
>     Class documentation goes here.
>     """
>
>     def __init__(self, parent=None):
>         """
>         Constructor
>
>         @param parent reference to the parent widget (defaults to None)
>         @type QWidget (optional)
>         """
>          super().__init__(parent)
>         self.setupUi(self)
>         self.tableWidget.setRowCount(200)...........
>         self.reader = .............
>         self.read_pos = Read()
>         self.read_pos_thread = QThread()
>         self.read_pos.moveToThread(self.read_pos_thread)
>         self.read_pos.reader_v = self.reader
>         self.read_pos_thread.started.connect(self.read_pos.run)
>         self.read_pos.letto.connect(self.scrivo_x_y)
>         self.read_pos.finished.connect(self.reset)
>         self.read_pos_thread.start()
>
>     def scrivo_x_y(self, variable):
>          self.lineEdit.setText(variable)
>
>     def reset(self):
>           self.read_pos_thread.start()
>

Sorry but your update is still not appropriate: the QTimer is connected to
a function that doesn't exist ("leggo_x_y" is now gone), and we still don't
know the actual problem you have. Does the code run or not? If it does run,
what *demonstrable* problems are you facing? Where is the code for that
"reader"? Do you get any debug output or relevant crash traceback? Can you
explain your need for using threading (eg: the "Servodrive" takes too much
time to reply)?

Multithreading and dealing with external I/O may be quite complex, we
cannot help you with vague explanations and incomplete/partial code.
Please take your time to carefully explain your context, the reasoning
behind your choices (threading), what actual problems you have with your
current attempts, and include relevant debug output (also, try to run the
code in a terminal or prompt if you're using an IDE). Finally, ensure you
provide an appropriate and comprehensive minimal reproducible example with
proper syntax and indentation.

Regards,
MaurizioB

PS: having a "parent" argument in the "def __init__()" of the subclass is
quite meaningless if you then call super().__init__(parent=None). If you
don't want the QObject instance to have a parent (which makes sense, if
you're going to move it into another thread), remove that keyword argument
from both of them, as they're useless and redundant; if you still need a
reference to "some parent", you could just call super().__init__() without
the "parent=None" keyword (as it's implicit) and eventually create an
instance attribute for that parent if you need that reference, but be aware
that if that parent isn't a thread-safe object (eg: a widget) you shall
*never* try to access it from that thread (anything directly called within
the "run()" call).
-- 
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20250704/fc315ca4/attachment.htm>


More information about the PyQt mailing list