简体   繁体   English

qt自定义类到QTreeView

[英]qt custom class to QTreeView

I have a custom class called A which basically consists of a vector of type B , where B is a private class inside A . 我有一个名为A的自定义类,它基本上由类型B的向量组成,其中BA内的私有类。

class A{
public:

explicit A(std::string name): name_{name} {}

void add_item(int i, double d, std::string s){
    list_.emplace_back(i, d, s);
}

private:

    class B{
    public:

        B(int i, double d, std::string s): i_{i}, d_{d}, s_{s} {}

    private:
        int i_;
        double d_;
        std::string s_;
    }

    std::string name_;
    std::vector<B> list_;
}

This class is from another project which does not use QT in any way. 此类来自另一个完全不使用QT的项目。 It is also not possible to include some QT headers into this class file. 也不能在此类文件中包括一些QT头。

My goal is to somehow connect an object of type A to a QTableView. 我的目标是以某种方式将类型A的对象连接到QTableView。 The goal is that in the QTableView there are as many rows as there are items in list and 3 columns, where the first one lists the values of the integers ( i_ ), the second the values from the doubles ( d_ ), and the third the values from the strings ( s_ ). 目标是在QTableView中,行的数量与list和3列中的项的数量相同,其中第一个列出整数( i_ )的值,第二个列出双精度数( d_ )的值,第三个列出字符串( s_ )中的值。

It should be possible to edit the values in the QTreeView and it should automatically add a row if I call add_item() . 应该可以编辑QTreeView中的值,并且如果我调用add_item() ,它应该自动添加一行。

I'm not quite sure how to start here. 我不太确定如何从这里开始。 I used QTableViews a lot but only with QStandardItemModel. 我经常使用QTableViews,但仅使用QStandardItemModel。

Of course, I could simply use a QStandardItemModel and parse the values into this model, but then I would need to convert it every time back to class A if I want to use it in another function. 当然,我可以简单地使用QStandardItemModel并将值解析为该模型,但是如果我想在其他函数中使用它,则每次都需要将其转换回A类。

I would appreciate any help. 我将不胜感激任何帮助。 I'm sure there has to be a way to simply do this, but I do not know what to look for. 我敢肯定必须有一种方法可以简单地做到这一点,但我不知道要寻找什么。 If you have a useful link or a key-word for googling please let me know. 如果您有用于谷歌搜索的有用链接或关键字,请告诉我。

My solution was to create a new class which is derived from QAbstractTableModel and overloading the functions 我的解决方案是创建一个从QAbstractTableModel派生的新类并重载函数

QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;

int rowCount(const QModelIndex &parent = QModelIndex()) const override;

int columnCount(const QModelIndex &parent = QModelIndex()) const override;

QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;

bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;

Qt::ItemFlags flags(const QModelIndex& index) const override;

bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;

bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;

additionally, I had to create a couple of extra Delegate classes which derive from QItemDelegate and override the following functions: 此外,我还必须创建几个额外的Delegate类,它们从QItemDelegate派生并覆盖以下功能:

QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;

void setEditorData(QWidget *editor, const QModelIndex &index) const override;

void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;

void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM