Results 1 to 5 of 5

Thread: QSqlQueryModel data update

  1. #1
    Join Date
    Nov 2006
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QSqlQueryModel data update

    This question may have been answered somewhere else already, but I just can't seem to find the answer. I'm using the model/view programming to display a set of data from a sql database. I'm using the QSqlQueryModel to retrieve the data.

    The database is empty initially, therefore there is nothing to display in the beginning. As data is coming in, the view doesn't get updated. Btw, the data in the database is inserted by a different thread. I though by setting the model in the view object is enough, but apprently more is needed to be done.

    I tried to connect the model's dataChanged() signal to the view's update() or repaint() slot, but no dice. I explictly tested the model's dataChanged signal, and only to find it never sent the signal. I tried to run the another instance of the same program, and the 2nd instance did show the data, but new incoming data still doesn't get displayed.

    Isn't the model supposed to be aware of the status of the data it is monitoring? I tried a similar example with QDirModel, the view doesn't get updated when I added or removed files in the folder that the model is monitoring.

    Any help is appreciated, thanks.

    code:
    Qt Code:
    1. mod->setQuery("SELECT id FROM infotable");
    2.  
    3. QListView* list = new QListView;
    4. list->setModel(mod);
    5. list->show();
    To copy to clipboard, switch view to plain text mode 

  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: QSqlQueryModel data update

    Databases don't notify their clients about a change in their contents, so you have to check it yourself now and then and refresh your model (using select()) when needed. That's how databases work and there is nothing you can do about it.

  3. #3
    Join Date
    Nov 2006
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQueryModel data update

    I was under the impression that the model classes would monitor the data assigned to them, guess I was wrong. According to what you said, I would just have to use a timer and write something like this?
    Qt Code:
    1. void timerEvent()(
    2. {
    3. mod->select();
    4. }
    To copy to clipboard, switch view to plain text mode 

    Do I need to set the model to refresh the view? Thank you.

    ps. this is very strange..... the above code will actually erase the view (ie. the header columns) for some reason.
    Last edited by psi; 22nd December 2006 at 02:38.

  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: QSqlQueryModel data update

    Quote Originally Posted by psi View Post
    I was under the impression that the model classes would monitor the data assigned to them
    In general - yes, but with databases it's simply impossible. The overhead for doing this is quite high.

    According to what you said, I would just have to use a timer and write something like this?
    Qt Code:
    1. void timerEvent()(
    2. {
    3. mod->select();
    4. }
    To copy to clipboard, switch view to plain text mode 
    Yes, something like that.

    Do I need to set the model to refresh the view?
    No, the model will tell the view to refresh itself.

    ps. this is very strange..... the above code will actually erase the view (ie. the header columns) for some reason.
    Yes. If you use select() you actually clear your model and populate it again, so the header data vanishes as well and you have to set it again. If you wish to avoid that, you have to avoid making a select() and compare and synchronise the model yourself, but it's slightly more work, so it's proabably easier to set the headers again. Note that you're likely to also loose any selection in the view.

  5. The following user says thank you to wysota for this useful post:

    psi (23rd December 2006)

  6. #5
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlQueryModel data update

    You should use setQuery.

Similar Threads

  1. QTableWidget Update - slow
    By DPinLV in forum Qt Programming
    Replies: 16
    Last Post: 18th August 2006, 21:09
  2. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 12:53
  3. How to convert binary data to hexadecimal data
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 8th March 2006, 16:17
  4. Replies: 16
    Last Post: 7th March 2006, 15:57
  5. Record update windowd entered data saving
    By MarkoSan in forum Qt Programming
    Replies: 56
    Last Post: 18th January 2006, 18:50

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.