[PySide] QThread crash before emitting finished signal
william.dias at gmail.com
Thu Jun 7 22:52:44 CEST 2012
Ok. I've just seen that I can't do a moveToThread in a object that has a
parent. That's the first point I've fixed in my code.
Apart from that, my threads are being destroyed as photoWorker and
photoThread objects run out of scope. How can I prevent python from doing
this? Making them instance objects is not an option since every time my
program reenters the method where they are declared it spawns new threads.
2012/6/7 William Dias <william.dias at gmail.com>
> Thank you, Sebastian. I followed the procedures described in the document
> and it worked. But I have another question.
> Take a look at the code bellow:
> class PhotoWorker(QObject):
> photoStatus = Signal(str, str)
> def work(self):
> self.db = sqlite_database.SQLiteDatabase("db1")
> self.timer = QTimer()
> logger.info("photo worker started")
> def __resendPhoto(self):
> data = self.db.selectPhoto()
> if data != None:
> self.filename = data
> self.tags = data
> logger.info("trying to resend photo " + self.filename)
> status = sendPhoto(self.tags, self.filename)
> self.photoStatus.emit(self.filename, status)
> And in a GUI class, I do the following:
> photoWorker = http_connection.PhotoWorker(self)
> photoThread = QThread()
> QMetaObject.invokeMethod(photoWorker, "work", Qt.QueuedConnection)
> I realized that when I create the photoWorker object, if I don't set its
> parent to self, my thread dies. Is this because my photoWorker doesn't have
> its own event loop? I am bit confused here. How should I proceed?
> Thank you.
> 2012/6/7 Sebastian Elner <sebastian at risefx.com>
>> Although it is in the documentation, QThread is not meant to be used
>> this way (subclassing). Have a look at this:
>> http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/ It helped me a
>> lot. Also a complete stripped down example would greatly help in a case of
>> a segfault.
>> On 06/07/2012 09:59 AM, William Dias wrote:
>> I have a subclass of QThread which uploads pictures to a webserver. For
>> each new picture created in the main thread, the program spawns a new
>> thread to handle the job. I've override the __init__ method to be able to
>> pass some parameters to the thread object, as you can see bellow.
>> class PhotoConnection(QThread):
>>> photoStatus = Signal(str, str)
>> def __init__(self, tags, filename, parent=None):
>>> QThread.__init__(self, parent)
>>> self.tags = tags
>>> self.filename = filename
>> def run(self):
>>> status = sendPhoto(self.tags, self.filename)
>>> self.photoStatus.emit(self.filename, status)
>> In the GUI thread, the method responsible dor creating the threads is
>> the following:
>> def sendPhoto(self):
>>> photoConnection = PhotoConnection(self.tags_to_string, self.filename,
>> In the PhotoConnection, if I don't set the parent of my Qthread object
>> to self (for example, if I set to None) or if I don't override the __init__
>> method and create another method to pass the arguments, when the thread
>> finishes the execution I got a segfault.
>> Why is this happening? Can you help me?
>> PySide mailing listPySide at qt-project.orghttp://lists.qt-project.org/mailman/listinfo/pyside
>> Sebastian Elsner - pipeline td - r i s e | fx
>> t: +49 30 20180300 sebastian at risefx.com
>> r i s e | fx GmbH
>> Schlesische Strasse 28 Aufgang B, 10997 Berlin
>> Geschäftsführer: Sven Pannicke, Robert Pinnow
>> Handelsregister Berlin HRB 106667 B
>> PySide mailing list
>> PySide at qt-project.org
> William Marques Dias
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the PySide