Results 1 to 17 of 17

Thread: how to execute a QsqlQuery on a different thread ?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to execute a QsqlQuery on a different thread ?

    Clone the database connection, open it, run your query, close and remove the connection. Everything apart cloning has to be done from the thread that is to execute the query.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to execute a QsqlQuery on a different thread ?

    If you want to use signal/slots to communicate from the GUI to the worker thread then you'll need a QThread that runs an event loop.

    You start by creating a QObject subclass that has the receiver slot and that slot does the database query.

    You create an instance of QThread and an instance of this new class and then use moveToThread() to make it run in the worker thread.

    The slot then gets invoked in the context of the worker thread.

    Cheers
    _

  3. #3
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to execute a QsqlQuery on a different thread ?

    Quote Originally Posted by anda_skoa View Post
    If you want to use signal/slots to communicate from the GUI to the worker thread then you'll need a QThread that runs an event loop.

    You start by creating a QObject subclass that has the receiver slot and that slot does the database query.

    You create an instance of QThread and an instance of this new class and then use moveToThread() to make it run in the worker thread.

    The slot then gets invoked in the context of the worker thread.
    _
    Thanks for the reply
    my problem is that , i have a class the represents the GUI , a button in it, when pressed it opens a dialog
    now in this dialog i get the user input through line edit , and i have a button , when pressed i want to execute the query
    so should i connect to the database in the main GUI ? or in the class subcalssing QObject ?
    the other problem is that if i start the thread on pressing the button in the dialog , the thread will get destroyed before doing the query
    where to create the intsnance of the class subclassing QObject ?
    since my query takes parameters , i should pass them to the Thread. How to do that ? Signals and Slots are not doing the job properly in my case

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to execute a QsqlQuery on a different thread ?

    Quote Originally Posted by toufic.dbouk View Post
    so should i connect to the database in the main GUI ? or in the class subcalssing QObject ?
    I would do it in the qobject class, in the slot that executes the query or in a slot connected to the thread's started signal.

    Quote Originally Posted by toufic.dbouk View Post
    the other problem is that if i start the thread on pressing the button in the dialog , the thread will get destroyed before doing the query
    Why would it? The default implementation of QThread::run() runs an event loop. it doesn't finish until QThread::exit() is called.

    Quote Originally Posted by toufic.dbouk View Post
    where to create the intsnance of the class subclassing QObject ?
    When you create the thread. Since you want to directly connect the dialog to the query slot, before you create the dialog or even when you create the applicatIons' main GUI.

    Quote Originally Posted by toufic.dbouk View Post
    since my query takes parameters , i should pass them to the Thread. How to do that ? Signals and Slots are not doing the job properly in my case
    Signals can transport parameters to the slot.
    Does it work if you do not use a thread and have the query class instance in the main thread?

    Cheers,
    _

Similar Threads

  1. Replies: 2
    Last Post: 9th April 2013, 09:15
  2. Replies: 1
    Last Post: 25th October 2012, 15:10
  3. QSqlQuery does not execute
    By weixj2003ld in forum Qt Programming
    Replies: 7
    Last Post: 11th July 2012, 12:13
  4. Replies: 1
    Last Post: 18th July 2011, 12:12
  5. Replies: 16
    Last Post: 7th October 2009, 08:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.