Search code examples
qtqt4qtreeview

HowTo restore QTreeView last expanded state?


What I have:

  1. QTreeView class with table data
  2. And connected QAbstractTableModel model

Question: how to save expanded state of items? Is some one have finished solutions?

PS: I know, that I can do this code by myself, but I don't have much time, and this is not the major problem of our project, but still we need it, because app contain a lot of such tables, and every time expanding tree items is annoyed process...


Solution

  • First, thanks to Razi for persistentIndexList and isExpanded way.

    Second, here is the code which works for me just fine :-)

    dialog.h file:

    class Dialog : public QDialog
    {
        Q_OBJECT;
    
        TreeModel *model;
        TreeView *view;
    
    public:
        Dialog(QWidget *parent = 0);
        ~Dialog(void);
    
        void reload(void);
    
    protected:
        void createGUI(void);
        void closeEvent(QCloseEvent *);
        void saveState(void);
        void restoreState(void);
    };
    

    dialog.cpp file:

    Dialog::Dialog(QWidget *parent)
    {
        createGUI();
        reload();
    }
    
    Dialog::~Dialog(void) {};
    
    void Dialog::reload(void)
    {
        restoreState();
    }
    
    void Dialog::createGUI(void)
    {
        QFile file(":/Resources/default.txt");
        file.open(QIODevice::ReadOnly);
        model = new TreeModel(file.readAll());
        file.close();
    
        view = new TreeView(this);
        view->setModel(model);
    
        QVBoxLayout *mainVLayout = new QVBoxLayout;
        mainVLayout->addWidget(view);
    
        setLayout(mainVLayout);
    }
    
    void Dialog::closeEvent(QCloseEvent *event_)
    {
        saveState();
    }
    
    void Dialog::saveState(void)
    {
        QStringList List;
    
        // prepare list
        // PS: getPersistentIndexList() function is a simple `return this->persistentIndexList()` from TreeModel model class
        foreach (QModelIndex index, model->getPersistentIndexList())
        {
            if (view->isExpanded(index))
            {
                List << index.data(Qt::DisplayRole).toString();
            }
        }
    
        // save list
        QSettings settings("settings.ini", QSettings::IniFormat);
        settings.beginGroup("MainWindow");
        settings.setValue("ExpandedItems", QVariant::fromValue(List));
        settings.endGroup();
    }
    
    void Dialog::restoreState(void)
    {
        QStringList List;
    
        // get list
        QSettings settings("settings.ini", QSettings::IniFormat);
        settings.beginGroup("MainWindow");
        List = settings.value("ExpandedItems").toStringList();
        settings.endGroup();
    
        foreach (QString item, List)
        {
            // search `item` text in model
            QModelIndexList Items = model->match(model->index(0, 0), Qt::DisplayRole, QVariant::fromValue(item));
            if (!Items.isEmpty())
            {
                // Information: with this code, expands ONLY first level in QTreeView
                view->setExpanded(Items.first(), true);
            }
        }
    }
    

    Have a nice day!)


    PS: this example based on C:\Qt\4.6.3\examples\itemviews\simpletreemodel code.