如何使用Qt和Go创建TreeView?

huangapple go评论119阅读模式
英文:

How to create a TreeView with Qt and Go?

问题

我正在尝试为我的应用程序创建一个Qt TreeView,我之前一直在使用ListView,但我认为TreeView更适合我的需求,然而文档几乎不存在。

要向ListView添加新项,可以使用以下代码:

var list = QNewListWidget(nil)
list.AddItem2("Sample")

然而,我一直在寻找如何将其更改为TreeView,以便我可以有以下结构:

Sample
 - Sample child 1
 - sample child 2

关于Go的文档几乎没有,有人知道我该如何做到这一点吗?

英文:

I'm trying to make a Qt TreeView for my application, I'd been using a ListView originally but I think a treeview would suit my needs better however the documentation is almost none existent.

to add a new item to a listview is use

var list = QNewListWidget(nil)
list.AddItem2("Sample")

however I've been looking to change it to a treeview so I can have:

Sample
 - Sample child 1
 - sample child 2

The documentation almost seems no existent for Go, anyone know how I can do this ?

答案1

得分: 1

对于任何想知道的人,我终于解决了。你需要添加一个不可见的项目,并将你的父项目作为子项目添加到其中:

list_item := widgets.NewQTreeWidgetItem2([]string{"示例项目"}, 0)
root := pointsOfInterest.InvisibleRootItem()

root.AddChild(list_item)

child_list_item := widgets.NewQTreeWidgetItem2([]string{"子项目"}, 0)
list_item.AddChild(child_list_item)
英文:

For anyone wondering, I finally worked it out. You have to add an invisible item and add your parent items as a child to that:

list_item := widgets.NewQTreeWidgetItem2([]string{"Sample Item"}, 0)
root := pointsOfInterest.InvisibleRootItem()

root.AddChild(list_item)

child_list_item := widgets.NewQTreeWidgetItem2([]string{"Child item"}, 0)
list_item.AddChild(child_list_item)

huangapple
  • 本文由 发表于 2022年3月7日 04:02:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/71373798.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定