0

The aim is to run two separate threads in pyQT after a flag has been set. The inside's of the while loop is not of concern. The program seems to run quite okay for a couple of seconds until it decides to crash, I'm overlooking something but i can't see it.

import sys
import threading
import serial
import time
import math
from   PyQt5.QtCore    import pyqtSlot
from   PyQt5.QtWidgets import QApplication, QDialog, QMainWindow, QMessageBox
from   PyQt5.uic       import loadUi


class Application( QMainWindow ):
    def __init__( self ):
        super(Application, self).__init__()
        loadUi('unitTest.ui', self)
        self.flag = False
        self.serialPort = serial.Serial()

    def run( self ):
        self.prcssOne.clicked.connect( self.startThread1 )
        self.prcssTwo.clicked.connect( self.startThread2 )
        self.startThreadBtn.clicked.connect( self.changeState )

    def changeState(self):
        self.flag = True
        self.serialPort = serial.Serial('COM41', 115200, timeout = 1)
        print ("Flag has been set.")


    def startThread1(self):
        if not self.flag:
            print ("Set Flag and try..")
            return 
        self.t1 = threading.Thread(target = self.loop1)
        self.t1.daemon = True
        self.t1.start()

    def startThread2(self):
        if not self.flag:
            print ("Set flag and try...")
            return
        self.t2 = threading.Thread(target = self.loop2)
        self.t2.deamon = True
        self.t2.start()

    def loop2(self):
        while(1):
            incoming = str(self.serialPort.readline())
            if not incoming:
                print ("no data yet")
            else:
                length = len(incoming)
                incoming = incoming[2:length-5]
                self.feed2.append(str(incoming))
            time.sleep(0.1)

    def loop1(self):
        while(1):
            self.feed1.append("hey")
            time.sleep(1)

def main():
    app = QApplication(sys.argv)
    widget = Application()
    widget.show()   
    widget.run()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

The code mentioned above is a sample piece of code used to recreate the problem in the original code, since the original is way too big for the post, but there aren't any other events adding to the problem.

After a few seconds of running, the app closes and this is the message that comes up.

Flag has been set.
QObject::connect: Cannot queue arguments of type 'QTextCursor'
(Make sure 'QTextCursor' is registered using qRegisterMetaType().)
[Finished in 7.9s]

I'm trying to figure out what the problem could be, suggestions are welcome.

  • Do not use `append()` directly since you are updating the GUI from another thread that in Qt is forbidden, an option is the one that indicates in the answer of the duplicated question, another option is to use `QMetaObject::invokeMethod()` changing to QtCore. `QMetaObject.invokeMethod(self.feed2, "append", QtCore.Qt.QueuedConnection, QtCore.Q_ARG(str, str(incoming)))` – eyllanesc Oct 14 '18 at 16:39
  • another option more of the style of Qt is to use QSerialPort that allows you to give the data through signaling without the need of threads so you will not have that problem anymore. – eyllanesc Oct 14 '18 at 16:43
  • Thanks for the comment @eyllanesc. What are the required imports necessary for the QMetaObject? what does the line do? –  Oct 16 '18 at 09:35

0 Answers0