Please nominate your Qt Champions for 2021!

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
            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);
                return QVariant();
    #ifndef MULMODEL_H
    #define MULMODEL_H
    #include <QAbstractTableModel>
    class MulModel : public QAbstractTableModel
        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;
        int m_rows, m_columns;
    #endif // MULMODEL_H

    Main cpp as

    QGuiApplication app(argc, argv);
    QQmlApplicationEngine* engine = new QQmlApplicationEngine();
    MulModel mulmodel(12, 12);
    engine->rootContext()->setContextProperty("MulModel", &mulmodel);
    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.

  • @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 your TableView, as suggested from the doc

    TableView {
        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 and tooltip directly from QML)

  • @milan Don't you have the corrected version of your code as it is coming from a book ??

  • 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.

  • @TobbY . Thank you very much this solves my question. I have a different question (maybe I should open new topic). How can I add tooltip role to table row's title in tooltip, maybe table cells' value in its tooltip.

Log in to reply