2

I want to create as the following:

Unfortunately, Qt does not supported ready widget for that.

Is there is a plugin or any way to do that?

László Papp
  • 51,870
  • 39
  • 111
  • 135
Lion King
  • 32,851
  • 25
  • 81
  • 143
  • Too broad for Stackoverflow. – Captain Obvlious Oct 26 '14 at 00:13
  • 6
    @CaptainObvlious: No it's not too broad. It has a very specific answer. **Just because you don't know the answer or think it's a broad subject it means this is really the case!** In the case of Qt it's as simple as creating instances of two classes and connecting them. Done. I absolutely hate it, if people vote down perfectly fine answers and vote for closing, just because they themselves have no clue about the subject matter. – datenwolf Oct 26 '14 at 00:13
  • +1, but why are you not using QML instead of widgets these days? There is a ready-made element for this! I also think that you were too quick to accept an answer here. Usually, there are better answers coming than "read this link". At least, I hope so that we aim for in here. :) I removed the drivers tag. I am not sure why that was added. – László Papp Oct 26 '14 at 08:35

2 Answers2

5

Use QFileSystemModel on a QTreeView. If you look at the first of those two links, it actually contains example code doing exactly that.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
1

would personally suggest not use QWidgets for this task if you can avoid it. Instead, try to utilize the new shiny QML way of building Qt UI. It might be only my personal opition, but QTreeView has several flaws in my opinion.

QML

Here you can find a simple example how it is done with QML these days. It is using the FolderListModel from Qt.labs.folderlistmodel 2.1.

FolderListModel provides access to information about the contents of a folder in the local file system, exposing a list of files to views and other data components.

Note: This type is made available by importing the Qt.labs.folderlistmodel module. Elements in the Qt.labs module are not guaranteed to remain compatible in future versions.

import Qt.labs.folderlistmodel 2.1

The folder property specifies the folder to access. Information about the files and directories in the folder is supplied via the model's interface.

C++ and QWidgets

Should you insist on doing in C++ with the old QWidget set, your choice is probably to use QTreeView as it is a tree view after all and then combine that with QFileSystemModel.

The code would be something like this:

QFileSystemModel *model = new QFileSystemModel;
model->setRootPath(QDir::currentPath());
QTreeView *tree = new QTreeView(splitter);
tree->setModel(model);
tree->setRootIndex(model->index(QDir::currentPath()));
Community
  • 1
  • 1
László Papp
  • 51,870
  • 39
  • 111
  • 135
  • 1
    One doesn't need to do *every* UI with QML these days. – dom0 Oct 26 '14 at 10:58
  • Thank you for your detailed answer. and `+1` for your answer. – Lion King Oct 26 '14 at 10:59
  • @dom0: it is really not about QML. It is about that QWidget has been deemed "done" and technologically very limited and since you will not find proper C++ API for SG.. so yeah, it is not about QML, but the new technology is brought to us by QML after all. As soon as we get proper C++ API for SG (how long have I been waiting for that...), I will be able to post a non-QWidget based C++ solution, too. One typical example is that it will look ugly on mobile platforms. – László Papp Oct 26 '14 at 11:48
  • *»It is about that QWidget has been deemed "done" and technologically very limited«* uhm no, sorry, QWidget gets active development and support. It's a fine technology, it's just not apt for mobile and embedded platforms (and certain kinds of desktop applications). They are both (QWidget/QPainter-based UI and QML/QSG-based) here to stay. – dom0 Oct 26 '14 at 20:08
  • @dom0: it does not get any serious active development, no. It is basically in maintenance mode if any. Having said that, even a single memory leak patch of mine got stuck, so... Active development would mean to redesign it from scratch, and that is exactly what the QML stack has been about, a truly cross-platform (and hardware accelerated!) UI stack which is the only addition of Qt to boost, etc, in my opinion. Hopefully, there will be C++ API to SG later, too, but yeah, QML for now. Anyway, let us stop flaming, my answer provides a solution for old and stubborn heads, too. :-) – László Papp Oct 26 '14 at 22:37
  • @LionKing: [it is not a shame by the way to change the accepted answer if a better answer comes up](http://meta.stackexchange.com/questions/62252/is-it-poor-form-to-switch-accepted-answers). It is the original answerer's responsibility to provide the best answer after all. I appreciate that if you do not find this a better answer though. – László Papp Oct 26 '14 at 22:41
  • lpapp, I don't see this as flaming. But I do know both "sub-toolkits" (so to speak) and they both have more-or-less inherent strength and weaknesses. But yeah, let's stop here :) – dom0 Oct 27 '14 at 08:41