[PyQt] doubt with QtCore.QThread

Mark Summerfield mark at qtrac.eu
Tue Oct 2 16:15:06 BST 2007


On 2007-10-02, Pradnyesh Sawant wrote:
> Hello,
> I have this code, which seems to be correct, but will not run :((
> ##################################################
> import sys
> from PyQt4 import QtCore, QtGui
> class Tmp(QtGui.QDialog, QtCore.QThread):
>     def __init__(self, parent = None):
>         QtGui.QDialog.__init__(self, parent)
>         QtCore.QThread.__init__(self)
>     def run(self):
>         print 'Test inside run :)'####
>
> if __name__ == '__main__':
>     app = QtGui.QApplication(sys.argv)
>     t = Tmp()
>     t.show()
>     t.start()
>     sys.exit(app.exec_())
> ##################################################
> the error it gives is:
> AttributeError: start
> although, 'print dir(t)' shows start to be an element of t

I think there's something wrong with the approach here.

In PyQt, there is just one GUI thread, but it looks to me like you are
trying to have two GUI threads, the first (and should be only) one that
begins with QApplication, and then a second one that you try to begin
with your Tmp() instance.

I'd try separating out into two classes, perhaps like this:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Worker(QThread):
    def __init__(self):
        QThread.__init__(self)
    def run(self):
        print 'Test inside run :)'####

class Tmp(QDialog):
    def __init__(self, parent=None):
        QDialog.__init__(self, parent)
        self.worker = Worker()
        self.setWindowTitle("Tmp")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    t = Tmp()
    t.show()
    t.worker.start()
    sys.exit(app.exec_())

-- 
Mark Summerfield, Qtrac Ltd., www.qtrac.eu



More information about the PyQt mailing list