Results 1 to 12 of 12

Thread: Sharing data across threads

  1. #1
    Join Date
    Apr 2008
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Sharing data across threads

    Hello,
    I am new to Qt and it could be that the answer to my question is really simple but still I can't find it. I have two threads in my application. One of them is gui thread and the other is database query thread. I have queued connections between these two and they work fine but I need to share data across them. I tried to use Q_DECLARE_METATYPE macro
    Qt Code:
    1. Q_DECLARE_METATYPE (QSqlQueryModel)
    To copy to clipboard, switch view to plain text mode 
    and I get the following error:
    Qt Code:
    1. /usr/local/Trolltech/Qt-4.3.4/include/QtCore/qabstractitemmodel.h: In copy constructor 'QSqlQueryModel::QSqlQueryModel(const QSqlQueryModel&)':
    2. /usr/local/Trolltech/Qt-4.3.4/include/QtSql/qsqlquerymodel.h:60: instantiated from 'void* qMetaTypeConstructHelper(const T*) [with T = QSqlQueryModel]'
    3. /usr/local/Trolltech/Qt-4.3.4/include/QtCore/qmetatype.h:151: instantiated from 'int qRegisterMetaType(const char*, T*) [with T = QSqlQueryModel]'
    4. database.h:62: instantiated from here
    5. /usr/local/Trolltech/Qt-4.3.4/include/QtCore/qabstractitemmodel.h:324: error: 'QAbstractTableModel::QAbstractTableModel(const QAbstractTableModel&)' is private
    6. /usr/local/Trolltech/Qt-4.3.4/include/QtSql/qsqlquerymodel.h:60: error: within this context
    7. /usr/local/Trolltech/Qt-4.3.4/include/QtCore/qmetatype.h: In function 'void* qMetaTypeConstructHelper(const T*) [with T = QSqlQueryModel]':
    8. /usr/local/Trolltech/Qt-4.3.4/include/QtCore/qmetatype.h:126: note: synthesized method 'QSqlQueryModel::QSqlQueryModel(const QSqlQueryModel&)' first required here
    To copy to clipboard, switch view to plain text mode 
    I know that later I need to use qRegisterMetatype function but I even didn't get that far.
    I am using linux/gcc with eclipse and Qt 4.3.
    Thank you in advance for any help you offer.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Sharing data across threads

    QSqlQueryModel already has a metatype declared. Whatever you are doing, it will probably not work as database connections can't be shared across threads. If you are trying to pass the model to another thread using signals and slots, it will not work too, because QObject subclasses can't be copied and that's required when using signal-slot connections across threads.

  3. #3
    Join Date
    Apr 2008
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sharing data across threads

    Ok, thank you for your reply. I have an application that manipulates database in one thread and post results in another thread (the gui thread). So basicly I execute a query in query thread and then I need to pass the result data to the gui thread. Do you have any idea how this might work? Thanks in advance.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Sharing data across threads

    Pass the data, not the model. You can either use signals and slots or events to do that. But why do you have to use a separate thread just to query the database for data? Can't you do that all in a single thread?

  5. #5
    Join Date
    Apr 2008
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sharing data across threads

    The database can get quite big and I don't want the application to hang while it executes queries. At the moment I am using signals and slots across threads with queued connection. I can send const QString &query_string from the gui thread to the query thread but I don't know how to send the queried data back. Can you tell me what data structure would be the best for this job? For now, I will try to send some test data across threads. Thanks in advance.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Sharing data across threads

    Quote Originally Posted by john_crichton View Post
    Can you tell me what data structure would be the best for this job?
    How about QSqlRecord or QVariantList?

  7. #7
    Join Date
    Apr 2008
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sharing data across threads

    I am trying to use QSqlRecord but I can't get it shared across threads. I try to use qRegisterMetaType
    Qt Code:
    1. qRegisterMetaType("My Type", QSqlRecord);
    To copy to clipboard, switch view to plain text mode 
    and I get the following error:
    Qt Code:
    1. main.cpp:30: error: expected primary-expression before ')' token
    To copy to clipboard, switch view to plain text mode 
    I am using linux/gcc with eclipse and Qt 4.3.
    Do you know what can be the problem. Thanks in advance.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Sharing data across threads

    It should be: qRegisterMetaType<QSqlRecord>("QSqlRecord")

  9. #9
    Join Date
    Apr 2008
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sharing data across threads

    Thank you. Now I can share QSqlRecord across threads. And now I have deeper understanding of how things are done in Qt and I think of Model/View architecture. Now, since models can't be shared across thread that means that I need to have one model in the gui thread. Then I could feed it with data from the query thread. Do you know how to feed the model with data? There is a method setData and I am trying to get it to work at the moment but I would appreciate any help you offer. Thanks in advance.

  10. #10
    Join Date
    Apr 2008
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sharing data across threads

    I found a solution. I send model pointer (QSqlQueryModel*) across thread.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Sharing data across threads

    Quote Originally Posted by john_crichton View Post
    Do you know how to feed the model with data? There is a method setData and I am trying to get it to work at the moment but I would appreciate any help you offer.
    You can't use setData directly from another thread - this method is not thread safe. You can use signals and slots for safe data transport.

    Quote Originally Posted by john_crichton View Post
    I found a solution. I send model pointer (QSqlQueryModel*) across thread.
    You're asking for trouble.

  12. #12
    Join Date
    Apr 2008
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sharing data across threads

    Hi, I know that it is unsafe to send pointers across threads. That is why I posted another question here http://www.qtcentre.org/forum/f-qt-p...ads-13358.html.
    Thank you in advance for any new help you offer.

Similar Threads

  1. Replies: 4
    Last Post: 19th October 2007, 20:47
  2. Data model
    By steg90 in forum Qt Programming
    Replies: 3
    Last Post: 17th September 2007, 13:14
  3. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 13:53
  4. How to convert binary data to hexadecimal data
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 8th March 2006, 17:17
  5. Replies: 16
    Last Post: 7th March 2006, 16:57

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.