
但是,只显示第一列.
原因是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_OBJECTpublic: 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:
#include“mymodel.h”
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.1import QtQuick.Controls 1.0applicationwindow { 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;然而我的实际代码更复杂
>数据实际上是查询的,
>我没有SimpleData类
>我基本上使用QAbstracttableModel作为代理.
因此,我不能切换到一个QQmlListProperty(或等效的)
>有一个项目来检查模型,ModelTest,但它不适用于Qt 5.2.
>数据类型(QString,qreal)不是一个问题:交换仍然只显示第一列.
相关链接:
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,网格视图).
总结以上是内存溢出为你收集整理的c – 将QAbstractTableModel与Qml TableView一起使用仅显示第1列全部内容,希望文章能够帮你解决c – 将QAbstractTableModel与Qml TableView一起使用仅显示第1列所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)