关于 QTreeView double_Clicked 事件:如何知道单击了哪个文件夹?

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

我对 C++ 和 qt creator 还是很陌生。我有一个显示目录的 TreeView,双击文件夹后我希望能够获取该文件夹的目录,这样我就可以对内容做一些事情。我注意到 double_Clicked 事件传递了一个“const QModelIndex& index”,我想这包含了一些关于树的哪个文件夹被点击的信息。我找不到关于此信号的任何文档,以及传递的“index”可能是什么。有没有人有解释、教程、示例、文档……对我来说?我一直在寻找一段时间并进行尝试,但找不到解决方案。或者另外:我如何检查传递的内容?我怎么打印这个,或者知道它是什么?

您可以像这样在构造函数中设置您的 treeView:

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/"));
}

然后在 double_Clicked() 插槽中,使用 QModelIndex 获取您单击的文件夹的名称:

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)
}