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. Qt - Sort method does not update view - QTreeView and QAbstractItemModel
QtWS25 Last Chance

Qt - Sort method does not update view - QTreeView and QAbstractItemModel

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 6.1k Views
  • 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.
  • S Offline
    S Offline
    sandeepsastry
    wrote on last edited by
    #1

    Hi,

    This question has been posted multiple times, but none of the answers provided could help me. Hence posting it again.

    I have a QTreeView and a QAbstractItemModel up and working (Source code copied from "Your text to link here...":http://github.com/icefox/ftpmodel ). The below program is expected to do a directory listing along with file/folder creation date, size etc. I recently introduced the sorting function, to sort the file/folder names. The problem is, sorting works fine only for the first level of root items in the hierarchy, but suppose if I expand a Folder, and then click on the sort indicator (on the 0th column), things gets messed up. The view does not show correct values. I added some prints in the sort function to see, if the function is just sorting (re-arranging) or doing any copying or insertion or deletion. Luckily the sort function does not re-inset or copy or delete, it just re-arranges (as far as I understood). As explained in other posts, I tried calling emit dataChange(index, index), tried emit layoutabouttobechanged/layoutchanged rest model etc. But none of the things could work fine. One thing I doubt is either the indexes are getting corrupted or I am not updating the view with the right signals. All I expect is when I expand some of the Folders and then click sort, the view must display properly with sorted values and leave the expanded Folders still intact. Please help me out what I am missing. Important note, I added couple of lines in main.cpp to enable sorting. Snippet as below

    @FtpWindow(QWidget *parent = 0) : QWidget(parent) {
    ui.setupUi(this);
    ui.leftView->setModel(&dirModel);
    ui.rightView->setModel(&ftpModel);
    ui.rightView->setSortingEnabled(true);
    ui.rightView->sortByColumn(0, Qt::AscendingOrder);
    ui.rightView->setAcceptDrops(true);
    ui.host->setText("localhost");
    connect(ui.leftUrl, SIGNAL(textChanged(const QString& )),
    this, SLOT(leftPathChanged(const QString &)));
    connect(ui.connectButton, SIGNAL(clicked()), this, SLOT(connectToHost()));
    connect(ui.refreshButton, SIGNAL(clicked()), this, SLOT(refresh()));
    }@

    Thanks,
    -Sandeep

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bjanuario
      wrote on last edited by
      #2

      Check this post, i think your awnser is there:
      "http://www.qtcentre.org/threads/48230-QTreeView-How-to-refresh-the-view":http://www.qtcentre.org/threads/48230-QTreeView-How-to-refresh-the-view

      Hope this helps you,
      Kind Regards

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sandeepsastry
        wrote on last edited by
        #3

        Hello bjanuario,

        I went through the post, but as I explained above, I tried calling emit dataChange(index, index), tried emit layoutabouttobechanged/layoutchanged rest model etc. So I did try thing like modelAboutToBeReset (), modelReset (), reset(), beginResetModel (), endResetModel (). When I use any of the reset related function, 2 things happen, 1) Whole of the hierarchy gets reset, which is fine,but Previously expanded child nodes, collapse (contract) 2) Also when I click on any of the folder some times it shows empty contents, though it actually has contents. This I can confirm because my print/debug statements print the contents of the folders, but just the view does not pick the right contents. Or did the indexes got corrupted? Do I need to use persistent index?

        Could you please let me know what is the correct api to use, I have had shared the link to my program, which is easily compilable. Just need to add two line of code to make it sort enables, which again I have shared above.

        Thanks for your time.
        -Sandeep

        1 Reply Last reply
        0
        • B Offline
          B Offline
          bjanuario
          wrote on last edited by
          #4

          Hi sandeepsartry,

          I dont think u need presistent index as it will consume a lot of resources, far i can check is not an index corrupted issue. I will try to check on my machine and try to reproduce this.

          Can u just tell me what version of Qt and OS you are using ?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sandeepsastry
            wrote on last edited by
            #5

            qmake --version

            QMake version 2.01a
            Using Qt version 4.8.1 in /usr/lib/x86_64-linux-gnu

            lsb_release -a

            No LSB modules are available.
            Distributor ID: Ubuntu
            Description: Ubuntu 12.04.2 LTS
            Release: 12.04
            Codename: precise

            If you can reproduce on your machine that would be really helpful. Kindly keep an eye on the use case that I mentioned above. "If I expand a folder and sort, then things go wrong".

            Thanks for timely replies.
            -Sandeep

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sandeepsastry
              wrote on last edited by
              #6

              Hi bjanuario,

              Did you get a chance to see what I am missing in the code.

              Thanks,
              -Sandeep

              1 Reply Last reply
              0
              • S Offline
                S Offline
                sandeepsastry
                wrote on last edited by
                #7

                Can anyone out there help me solve this? I am stuck for quite a long time.

                Thanks,
                -Sandeep

                1 Reply Last reply
                0

                • Login

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