对于C++和qt creator,我还是个新手。我有一个显示目录的TreeView,当双击一个文件夹时,我希望能够获得该文件夹的目录,这样我就可以对内容执行一些操作。我注意到double_Clicked事件传递了一个"const QModelIndex& index“,我认为这包含了一些关于树的哪个文件夹被单击的信息。我找不到关于这个信号的任何文档,以及传递的"index“可能是什么。有没有人有解释,教程,例子,文档,...对我来说?我已经寻找了一段时间,并尝试了一些东西,但我找不到解决方案。或者另外:我如何检查传递的内容?我如何打印它,或者知道它是什么?
发布于 2020-08-01 00:37:14
您可以在构造函数中设置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)
}https://stackoverflow.com/questions/63194996
复制相似问题