Question

Well. I'm working on an IDE. Some of you maybe saw a post about it. Well, i have no clue of how QTreeWidget & QtreeWidgetItem works since can't find a demo and the documentation doesn't help. Well, what i'm trying to do is a IDE that you open the project file and then include all the files of the project to the tree. (Files in the project file are included by doing #include "filename"). How i do this? Then you click a file and open it in a Tab (That was on other question). So in this part i just need an example of how to do the file click. :)

Was it helpful?

Solution

A simple solution would be to directly use QTreeWidgetItem. For every file in your project, create an instance of QTreeWidgetItem:

QTreeWidgetItem *file = new QTreeWidgetItem(browserWidget);
 file->setText(0, filename);

Where "filename" is a string containing the name of your file. You can get the filename by parsing your project file looking for lines beginning with "#include".

By passing another QTreeWidgetItem in the constructor of a new item, you make the new item a child of the othe one. That way you can create directory structures.

To open a file, you can connect to the signal "itemDoubleClicked" of the QTreeWidget. You will get a pointer to the clicked widget item. Calling "text()" will retrieve the filename. If you have a directory structure, you need to do this too for all parent widget items. By concatenating the strings, you will get the path to your file.

You can find an example for this in the Qt Docs (see file settingstree.cpp)

As long as your IDE stays simple, this will be sufficient. A more flexible and "object-oriented" solution would be to create a subclass of QTreeWidgetItem. You will need to overwrite some methods. Since you probably only need read-only access the four methods described in the Qt Docs will be enough.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top