[PyQt] Segmentation fault

Matt Smith melkor at orangepalantir.org
Sun Jan 17 13:25:05 GMT 2010


Hao

I don't get a segmentation fault, I get a different error:
QObject::connect: Cannot queue arguments of type 'QTextCursor'
(Make sure 'QTextCursor' is registered using qRegisterMetaType().)

  But I would suggest using a QThread instead of a Threading.thread, and
then you can connect them with signals.  I would guess that you are
getting your error from the:

self.box.append(line)

Try to do all of your gui work on the main thread. So try to emit a
signal, connect that signal to a slot on your main class and update the
text box there.  I have changed the example you sent to do that and I
don't get any errors.



import time, sys, os, subprocess, select, threading
from PyQt4 import QtCore, QtGui

class TestGUI(QtGui.QWidget):
  def __init__(self, parent = None):
    QtGui.QWidget.__init__(self, parent)
    self.Widgets()
    self.Connections()

  def Widgets(self):
    self.txtBox = QtGui.QTextEdit()
    self.btnStart = QtGui.QPushButton("Start Thread")
    self.txtBox.setReadOnly(True)

    hb = QtGui.QHBoxLayout()
    hb.addWidget(self.txtBox)
    hb.addWidget(self.btnStart)

    self.setLayout(hb)

  def Connections(self):
    self.connect(self.btnStart, QtCore.SIGNAL("clicked()"),
self.startThread)

  def startThread(self):
    self.thread = TestThread(self, cmd = ["ls", "/home/hao"], box =
self.txtBox)
    self.thread.progress.connect(self.showID)
    self.thread.start()
    self.btnStart.setDisabled(True)
  @QtCore.pyqtSlot("QString")
  def showID(self, s):
    self.txtBox.append(s);
    
    
class TestThread(QtCore.QThread):
  progress = QtCore.pyqtSignal("QString")
  def __init__(self, parent, cmd = None,  box = None):
    QtCore.QThread.__init__(self)
    self.cmd = cmd
    self.box = box

  def run(self):
    self.prog = subprocess.Popen(self.cmd, bufsize = 1, stdout =
subprocess.PIPE, stderr = subprocess.STDOUT)
    self.rFile = select.select([self.prog.stdout], [], [], 3600)
    line = ""

    for line in self.rFile[0][0]:
      self.progress.emit(line)

if __name__ == "__main__":
  app = QtGui.QApplication(sys.argv)
  t = TestGUI()
  t.show()
  sys.exit(app.exec_()) 



More information about the PyQt mailing list