c++ - 在Qt中如何对QTreeWdiget上的节点和相对应的数据绑定?
PHP中文网
PHP中文网 2017-04-17 13:37:48
0
1
684

我目前的程序中需要一个QTreeWdiget来显示数据。

数据在代码里是一个结构体,里面有几百个变量(有基本类型,也有子结构体),结构体的每个变量的值已经通过memset赋值过。
现在我需要在QTreeWdiget显示出每个变量的名和值。
比如有一个结构是:

struct Child{
    long c;
    int d;
};
struct Node{
    char a;
    short b;
    Child child;
}

那么我希望在QTreeWdiget以树的形式展现出来,有什么简便的方法可以做到?

PHP中文网
PHP中文网

认证0级讲师

reply all(1)
大家讲道理

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:

  1. Implement your own model, which needs two functions: data and setData. The data should be placed in a specific role, so don’t mess it up;

  2. Set a model for TableView (data is bound in the model);

  3. 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.

// your model header file
class PointsModel : public QAbstractTableModel
{
public:
    PointsModel( QList<TextureNotation::TN_Pt> *pts, QObject *parent = 0 );
    int rowCount( const QModelIndex &parent ) const;
    int columnCount( const QModelIndex &parent ) const;
    QVariant data( const QModelIndex &index, int role ) const;
    bool setData( const QModelIndex &index, const QVariant &value, int role );
    QVariant headerData( int section, Qt::Orientation orientation, int role ) const;
    Qt::ItemFlags flags( const QModelIndex &index ) const;
    int numChangedPoints( ) { return changedPoints.size(); }
private:
    QList<TextureNotation::TN_Pt> *_pts;
    QSet<int> changedPoints;
};

// your delegate header file
class NotationPointDelegate : public QItemDelegate
{
    Q_OBJECT

public:
    NotationPointDelegate( QObject *parent = 0 );

    QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option,
                           const QModelIndex &index ) const;
    void setEditorData( QWidget *editor, const QModelIndex &index ) const;
    void setModelData( QWidget *editor, QAbstractItemModel *model,
                       const QModelIndex &index ) const;
    void updateEditorGeometry( QWidget *editor,
                               const QStyleOptionViewItem &option, 
                               const QModelIndex &index ) const;
};

// your widget cpp file
doublespin = new NotationPointDelegate;
ui->tableView->setSortingEnabled( true );
ui->tableView->setItemDelegate( doublespin );

ui->tableView->setAlternatingRowColors( true );
ui->tableView->horizontalHeader()->setStretchLastSection( true );
ui->tableView->setEditTriggers( QAbstractItemView::DoubleClicked );
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template