我目前的程序中需要一个QTreeWdiget来显示数据。
数据在代码里是一个结构体,里面有几百个变量(有基本类型,也有子结构体),结构体的每个变量的值已经通过memset赋值过。
现在我需要在QTreeWdiget显示出每个变量的名和值。
比如有一个结构是:
struct Child{
long c;
int d;
};
struct Node{
char a;
short b;
Child child;
}
那么我希望在QTreeWdiget以树的形式展现出来,有什么简便的方法可以做到?
The simplest way is to use QtPropertyBrowser. You can refer to the official example. I packaged a CMake version: http://whudoc.qiniudn.com/2016/QtPropertyBrowser.7z
If you don’t want to use this library, you have to implement it yourself:
Implement your own model, which needs two functions:
data
andsetData
. The data should be placed in a specific role, so don’t mess it up;Set a model for TableView (data is bound in the model);
Set the delegate for TableView (this is the control for editing data);
Tell me briefly about delegate. A delegate usually inherits QItemDelegate. A delegate will get a pointer to a widget (that is, the child widget of your treeView), and then create an editor to implement the editing function. The editor first loads data from the widget, and then setsModelDate to save the data back after editing.