0
votes

I'm making a simple file explorer and I ran into some problems with Qt. I want to show the user a tree view of files on his computer, but I also want to be able to select multiple files/directories and do something with them later on (by selecting checkboxes or multiple select using ctrl+left click or shift+left click). I've placed the QTreeView element and set up a model to it (QFileSystemModel). It gives me a good tree view, but I can't modify the headers (column names) or add my own column with checkbox in every row (for example). Qt is new to me, I've searched for few good hours for some tips/solutions, but nothing is working with QFileSystemModel. Is there anything I can do to get this working?

The code is short and simple:

QString lPath = "C:/";
QString rPath = "C:/";
leftTree_model = new QFileSystemModel(this);
rightTree_model = new QFileSystemModel(this);

leftTree_model->setRootPath(lPath);
rightTree_model->setRootPath(rPath);

//i have actually 2 tree views that work the same
ui->leftTree->setModel(leftTree_model); //ui->leftTree is the first tree view
ui->rightTree->setModel(rightTree_model); //the second
1

1 Answers

1
votes

Use something of the following:

CheckStateRole to add checkboxes to your model. To do this, you inherit your custom item model (which you're going to use) from the QFileSystemModel, and reimplement the data() method, where you return bool values for CheckStateRole. You will also need the QAbstractItemModel::setData method to handle changes. You can also check the docs for QAbstractItemModel to see how to change header texts (headerData())

Change the selection mode of your view to allow multiple selections

EDIT: here's a sample code to inherit from the model

  class MyFancyModel : public QFileSystemModel
  {
  public:
    MyFancyModel(QObject* pParent = NULL) : QFileSystemModel(pParent)
    {
    }

    QVariant data(const QModelIndex & index, int role = Qt::DisplayRole ) const
    {
      if (role == Qt::CheckStateRole)
      {
        // stub value is true
        return true;  // here you will return real values 
                      // depending on which item is currently checked
      }
      return QFileSystemModel::data(index, role);
    }
  };