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



  • 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





  • 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



  • 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 ?



  • 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



  • Hi bjanuario,

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

    Thanks,
    -Sandeep



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

    Thanks,
    -Sandeep


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.