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

  • SEARCH
  • Home
  • 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 6854285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:36:51+00:00 2026-05-27T01:36:51+00:00

I’ve treeview in which I’d like to have displayed files selected by user via

  • 0

I’ve treeview in which I’d like to have displayed files selected by user via file_dialog.getOpenFileNames(); file_dialog is QFileDialog.
I did create model class:

class File_Display_Model : public QAbstractItemModel
{
    Q_OBJECT

private:
    QStringList* selected_files_;

public:
    explicit File_Display_Model(QObject *parent = nullptr,QStringList* selected_files = nullptr);

    int File_Display_Model::columnCount( const QModelIndex & parent ) const
    {
        selected_files_->count();
    }

    QVariant File_Display_Model::data(const QModelIndex & index, int role) const
    {
        if (!index.isValid())
        {
                return QVariant();
        }
        else
        {
            if (role == Qt::DisplayRole) {
                    if (index.row() == index.column())
                    {
                        return 0;
                    }
                    else
                    {
                        return selected_files_->at(role);
                    }
                }
                return QVariant();
        }
    }

    QModelIndex File_Display_Model::index(int row, int column, const QModelIndex & parent ) const
    {
         /*DUMMY - HERE I JUST DON'T KNOW WHAT TO RETURN*/
         return QModelIndex(); 
    }

    QModelIndex File_Display_Model::parent(const QModelIndex & index) const
    {
        return QModelIndex();
    }

    int File_Display_Model::rowCount( const QModelIndex & parent ) const
    {
        selected_files_->count();
    }
};

And I also provided this class as a model to tree view. There is a problem with a index method in this class – I don’t know what to return.
Could someone please help me and guide me how to make it work so files selected by an user are displayed in a treeview?

  • 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-27T01:36:52+00:00Added an answer on May 27, 2026 at 1:36 am

    First of all there is no reason for using a QStringList*. Qt uses implicit sharing so it is efficient to pass it as an argument (dont forget that QStringList is nothing more than a QList<QString>).

    Second you should review the excellent Qt Model/View Programming documentation.

    Row and Column Count

    You are trying to create a tree model so you should read carefully the tree model example. Notice that the rowCount and columnCount functions have as argument a model index.

    The rowCount() function simply returns the number of child items for
    the item that corresponds to a given model index, or the number of
    top-level items if an invalid index is specified

    and for the column count

    Since each item manages its own column data, the columnCount()
    function has to call the item’s own columnCount() function to
    determine how many columns are present for a given model index. As
    with the rowCount() function, if an invalid model index is specified,
    the number of columns returned is determined from the root item

    So you have to think how your stringlist will be represented as a tree model. How columns will you have and what will be stored there for every level? How will be the rows hierarchy? Why are you using as column count the number of strings?

    Model Index

    When you reimplement the index() function you just have to check if the provided row and column are valid and if yes you should call the createIndex function. Again it all depends on what data your model contains and how you have structured them. Since you want to implement a tree model you have to take under consideration the parent item as well.

    When reimplementing this function in a subclass, call createIndex() to
    generate model indexes that other components can use to refer to items
    in your model.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I would like to run a str_replace or preg_replace which looks for certain words
I have thousands of HTML files to process using Groovy/Java and I need to
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

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.