Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QtableView setSortingEnabled  forcing sortByColumn()
Forum Updated to NodeBB v4.3 + New Features

QtableView setSortingEnabled  forcing sortByColumn()

Scheduled Pinned Locked Moved Solved General and Desktop
qt5.9qtableviewqtableview c++sorting
23 Posts 5 Posters 30.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M mapuna
    10 Nov 2017, 10:18

    @JNBarchan Thanks a lot for helping me, I just wanted to know I more thing (I ma newbie in QT ).As suggested by you to subcalss the QTableView, following is my question regarding the same:

    • When I make DerivedTableView(model) : QTableView(model), how can I add this new View in the QTCreator or do you suggest to add this new view programmatic ally or is there any other away to do so.

    • I have a tableview added in a complex layout in QtCreator and in the code I add models/delegates to it using
      ui->tableView_name->setModel(myProxy) etc... can I typecast ui->tableView_name at runtime to DerivedTableView is this also possible and then use DerivedTableView to set models/etc

    • I also could not find some working example where the Custom View is added in code and used if you can provide some reference it would help.

    Thanks in advance.

    J Online
    J Online
    JonB
    wrote on 10 Nov 2017, 11:28 last edited by JonB 11 Oct 2017, 11:28
    #21

    @mapuna
    I'm afraid I'm not the right person to ask really:

    • I don't use QtCreator, so I don't know.

    • I'm not sure just what you mean. You cannot just dynamically/run-time cast an object to another type/derived type because "you feel like it", when the object was constructed it must actually have been of the type you are casting to. So your QTableView must actually have been created as a DerivedTableView (including if that's done in QtCreator). But then wherever you can use a QTableView you can use your DerivedTableView, e.g. for setModel() call.

    • No example from me. I don't even use C++, I use Python! :(

    But I'm sure someone else can clarify the above.....

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 10 Nov 2017, 22:12 last edited by
      #22

      Hi,

      1. http://doc.qt.io/qt-5/designer-using-custom-widgets.html
      2. You don't need to, just call setModel like you are doing currently
      3. Do you mean something like:
      MyView *view = new MyView;
      ui->layout_name->add(view);
      

      ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • V Offline
        V Offline
        Vitaliy Sapronenko
        wrote on 27 Mar 2019, 16:28 last edited by
        #23

        You could set default sorting column to -1 by QHeaderView::setSortIndicator like this

        yourTableView->horizontalHeader()->setSortIndicator(-1, Qt::AscendingOrder);
        

        before running

        yourTableView->setSortingEnabled(true);
        

        because QTableView::setSortingEnabled uses QHeaderView::sortIndicatorOrder and QHeaderView::sortIndicatorSection to identify by which column should model be sorted.

        Note that documentation of QHeaderView::setSortIndicator tells that not all models support setting sort column to -1 and may even crash in this case.

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved