Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 950139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:31:08+00:00 2026-05-15T23:31:08+00:00

What I have: QTreeView class with table data And connected QAbstractTableModel model Question :

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:31:09+00:00Added an answer on May 15, 2026 at 11:31 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a following problem. I'm trying to implement a model for my QTreeView
I have two QTreeView s in a QHBoxLayout . <layout class=QHBoxLayout name=lay_disk_content> <item> <widget
I have a number of items in a QTreeView. Each item is generated using
I have a class that contains hierarchical data. I want to present this data
Hello all I have class that inherited from Qtreeview and I implement simple (
Problem : I have QTreeView object, and a QStandardItemModel as model to view widget;
I have a TreeView that is bound to a XmlDataSource control. I've added some
I have a treeView , and i want to know if some node can
I'm testing QTreeView functionality right now, and i was amazed by one thing. It
I have a TreeView that is bound to a collection class that I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.