[PyQt] Populate Model data in a separate thread (Demetrius Cassidy)

Demetrius Cassidy dcassidy36 at mass.rr.com
Tue Apr 28 01:07:30 BST 2009

I suggest you read over http://doc.trolltech.com/4.5/qthread.html, and while
the examples are in C++ don't look too much into the syntax differences but
rather what it's doing.

Although I haven't used QThread before, I have used python's threading
classes. My understanding is that you create a QThread derived class, which
defines a run() method. In the run method is where you do your blocking
calls (i.e. talk to the database).

You are basically going to have your model defer the loading of your data to
your thread class. You need to have a method in your model call the thread's
start() method, and connect to the thread's finished signal.

Note that you can not call a widget from your data thread. #1 rule of a data
thread is that it should NEVER directly or indirectly touch a GUI thread.
Very bad things can happen if you do so.

class MyThread(QThread):
   def __init__(self, parent=None):
       QThread.__init__(self, parent)

  def run(self):
     callDatabaseBlockingMethod() #do something with the data
     self.exec() #start the event loop

class MyModel(QAbstractTableModel):
   def __init__(self, parent=None):
        QAbstractTableModel.__init__(self, parent)
        myDataThread = MyThread(parent)

  def loadData(self):
      self.beginInsertRows(firstRow, rowCount, QModelIndex())
      self.myDataThread.start() #this slot will call the run() method on the

  def _finished(self):
      #do something with data from myDataThread


Edwin Marshall-2 wrote:
> First of all, I am subscribed to the digest version of the mailing list,
> so I'm not 100% sure how to reply to specific topics, sorry.
> Demetrius, my model currently contains approx. 1,800 records, and will
> eventually contain about 4,400 records, each with 11 columns, one of which
> displays a pixmap.
> In my model's __init__() method, I load the data from a sqlite table using
> python's native sqlite3 API (QTSql was way too slow, which is why I can't
> use QSQLRelationalTableModel). In the rowCount() method, I return the
> number of records in my database table, and in the columnCount() method I
> return the number of columns. The data method simply maps an index.row()
> and index.column() to the same row and column in the database.
> I would like to defer the loading of the model's data to a thread so that
> the user doesn't experience delay when he opens my program. Ideally, the
> program's window would immediately be shown with an empty table view, and
> once my model was done getting it's data, would populate the table view.
> If I understood you correctly, you are suggesting that I implement my
> model as an editable one, then have a method that is run in a thread that
> populates it using its setData() and insertRows() methods? While I think I
> have a grasp of writing editable models, I'm not sure I understand how to
> connect signals from a qthread to that of my model. Could you elaborate,
> please?
> Thanks in advance,
> Edwin Marshall
>> -----Original Message-----
>> From: pyqt-request at riverbankcomputing.com
>> Sent: Mon, 27 Apr 2009 12:00:12 +0100
>> To: pyqt at riverbankcomputing.com
>> Subject: PyQt Digest, Vol 57, Issue 53
>> Send PyQt mailing list submissions to
>> 	pyqt at riverbankcomputing.com
>> To subscribe or unsubscribe via the World Wide Web, visit
>> 	http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>> or, via email, send a message with subject or body 'help' to
>> 	pyqt-request at riverbankcomputing.com
>> You can reach the person managing the list at
>> 	pyqt-owner at riverbankcomputing.com
>> When replying, please edit your Subject line so it is more specific
>> than "Re: Contents of PyQt digest..."
>> Today's Topics:
>>    1. pyrcc4 regression in snapshot-0425? (Grissiom)
>>    2. resource name problem in ui_* (Robert Norman)
>>    3. Re: Searching for a very small scprit using CLIPBOARD
>>       (Aron Bierbaum)
>>    4. Re: re[PyQt] source name problem in ui_* (Demetrius Cassidy)
>>    5. Re: RuntimeError: underlying C/C++ object has been deleted
>>       (Alexandr N Zamaraev)
>>    6. Re: RuntimeError: underlying C/C++ object has been deleted
>>       (Demetrius Cassidy)
>>    7. Re: Populate Model data in a separate thread (Demetrius Cassidy)
>>    8. Re: RuntimeError: underlying C/C++ object has been deleted
>>       (Alexandr N Zamaraev)
>> _______________________________________________
>> PyQt mailing list
>> PyQt at riverbankcomputing.com
>> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
> ____________________________________________________________
> Receive Notifications of Incoming Messages
> Easily monitor multiple email accounts & access them with a click.
> Visit http://www.inbox.com/notifier and check it out!
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt

View this message in context: http://www.nabble.com/Re%3A-Re%3A-Populate-Model-data-in-a-separate-thread-%28Demetrius-Cassidy%29-tp23265634p23267744.html
Sent from the PyQt mailing list archive at Nabble.com.

More information about the PyQt mailing list