[PyQt] QThread and qRegisterMetaType

Janwillem van Dijk jwevandijk at xs4all.nl
Sun Oct 17 18:04:16 BST 2010

I want the output of a function that runs in a thread to be shown in a 
QPlainTextEdit on the main window. Now this crashes with a message that 
I sould use qRegisterMetaType. There are dozens of entries on the web on 
this but I could not find any that tells how to. At least not how to in 

My handicap is that I have not too much experience with oo-programming 
and none at all with c++. Thus suggestions like "call 
qRegisterMetaType() to register the data type before you establish the 
connection." are of no help and I have no idea what to make of things 
like "qRegisterMetaType<VolumeType>( "VolumeType" );"

Please can someone tell me what to do with the small example below to 
get it working.

Small test application for threaded output to a QPlainTextEdit.
The main window consists of three buttons and the QPlainTextEdit
- pressButtonInsert clicked is connected to slotInsert: append text to 
the edit widget
- pressButtonThread clicked is connected to slotInsertThreaded:
     append text to edit widget from within a separate thread
- pressButtonClear clicked is connected to slotClear: clear the edit widget
import sys, time
from PyQt4 import QtCore, QtGui
from qPlainTextEditGui import Ui_Form #the main form containing the 4 
class MyForm(QtGui.QMainWindow):
     def __init__(self, parent=None):
         QtGui.QWidget.__init__(self, parent)
         self.ui = Ui_Form()
     def slotClear(self):
         print('ButtonClear triggered')
     def slotInsert(self):
         print('ButtonInsert triggered')
     def slotInsertThreaded(self):
         print('ButtonTreaded triggered')
         self.work = work_thread(self.ui.plainTextEdit)

class work_thread(QtCore.QThread):
     After http://joplaete.wordpress.com/2010/07/21/threading-with-pyqt4/
     def __init__(self, edit, parent=None):
         QtCore.QThread.__init__(self, parent)
         self.edit = edit
     def run(self):

def append_to_edit(edit):
     for i in range(5):
         edit.appendPlainText('line %d' % i)

if __name__ == "__main__":
     app = QtGui.QApplication(sys.argv)
     myapp = MyForm()

Now it works fine when pressButtonInsert is triggered but returns this 
when pressButtonThread is triggered:
QObject::connect: Cannot queue arguments of type 'QTextBlock'
(Make sure 'QTextBlock' is registered using qRegisterMetaType().)
QObject::connect: Cannot queue arguments of type 'QTextCursor'
(Make sure 'QTextCursor' is registered using qRegisterMetaType().)

Many thanks for helping me out,

More information about the PyQt mailing list