5

在 GUI 编程方面,我几乎是个初学者。我将 QT 与 python 绑定(PyQT4)结合使用。

我正在尝试做的事情:

  • 使用 pyserial设置QThread读取和写入串行端口。
  • 主应用程序应该能够通过信号向正在运行的QThread. 并通过信号接收串行数据 QThread

我基于此代码(链接)开始了我自己的测试实现。在此之前,我阅读了有关QThreads 的基础知识并试图了解它们的用途。以下测试代码是我想出的。对不起,我尽量保持最小,但它仍然是 75 行代码:

from PyQt4 import QtCore, QtGui
import time
import sys

class SerialData(QtCore.QObject):
    def __init__(self, message):
        super(SerialData, self).__init__()
        self.__m = message
    def getMsg(self):
        return self.__m

class SerialCon(QtCore.QObject):

    finished = QtCore.pyqtSignal()
    received = QtCore.pyqtSignal(SerialData)

    def init(self):
       super(SerialCon, self).__init__()
       # TODO setup serial connection:
       # setting up a timer to check periodically for new received serial data
       self.timer = QtCore.QTimer()
       self.timer.setInterval(400)
       self.timer.timeout.connect(self.readData)
       self.timer.start(200)
       # self.finished.emit()

    def readData(self):
       self.received.emit(SerialData("New serial data!"))
       print "-> serial.readLine() ..."

    @QtCore.pyqtSlot(SerialData)
    def writeData(self, data):
       print "-> serial.write(), ", data.getMsg()


class MyGui(QtGui.QWidget):
    serialWrite = QtCore.pyqtSignal(SerialData)

    def __init__(self):
       super(MyGui, self).__init__()
       self.initUI()

    def initUI(self):
       bSend = QtGui.QPushButton("Send",self)
       bSend.clicked.connect(self.sendData)
       self.show()

    @QtCore.pyqtSlot(SerialData)
    def updateData(self, data):
        print "Gui:", data.getMsg()

    def sendData(self, pressed):
       data = SerialData("Send me!")
       self.serialWrite.emit(data)

def usingMoveToThread():
    app = QtGui.QApplication(sys.argv)
    guui = MyGui()

    thread = QtCore.QThread()
    serialc = SerialCon()
    serialc.moveToThread(thread)

    # connecting signals to slots
    serialc.finished.connect(thread.quit)
    guui.serialWrite.connect(serialc.writeData)
    serialc.received.connect(guui.updateData)
    thread.started.connect(serialc.init)
    thread.finished.connect(app.exit)
    thread.start()

    sys.exit(app.exec_())

if __name__ == "__main__":
    usingMoveToThread()

我的问题:

  • 在测试代​​码中,从SerialCon对象(已移动到QThread)发出的信号似乎没有被相应的插槽(in MyGuiupdateData)接收

  • 迟早,正在运行的测试代码总是会导致Segmentation fault (core dumped). 这让我相信我错过了一些重要的部分。

什么可能导致这种情况?

也许我采取了完全错误的方法?- 因此,如果您对如何实现这一目标有更好的了解,我将非常感激听到它!

非常感谢!

4

1 回答 1

1

起初我只关注新的方式,从 QT4 (链接QThreads)开始应该如何使用,通过创建一个,然后调用,就像在我的第一个代码示例中一样(至少我是这么理解的)。但是我只是想不通,为什么我无法将信号从 主应用程序传递。QObjectmoveToThread()QThread

由于我真的需要快速解决我的问题,我拼命尝试了各种事情。这是第二个代码,它似乎确实按我想要的方式工作:

from PyQt4 import QtCore, QtGui
import time
import sys
import math


class SerialCon(QtCore.QThread):

    received = QtCore.pyqtSignal(object)

    def __init__(self, parent=None):
        QtCore.QThread.__init__(self)
        # specify thread context for signals and slots:
        # test: comment following line, and run again
        self.moveToThread(self)
        # timer:
        self.timer = QtCore.QTimer()
        self.timer.moveToThread(self)
        self.timer.setInterval(800)
        self.timer.timeout.connect(self.readData)

    def run(self):
        self.timer.start()
        #start eventloop
        self.exec_()

    def readData(self):
        # keeping the thread busy
        # figure out if the GUI remains responsive (should be running on a different thread)
        result = []
        for i in range(1,1000000):
            result.append(math.pow(i,0.2)*math.pow(i,0.1)*math.pow(i,0.3))
        #
        self.received.emit("New serial data!")

    @QtCore.pyqtSlot(object)
    def writeData(self, data):
       #print(self.currentThreadId())
       print(data)

class MyGui(QtGui.QWidget):
    serialWrite = QtCore.pyqtSignal(object)

    def __init__(self, app, parent=None):
       self.app = app
       super(MyGui, self).__init__(parent)
       self.initUI()

    def initUI(self):
       self.bSend = QtGui.QPushButton("Send",self)
       self.bSend.clicked.connect(self.sendData)
       self.show()
    def closeEvent(self, event):
        print("Close.")
        self.serialc.quit();

    @QtCore.pyqtSlot(object)
    def updateData(self, data):
        print(data)

    def sendData(self, pressed):
       self.serialWrite.emit("Send Me! Please?")

    def usingMoveToThread(self):
        self.serialc = SerialCon()
        # binding signals:
        self.serialc.received.connect(self.updateData)
        self.serialWrite.connect(self.serialc.writeData)
        # start thread
        self.serialc.start()

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    guui = MyGui(app)
    guui.usingMoveToThread()
    sys.exit(app.exec_())

我现在认为这是一种解决方法,但它并不能真正回答我的问题。另外,正如之前链接的 QT 博客条目中所提到的,这并不是使用QThread. 所以我仍然想知道如何让我的问题中的第一个代码按预期工作。如果您对我的第一个代码有什么问题有一些想法,请告诉我!

于 2013-08-23T12:01:45.520 回答