robinzero200
robinzero200

Reputation: 53

On QTreeView double_Clicked event: how to know which folder was clicked?

I'm still pretty new to C++ and qt creator. I have a TreeView displaying a directory, and upon double-clicking a folder I'd like to be able to get the directory of the folder, so i can do something with the contents. I notice the double_Clicked event passing along a "const QModelIndex& index" and I suppose this holds some information on which folder of the tree was clicked. I can't find any documentation on this signal, and what the "index" passed along could be. Does anyone have an explanation, tutorial, example, documentation, ... for me? I've been searching for awhile and trying things out but I can't find the solution. Or additionally: how can I check what gets passed along? How could I print this, or know what it is?

Upvotes: 0

Views: 678

Answers (1)

Waqar
Waqar

Reputation: 9331

You can set up your treeView in the constructor like this:

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    model = new QFileSystemModel(this);
    model->setRootPath("");
    ui->treeView->setModel(model);
    ui->treeView->setRootIndex(model->index("/home/waqar/"));
}

And in the double_Clicked() slot, use the QModelIndex to get the name of the folder you clicked on:

void MainWindow::on_treeView_doubleClicked(const QModelIndex &index)
{
    //displays the name of the folder you clicked on in the terminal
    qDebug () << index.data(Qt::DisplayRole).toString();

   //get full path
  QString path = model->filePath(index)
}

Upvotes: 1

Related Questions