Dibo
Dibo

Reputation: 1189

QT - adding own column to QFileSystemModel

Can I extend QFileSystemModel and add new column with text / icon?

Regards

Upvotes: 2

Views: 3103

Answers (1)

ellimilial
ellimilial

Reputation: 1236

I would start by subclassing the model, providing the additional column and supplying the data to it.

So at the very least I would reimplement columnCount() and data() in both cases calling the base class and manipulating the results accordingly.

class yourSystemModel : public QFileSystemModel
{
    Q_OBJECT

    int columnCount(const QModelIndex& parent = QModelIndex()) const
    {
        return QFileSystemModel::columnCount()+1;
    }

    QVariant data(const QModelIndex& index,int role) const
    {
       if(!index.isValid()){return QFileSystemModel::data(index,role);}
       if(index.column()==columnCount()-1)
       {
           switch(role)
           {
              case(Qt::DisplayRole):
                  {return QString("YourText");}
              case(Qt::TextAlignmentRole):
                  {return Qt::AlignHCenter}
              default:{}
           }
       }
       return QFileSystemModel::data(index,role);
   }
}

The official doc outline some basis as to minimal reimplementation for the abstract item model, but in this case you can run away with much less. http://doc.qt.digia.com/stable/qabstractitemmodel.html - Subclassing section.

Upvotes: 5

Related Questions