Results 1 to 6 of 6

Thread: qsqltablemodel "order by" clause in setfilter?

  1. #1
    Join Date
    Jul 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X

    Default qsqltablemodel "order by" clause in setfilter?

    I've been using an "Order By" clause in qsqltablemodel::setfilter, because i need to sort a table by two columns, and as far as I can tell qsqltablemodel::setSort only allows you to sort by one column.
    Order By *does* work in setFilter, but is it supported? I've never seen it in the documentation. I ask because, if I assign to a qtableview a qsqltablemodel model that has an order by clause in its filter, and if that view has sorting enabled, then resorting using the view's sort function produces an empty view.
    So I'm guessing that I shouldn't be using Order By in setFilter? Is there some other way to sort a tablemodel by 2 columns? I can't use a querymodel because they are read only, and I need to write to the sorted table.

    thanks

    John
    Learning, one problem at a time.

  2. #2
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: qsqltablemodel "order by" clause in setfilter?

    maybe, you can do like following
    Qt Code:
    1. model->setTable("tablename");
    2. model->setFilter("1=1 ORDER BY col1 ASC, col2 DESC");
    3. model->select();
    To copy to clipboard, switch view to plain text mode 

  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: qsqltablemodel "order by" clause in setfilter?

    You could create your own writeable model derived from QSqlQueryModel. An example is in the "Query Model Example" you can find in Assistant (and the QSqlQueryModel docs).

    You don't say why you want the rows returned in sorted order or how big the data set is. If the size is manageable and the sort is for display purposes then a customised QSortFilterProxyModel might be of use.

  4. #4
    Join Date
    Jul 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: qsqltablemodel "order by" clause in setfilter?

    That's what I am doing, but that breaks if it's on a table view that performs its own sort when the user clicks on a sort arrow in the horizontal header row: the model returns an empty set, rather than a sorted set.
    Learning, one problem at a time.

  5. #5
    Join Date
    Jul 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: qsqltablemodel "order by" clause in setfilter?

    Sorry, that previous post was in reference to:


    maybe, you can do like following

    model->setTable("tablename");
    model->setFilter("1=1 ORDER BY col1 ASC, col2 DESC");
    model->select();
    Learning, one problem at a time.

  6. #6
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qsqltablemodel "order by" clause in setfilter?

    You can always do this: view->setSortingEnabled(FALSE);

    To disable user sorting.

Similar Threads

  1. QToolBar allowedAreas "paint" order
    By Lykurg in forum Qt Programming
    Replies: 1
    Last Post: 30th September 2011, 03:35
  2. Trouble with "INSERT" by QSqlTableModel
    By AD in forum Qt Programming
    Replies: 12
    Last Post: 10th November 2008, 08:21
  3. how to change the "tab order"?
    By oscar721 in forum Qt Programming
    Replies: 2
    Last Post: 21st July 2008, 11:31
  4. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05
  5. Replies: 11
    Last Post: 10th July 2007, 15:58

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.