O
O
Oleg_Mishutin2018-07-08 18:17:47
Qt
Oleg_Mishutin, 2018-07-08 18:17:47

QFileSystemModel, QTreeView: how to assign SIGNAL to specific file from tree?

Good afternoon. I'm trying to build a simple local help system in Qt: on the left - displaying the file system, on the right - displaying the contents of the file.
Code in main.cpp:

QFileSystemModel *model = new QFileSystemModel();
    model->setRootPath(QDir::currentPath());
    QTreeView *tree = new QTreeView();
    tree->setModel(model);
    tree->setRootIndex(model->index(QCoreApplication::applicationDirPath() + "/pkb_db")); // в этом каталоге - другие папки и файлы - "база"
    layout1->addWidget(tree);
    QModelIndex index1 = model->index(QCoreApplication::applicationDirPath() + "/pkb_db/Roadmaps/Roadmaps.html"); // назначаю индекс для определенного файла

    QTextBrowser* center = new QTextBrowser;
    layout2->addWidget(center);
    center->setSource(QUrl("file:///" + QCoreApplication::applicationDirPath() + "/pkb_db/Roadmaps/Roadmaps.html"));
    center->hide();

    QObject::connect(tree, SIGNAL(clicked(const QModelIndex&)), center, SLOT(show()));

With this code, the content of the Roadmaps.html file is displayed when you click on any directory or file from the tree on the left. But assigning a click signal to the desired file in the tree does not work. How to correctly apply index in connect here? Or am I not digging there at all?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ighor July, 2018-07-08
@IGHOR

To find out what was clicked, you need a slot with the parameter const QModelIndex &model
And get the element like this QStandardItem *item = model->itemFromIndex(model->mapToSource(index));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question