how to add C++ model to Tableview
-
I am following a book on Qt. It has following example.
// mulmodel.cpp #include "mulmodel.h" MulModel::MulModel(int rows, int columns, QObject *parent) : QAbstractTableModel(parent) { m_rows = rows; m_columns = columns; } Qt::ItemFlags MulModel::flags(const QModelIndex &index) const { if(!index.isValid()) return Qt::ItemIsEnabled; return Qt::ItemIsSelectable | Qt::ItemIsEnabled; } QVariant MulModel::headerData(int section, Qt::Orientation orientation, int role) const { if( role != Qt::DisplayRole ) return QVariant(); return section+1; } int MulModel::rowCount(const QModelIndex &parent) const { if (parent.isValid()) return 0; return m_rows; } int MulModel::columnCount(const QModelIndex &parent) const { if (parent.isValid()) return 0; return m_columns; } QVariant MulModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); switch(role) { case Qt::DisplayRole: return (index.row()+1) * (index.column()+1); case Qt::ToolTipRole: return QString("%1 x %2").arg(index.row()+1).arg(index.column()+1); default: return QVariant(); } }
#ifndef MULMODEL_H #define MULMODEL_H #include <QAbstractTableModel> class MulModel : public QAbstractTableModel { Q_OBJECT public: explicit MulModel(int rows, int columns, QObject *parent = nullptr); Qt::ItemFlags flags( const QModelIndex &index ) const; // Header: QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; // Basic functionality: int rowCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; private: int m_rows, m_columns; }; #endif // MULMODEL_H
Main cpp as
//Main.cpp QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine* engine = new QQmlApplicationEngine(); MulModel mulmodel(12, 12); engine->rootContext()->setContextProperty("MulModel", &mulmodel); engine->load(QUrl(QStringLiteral("qrc:/ui/main.qml")));
TableView { width: 600 alternatingRowColors: true model: MulModel }
I have no idea, why the code is not working. Can somebody help on this.
-
@milan What do you mean by the code is not working ? Does the application start ? Do you see a "blank" window ?
I see two mistake in the code:
- In main.cpp,
return app.exec();
is missing - There is no height defined for TableView, I assume this is the root element of your main.qml file.
- In main.cpp,
-
@Gojir4 .The main cpp does have app.exec(). I just posted code snippet for main.cpp. The tableview, even if height is defined, it does not work. And yes, the window does open, only Table is populated with mulmodel data. The table can also be seen, it is blank though.
-
@milan I guess you have to add some
TableViewColumn
in yourTableView
, as suggested from the docTableView { TableViewColumn { role: "display" title: "Display" width: 100 } TableViewColumn { role: "tooltip" title: "ToolTip" width: 200 } model: libraryModel }
You will also probably have to implement QAbstractItemModel::roleNames() method to provide role names for QML (I don't thinkif you can use
display
andtooltip
directly from QML) -
Hi,
Please refer this page for your reference. this will surely help your problem. You need to have your own custom role and modification in your qml file.
https://qiita.com/illness072/items/a6f2ce9f7a1bfff44049