项目的 QTreeWidget
出色地。我正在开发 IDE。你们中有些人可能看过有关它的帖子。 好吧,我不知道 QTreeWidget 和 QTreeWidget 是如何实现的。 QtreeWidgetItem 可以工作,因为找不到演示并且文档也没有帮助。 好吧,我想做的是一个 IDE,您可以打开项目文件,然后将项目的所有文件包含到树中。 (项目文件中的文件是通过执行 #include "filename" 来包含的)。我如何做到这一点? 然后单击一个文件并在选项卡中打开它(这是在其他问题上)。因此,在这一部分中,我只需要一个如何单击文件的示例。 :)
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. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个简单的解决方案是直接使用 QTreeWidgetItem。对于项目中的每个文件,创建一个 QTreeWidgetItem 实例:
其中“filename”是包含文件名的字符串。您可以通过解析项目文件查找以“#include”开头的行来获取文件名。
通过在新项目的构造函数中传递另一个 QTreeWidgetItem,您可以使新项目成为另一个项目的子项目。这样您就可以创建目录结构。
要打开文件,您可以连接到 QTreeWidget 的信号“itemDoubleClicked”。您将获得指向单击的小部件项目的指针。调用“text()”将检索文件名。如果您有目录结构,则也需要对所有父小部件项执行此操作。通过连接字符串,您将获得文件的路径。
您可以在 Qt 文档 中找到相关示例(请参阅文件设置树.cpp)
只要您的 IDE 保持简单,这就足够了。更灵活和“面向对象”的解决方案是创建 QTreeWidgetItem 的子类。您将需要覆盖一些方法。由于您可能只需要只读访问 Qt Docs 就足够了。
A simple solution would be to directly use QTreeWidgetItem. For every file in your project, create an instance of QTreeWidgetItem:
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.