Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

moving sql results using signals and slots



  • Hi,
    In my app I have a mysql class that runs in a dedicated Qthread, I want all the mysql operations to happen in this class and the results be sent through signals/slots to the main thread for displaying.
    I have a problem in sending QSqlQuery data through signals, if i do something like this in the mysql class:

    @qtc.pyqtSlot()
    def fetchAll(self):
        if(self.db.isOpen()):
            results = self.db.exec_("SELECT * FROM members")
            if not results.lastError().isValid():
                self.populate.emit(results)
    

    then on the receiving end I should define the slot like so:

    @qtc.pyqtSlot(QtSql.QSqlQuery)
    def populate(self,allMembers):
    

    to be able to iterate over the QSqlQuery and populate my widget with the data
    but this gives an error:

    QObject::connect: Cannot queue arguments of type 'QSqlQuery'
    (Make sure 'QSqlQuery' is registered using qRegisterMetaType().)
    

    I know i can convert the results to a pythonic datatype like a dict or a list in the db class which i can then send over the signal,but this will eat the memory if i have alot of rows with images or any other big data
    I'm pretty sure I'm doing this the wrong way, so what is the recommended way to move sql results using signals and slots?



  • @rhx9
    The answer at multithreaded QSqlQuery sums it up simply:

    QSqlQuery::prepare and QSqlQuery::bindValue methods cannot be called outside the thread created them. Hence there is no reason to pass it around in signal-slot.

    (For a big resultset, you might look at how QSqlQueryModel::fetchMore() works, but I'm not convinced that will help in your case.)

    Actually, there is a thread on this forum: QSqlQuery - execution in one thread and reading the results in another.. Also https://forum.qt.io/topic/52967/qsqlquery-exec-in-one-thread-read-result-in-another-is-it-legal


Log in to reply