简体   繁体   中英

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 .

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. It is also not possible to include some QT headers into this class file.

My goal is to somehow connect an object of type A to 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_ ).

It should be possible to edit the values in the QTreeView and it should automatically add a row if I call add_item() .

I'm not quite sure how to start here. I used QTableViews a lot but only with 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.

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

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:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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