Results 1 to 6 of 6

Thread: Problem: QThead and QTableview

  1. #1
    Join Date
    Sep 2006
    Location
    Rio de Janeiro, Brazil
    Posts
    44
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11

    Default Problem: QThead and QTableview

    Hello Friends,

    I am having problems of communication between Thread and my MainWindowForm.

    Inside the Thread I run a method for upgrading the QTableView, I am getting more errors.

    Here Class Header:

    Qt Code:
    1. class ThreadNotify : public QThread
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. ThreadNotify(QObject *parent = 0);
    7. virtual ~ThreadNotify();
    8.  
    9. void setCapsule(MainWindowForm *pointMainCapsule);
    10.  
    11. MainWindowForm *accessMainCapsule;
    12.  
    13. void setMessage(const QString &message);
    14. void stop();
    15.  
    16. signals:
    17.  
    18. void transactionStarted(const QString &message);
    19.  
    20. ....
    21.  
    22. protected:
    23.  
    24. void run();
    25. ....
    26. };
    27.  
    28. class MainWindowForm : public QMainWindow, public Ui::MainWindowForm
    29. {
    30. Q_OBJECT
    31.  
    32. public:
    33.  
    34. MainWindowForm(QWidget *parent = 0);
    35. virtual ~MainWindowForm();
    36.  
    37. ThreadNotify instanceThreadNotify;
    38. QSqlQueryModel *model;
    39.  
    40. void modelRefresh();
    41. ...
    42. }
    To copy to clipboard, switch view to plain text mode 

    Here Code:

    Qt Code:
    1. ThreadNotify::ThreadNotify(QObject *parent)
    2. : QThread (parent)
    3. {
    4. stopped = false;
    5. }
    6.  
    7. ThreadNotify::~ThreadNotify()
    8. {
    9. /** Destroys the object and frees any allocated resources */
    10. }
    11.  
    12. void ThreadNotify::setCapsule(MainWindowForm *pointMainCapsule)
    13. {
    14. accessMainCapsule = pointMainCapsule;
    15. }
    16.  
    17. ....
    18.  
    19. void ThreadNotify::run()
    20. {
    21. while (!stopped == nnotifies < N_NOTIFICATIONS)
    22. {
    23. int sock;
    24. fd_set input_mask;
    25.  
    26. sock = PQsocket(conn);
    27.  
    28. if (sock < 0)
    29. break;
    30.  
    31. FD_ZERO(&input_mask);
    32. FD_SET(sock, &input_mask);
    33.  
    34. if (select(sock + 1, &input_mask, NULL, NULL, NULL) < 0)
    35. {
    36. (void)exit_nicely(conn);
    37. }
    38.  
    39. PQconsumeInput(conn);
    40.  
    41. while ((notify = PQnotifies(conn)) != NULL)
    42. {
    43. qApp->lock();
    44.  
    45. accessMainCapsule->modelRefresh();
    46.  
    47. qApp->unlock();
    48. PQfreemem(notify);
    49. nnotifies++;
    50. }
    51. }
    52.  
    53. PQfinish(conn);
    54. stopped = false;
    55. }
    56.  
    57. ...
    58.  
    59. MainWindowForm::MainWindowForm(QWidget *parent)
    60. : QMainWindow(parent)
    61. {
    62. ...
    63. }
    64.  
    65. void MainWindowForm::modelRefresh()
    66. {
    67. QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
    68.  
    69. model->setQuery(QString("SELECT ....");
    70.  
    71. if (model->lastError().isValid())
    72. {
    73. qDebug() << model->lastError();
    74. }
    75.  
    76. QApplication::restoreOverrideCursor();
    77. }
    78.  
    79. ...
    To copy to clipboard, switch view to plain text mode 

    When I receive the notification of PostgreSQL running on the Thread, the method of class MainWindowForm is triggered.

    More get the following error:

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

    Notify ==> ASYNC NOTIFY trigger 'nalertupdatetab' backend pid 21611
    =======ERROR====================================

    I used something similar in Qt3 with QDataTable, and not had that problem, someone could help me with that?

    Thanks, edm.

  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: Problem: QThead and QTableview

    You can type in
    Qt Code:
    1. qRegisterMetaType<QModelIndex>("QModelIndex");
    To copy to clipboard, switch view to plain text mode 
    in main(). But I wouldn't do that, the index may become invalid before the slot is triggered. Why don't you use the PostgreSQL driver that comes with Qt? You could avoid the worker thread and have a nice object oriented interface to your database.

    This might be interesting for you as well:
    http://labs.trolltech.com/blogs/2007...notifications/

  3. #3
    Join Date
    Sep 2006
    Location
    Rio de Janeiro, Brazil
    Posts
    44
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Problem: QThead and QTableview

    Hummm... ok,

    Interestingly the new functionality of Qt 4.4, more is still in the snapshot.

    One question, with subscribeToNotification() I will not need to use the
    most Thread?

    Thanks, edm

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problem: QThead and QTableview

    edm, I find this thread confusing because you have marked Qt3 alone as used products. Furthermore, QApplication::lock() / QApplication::unlock() only exist in Qt3, whereas QModelIndex only exists in Qt4.
    J-P Nurmi

  5. #5
    Join Date
    Aug 2006
    Posts
    44
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem: QThead and QTableview

    The gotcha here (and it's subtle) is that down in the bowels of all this, there's a signal / slot connection that fails because, as you saw, QModelIndex can't be part of an async connection. Since a model index is not 'safe' to post across threads, this limitation exists with good reason. Trying to register QModelIndex as a meta type is doomed to fail.

    So, what can you do? Well, first, a question: Must the update be done synchronously? If not, I suggest posting a custom event over to your main window, and have the handler for the custom event execute the refresh.

    If it needs to be synchronous, you can create a custom event, post it, and wait for it to finish before posting again. If you're worried about flooding the queue with posted events, you can always employ the various mechanisms Qt has to prevent this kind of thing.

    In our apps, we've had to deal with this problem. What we found was that finding out / understanding what was happening and why was way harder than 'fixing' the problem. (Surprising how often that's the case. ;D ).

    Good luck!

  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: Problem: QThead and QTableview

    Quote Originally Posted by ederbs View Post
    One question, with subscribeToNotification() I will not need to use the
    most Thread?
    Right, no extra threads needed.

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.