title: How can I query asynchronously via a QSqlQueryModel?
body: <p>I wish to query a SQL database via <a href="http://qt-project.org/doc/qt-5/QSqlQueryModel.html" rel="nofollow">QSqlQueryModel</a> (PyqQt 5/Qt 5.2) asynchronously, so that the GUI doesn't block. How can this be accomplished? Maybe through multithreading? Please provide code of how to do this. If using QSqlQueryModel asynchronously isn't practical, feel free to provide alternatives (should be usable with QTableView though).</p>
  
  <p>My (synchronous) code currently looks as shown beneath. The main script bin/app.py loads gui/__init__.py and executes its <code>main</code> method. That in turn uses <code>gui.models.Table</code> to load data from the database. The problem is that <code>gui.models.Table</code> queries the database synchronously and locks up the GUI in the meantime.</p>
  
  <h1>bin/app.py:</h1>
  
  <pre><code>import os.path
  import sys
  
  sys.path.insert(0, os.path.abspath(os.path.join(
      os.path.dirname(__file__), "..")))
  
  import gui
  
  
  if __name__ == "__main__":
      gui.main()
  </code></pre>
  
  <h1>gui/<strong>__<em>init</em>__</strong>.py:</h1>
  
  <pre><code>import sys
  import os.path
  from PyQt5 import uic
  from PyQt5 import QtCore, QtWidgets
  
  from gui import models
  
  
  class MainWindow(QtWidgets.QMainWindow):
      def __init__(self):
          super(MainWindow, self).__init__()
          uic.loadUi(os.path.join(os.path.dirname(__file__), 'app.ui'), self)
          self.tableView.setModel(models.Table(self))
  
  
  def main():
      app = QtWidgets.QApplication(sys.argv)
      w = MainWindow()
      w.show()
      app.exec_()
  </code></pre>
  
  <h1>gui/models.py:</h1>
  
  <pre><code>import os.path
  from PyQt5.QtCore import *
  from PyQt5.QtSql import *
  
  
  class Table(QSqlQueryModel):
      def __init__(self, parent=None):
          super(Table, self).__init__(parent)
  
          pth = os.path.abspath(os.path.join(os.path.dirname(__file__), "..",
                                             "test.sqlite"))
          db = QSqlDatabase.addDatabase("QSQLITE")
          db.setDatabaseName(pth)
          if not db.open():
              raise Exception("Couldn't open database '{}'".format(pth))
          try:
              self.setQuery("select * from Test")
          finally:
              db.close()
  </code></pre>