c – 将QAbstractTableModel与Qml TableView一起使用仅显示第1列
我试图使用一个(派生自)QAbstractTableModel与一个Qml TableView;
但是,只显示第一列. 原因是QVariant MyModel :: data(const QModelIndex& index,int role)不是为非零列调用的,但我不明白为什么. QTableView工作正常. 我制作了一个单独的,简单的项目来重现我的问题: MyModel.h: #ifndef MYMODEL_H #define MYMODEL_H #include <QObject> #include <QAbstractTableModel> #include <QList> #include <QString> #include <QDebug> struct SimpleData { QString m_one; qint32 m_two; qreal m_three; }; class MyModel : public QAbstractTableModel { Q_OBJECT public: explicit MyModel();//MyData *the_data); int rowCount(const QModelIndex & parent = QModelIndex()) const Q_DECL_OVERRIDE; int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; QVariant data(const QModelIndex &index,int role = Qt::DisplayRole) const Q_DECL_OVERRIDE; QHash<int,QByteArray> roleNames() const Q_DECL_OVERRIDE; signals: public slots: void theDataChanged(); private: QList<SimpleData> m_the_data; }; #endif // MYMODEL_H mymodel.cpp: MyModel::MyModel() : QAbstractTableModel(0) { m_the_data << SimpleData{"Alpha",10,100.0} << SimpleData{"Beta",20,200.0} << SimpleData{"Gamma",30,300.0} << SimpleData{"Delta",40,400.0}; } int MyModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent) return m_the_data.size(); } int MyModel::columnCount(const QModelIndex &parent) const { Q_UNUSED(parent) return 3; } QVariant MyModel::data(const QModelIndex &index,int role) const { // Check DisplayRole if(role != Qt::DisplayRole) { return QVariant(); } // Check boudaries if(index.column() < 0 || columnCount() <= index.column() || index.row() < 0 || rowCount() <= index.row()) { qDebug() << "Warning: " << index.row() << "," << index.column(); return QVariant(); } // Nominal case qDebug() << "MyModel::data: " << index.column() << "; " << index.row(); switch(index.column()) { case 0: return m_the_data[index.row()].m_one; case 1: return m_the_data[index.row()].m_two; case 2: return m_the_data[index.row()].m_three; default: qDebug() << "Not supposed to happen"; return QVariant(); } } QHash<int,QByteArray> MyModel::roleNames() const { QHash<int,QByteArray> roles; roles[0] = "one"; roles[1] = "two"; roles[2] = "three"; return roles; } void MyModel::theDataChanged() { //TODO } main.qml: import QtQuick 2.1 import QtQuick.Controls 1.0 ApplicationWindow { title: qsTr("Hello World") width: 640 height: 480 menuBar: MenuBar { Menu { title: qsTr("File") MenuItem { text: qsTr("Exit") onTriggered: Qt.quit(); } } } TableView { anchors.fill: parent TableViewColumn {title: "1"; role: "one"; width: 70 } TableViewColumn {title: "2"; role: "two"; width: 70 } TableViewColumn {title: "3"; role: "three"; width: 70 } model: theModel } } main.cpp中: #include <QtQml> #include <QQmlApplicationEngine> #include <QApplication> #include <QQuickWindow> #include <QTableView> #include "mymodel.h" int main(int argc,char *argv[]) { // Application : QApplication app(argc,argv); // Data stuff : //MyData data(&app); MyModel model; // UI : QQmlApplicationEngine engine; engine.rootContext()->setContextProperty("theModel",&model); engine.load(QUrl("qrc:/qml/main.qml")); QList<QObject*> temp = engine.rootObjects(); QObject *topLevel = temp.value(0); QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel); if ( !window ) { qWarning("Error: Your root item has to be a Window."); return -1; } // Display the main.qml,which show the model: window->show(); // Same,using a QTableView: QTableView view;; view.setModel(&model); view.show(); return app.exec(); } qDebug输出关于TableView(row,then column): MyModel::data: 0 ; 0 MyModel::data: 0 ; 0 MyModel::data: 0 ; 1 MyModel::data: 0 ; 1 MyModel::data: 0 ; 2 MyModel::data: 0 ; 2 MyModel::data: 0 ; 3 MyModel::data: 0 ; 3 关于QTableVierw的qdebug输出: MyModel::data: 0 ; 0 MyModel::data: 0 ; 0 MyModel::data: 0 ; 0 MyModel::data: 1 ; 0 MyModel::data: 2 ; 0 MyModel::data: 0 ; 1 MyModel::data: 1 ; 1 MyModel::data: 2 ; 1 MyModel::data: 0 ; 2 MyModel::data: 1 ; 2 MyModel::data: 2 ; 2 MyModel::data: 0 ; 3 MyModel::data: 1 ; 3 MyModel::data: 2 ; 3 笔记/东西我试过: >在我给的代码中,我可以使用一个QQmlListProperty;然而我的实际代码更复杂 >数据实际上是查询的, >有一个项目来检查模型,ModelTest,但它不适用于Qt 5.2. 相关链接: http://qt-project.org/doc/qt-5/QAbstractTableModel.html http://qt-project.org/doc/qt-5/qtquick-modelviewsdata-cppmodels.html#qabstractitemmodel 提前致谢! 规格:Windows 7,Qt 5.2,Mingw 4.8,Qt Creator 3.0 解决方法
TableViewColumn API表明,通过角色而不是列检索列中的数据,即“一”,“二”和“三”,而传递的列将始终为0.您返回QVariant()除了Qt之外的所有内容:: DisplayRole.
Qt :: DisplayRole为0,转换为int.在rolenames中,您将名称设置为0到“one”,这就是为什么你碰巧看到“one”(DisplayRole)的东西. 所以要解决这个问题,你必须返回一两个,三个.我建议在标题中定义自定义角色枚举: //In class MyModel: enum Role { OneRole=Qt::UserRole,TwoRole,ThreeRole }; 定义角色名称: QHash<int,QByteArray> roles; roles[OneRole] = "one"; roles[TwoRole] = "two"; roles[ThreeRole] = "three"; return roles; } 请注意,我从Qt :: UserRole而不是0开始.这样可以避免与预定义的角色(如Qt :: DisplayRole)的冲突. 然后在data()中返回一个,两个和三个东西: ... switch(role) { case OneRole: return m_the_data[index.row()].m_one; case TwoRole: return m_the_data[index.row()].m_two; case ThreeRole: return m_the_data[index.row()].m_three; } ... 现在你应该看到数据. 看来来自QtQuickControls的TableView / TableViewColumn有一些不太令人困惑的角色和列组合:虽然命名让我们想到一个模型列(但实际上它们在这里引用了视图的列),只能通过不同的角色检索数据,列被固定为0.(对于我,TableViewColumn中应该有另一个可选属性“列”.)这是C QAbstractItemModel / QTableView方法之间的一个冲突,其中多个列是一个自然的事情,而QtQuick视图(其中仅使用角色引用数据)通常不支持多个列(ListView,网格视图). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |