我创建了QAbstractTableModel子类来表示它就像Qml中的一个表。当在qml中我使用GridView或ListView项时,他们只使用第一列来表示。根据文档:http://doc.qt.io/qt-5/qml-qtquick-gridview.html这个类使用QAbstractListModel。是否有任何类,在qml中更正表示QAbstractTableModel,就像Grid?
我的模型中有.h文件(以下所有函数都是用C ++实现的)
class ButtonModel : public QAbstractTableModel
{
Q_OBJECT
public:
enum ButtonRoles {
BUTTON_ID_ROLE = Qt::UserRole + 1,
};
enum ColumnNames{
FIRST = 0,
SECOND,
THIRD,
FOURTH,
FIFTH,
SIXTH,
SEVENTH,
LAST
};
ButtonModel(QObject* parent = 0);
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
bool loadModel (const QVector<QHash<ColumnNames, Button *>> &buttons, const QModelIndex &parent = QModelIndex());
private:
QVector<QHash<ColumnNames, Button*>> _buttons;
};
和data()函数实现
QVariant ButtonModel::data(const QModelIndex &index, int role) const
{
if ((!index.isValid()) || (this->rowCount() <= index.row()) || (this->columnCount() <= index.column())){
return QVariant();
}
switch (role){
case ButtonRoles::BUTTON_ID_ROLE:
return QVariant(_buttons[index.row()][ColumnNames(index.column())]->buttonId());
break;
case Qt::DisplayRole:
return QVariant(_buttons[index.row()][ColumnNames(index.column())]->displayText());
break;
default:
return QVariant();
}
}
答案 0 :(得分:0)
您可能希望QTableView不是QGridView。
网格视图实际上只是列表的不同布局,而表视图确实有多个列,可以从C ++ QAbstractTableModel获取其模型数据。