Results 1 to 3 of 3

Thread: QSqlQueryModel, setQuery and updating attached views

  1. #1
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Question QSqlQueryModel, setQuery and updating attached views

    G'day All,

    I currently have a QSqlQueryModel that executes a query to aggregate a number of columns from a single table of potentially a few thousand rows and return a single row of totals. The query is dynamically built to include or exclude rows matching criteria based on a few flags. Every time the flags changes the model builds a query string and uses setQuery() to change its presented data (this process does not lend itself to a parameterised query).

    The model drives a simple form through a QDataWidgetMapper. When I create the model, execute setModel() and toFirst() the form is updated with the model's default query values.

    When the model flags change the query is changed, no error is raised (the query is good), but the view did not update automatically as I had expected it to. What I've done is to execute reset() in the model after setQuery() and connected the modelReset() signal to the mapper's toFirst() slot.

    Is this the 'correct' Qt way to go about it? Are there better signals and slots I've missed?

    Alternatively, is there a better way to achieve the goal, perhaps using QSqlTable Model and filtering proxy?

    Any and all input gratefully accepted.

  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, setQuery and updating attached views

    Setting a new query on the model should call reset() by itself so you don't have to do that manually. As QDataWidgetMapper is not a real view, you have to make sure it is told to position itself on a valid index (as you did by calling toFirst()).

    I would advise against using a proxy for filtering in your case unless the data in your database is stationary (doesn't change over time). If it is, then using a filter proxy is a better choice than requerying the database. But if the data changes, then you have to refresh the model from the database or else you won't notice changes. Also if you have more than few thousand rows using a filter proxy might become unefficient. SQL DBMS are optimized for filtering data by using indexes, Qt models are not.
    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.


  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSqlQueryModel, setQuery and updating attached views

    Thanks,

    That was how I read the docs too (4.5.0), but if I remove the explicit call to reset() the data mapper does not update (presumably the signal is not sent). I also looked at the requirement that the query be active() and not isForwardOnly(): both OK before and after the setQuery().

    It reassuring to know that this newbie is not completely off-beam

Tags for this Thread

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.