smallB
smallB

Reputation: 17120

Model view difficulties

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?

Upvotes: 0

Views: 281

Answers (1)

pnezis
pnezis

Reputation: 12321

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.

Upvotes: 2

Related Questions