Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
528 views
in Technique[技术] by (71.8m points)

qt - Is it possible to use QAbstractTableModel with TableView from QtQuick.Controls?

I’m trying to implement following thing:

There is 2d table with values. I need to implement UI for viewing data and editing it. To simplify synchronisation between edits from UI and table updates coming from the back-end, I want model to store uncommitted edited values and expose a couple of methods to accept/reject pending changes.

From what I understand this is reasonably easy to do with QtWidgets + models:

Base model on QAbstractTableModel. Add a few extra roles for storing/querying pending changes, etc. Use QTableView with custom item delegate, which can query model whether cell is editable, show uncommitted changes, etc.

But I’m puzzled about how to implement it QtQuick.Controls.TableView. From my experiments, TableView doesn’t work with QAbstractTableModel — it iterates over the first column of the model and uses roles to simulate second dimension.

Is there a way to make TableView work with QAbstractTableModel correctly? As an alternative — I can change model to use roles for columns, but I’m not sure how to handle other aspects of cell data (modified flag, uncommited value, etc.). The only idea I have so far is to return composite (dictionary) value for each cell. E.g. return QMap or QJsonObject as a value of “cell” and interpret it on QML side.

Are there any other ways to do it? What’s more effective if I decide to implement my second solution — QMap or QJsonObject?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is an example maybe it'll help you hopefully

Suppose we have sqlite database containing a table "mytable" with three columns "nom" (text),"prenom"(text) and "image"(url as text)

--------------------------- PRESENCEMODEL.h

#include <QSqlTableModel>
typedef  QHash<int,QByteArray> qMyHash  ;

class PresenceModel : public QSqlTableModel {

    Q_OBJECT
    Q_PROPERTY(QStringList dictionary READ dictionary NOTIFY dictionaryChanged)
    Q_PROPERTY(QString filterString READ filterString WRITE setFilterString NOTIFY filterStringChanged)
    Q_PROPERTY(qMyHash roles READ roles NOTIFY rolesChanged)

public:

    PresenceModel(QSqlDatabase, QObject* parent = 0);
    void generateRoleNames();

    QString filterString() const { return p_filterString; }
    void setFilterString(const QString&);

    Q_INVOKABLE QStringList getData(int currentRow);

    QVariant data(const QModelIndex&, int role) const;

    Q_INVOKABLE void insert(const QString& url, const QString& title);
    void populate();

    QHash<int, QByteArray> roleNames() const;
    Q_INVOKABLE void p_setTable(const QString &tableName);
    QStringList dictionary() const;
    void setDictionary(const QStringList &value);
    QHash<int, QByteArray> roles() const;



Q_SIGNALS:

    void dataChanged();
    void gotData();

    void rolesChanged();

private:


    enum ColumnRH {
        nom= Qt::UserRole + 1,
        prenom= Qt::UserRole + 2,
        image= Qt::UserRole + 3
    };

    QHash<int, QByteArray> p_roles;
    QString p_filterString;
    QStringList p_dictionary;
};

--------------------------- PRESENCEMODEL.cpp

#include "presencemodel.h"
#include <QtCore/QDateTime>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlRecord>
#include <QDebug>

PresenceModel::PresenceModel(QSqlDatabase database, QObject *parent)
    : QSqlTableModel(parent,  database)
{
}

void PresenceModel::generateRoleNames()
{

    p_roles[nom] = "nom";
    p_roles[prenom] = "prenom";
    p_roles[image] = "image";
}



void PresenceModel::populate()
{

    select();

}

void PresenceModel::insert(const QString& url, const QString& title)
{

}



QVariant PresenceModel::data(const QModelIndex& index, int role) const
{
    if (role < Qt::UserRole)
        return QSqlQueryModel::data(index, role);

    const int columnId = role - Qt::UserRole;
    const QModelIndex modelIndex = createIndex(index.row(), columnId-1);
    return QSqlTableModel::data(modelIndex, Qt::DisplayRole);

}



QStringList PresenceModel::getData(int currentRow)
{
    QStringList rowDataList;

    for (int i=0;i<columnCount();i++)

        rowDataList.append(data(this->index(currentRow,i),Qt::DisplayRole).toString());

    emit gotData();

    return rowDataList;
}


QHash<int, QByteArray> PresenceModel::roleNames() const
{
    return p_roles;
}


QHash<int, QByteArray> PresenceModel::roles() const
{
    return roleNames();
}

--------------------------- main.cpp

 int main(int argc, char *argv[])
 {
 QGuiApplication app(argc, argv);

 QQmlApplicationEngine engine;

 QSqlDatabase m_database = QSqlDatabase::addDatabase("QSQLITE");

 m_database.setDatabaseName("/.../mydatabase.sqlite");


 PresenceModel  *p_pModel= new PresenceModel(m_database);

 p_pModel->setTable("mytable");

 m_database.open(); 

 QQmlContext *ctxt = engine.rootContext();

 ctxt->setContextProperty("ppModel", (PresenceModel*) p_pModel);

 engine.load(QUrl("qrc:/main.qml"));

 return app.exec();

  }

QML

--------------------------- main.qml

Rectangle {

    Component.OnCompleted : ppModel.populate()
    TableView{
        id:tableview_actes
        TableViewColumn{ role: "nom"  ; title: "nom" ; width: 330 }
        TableViewColumn{ role: "prenom" ; title: "prénom" ; width: 65}
        TableViewColumn{ role: "image" ; title: "Photo" ; width:65}

        model:ppModel
        onCurrentRowChanged:  {
            var list=   myModel.getData(currentRow)   // invoke c++ function                                    

            console.log(list[0])



        }
        Listview{
            model:ppModel
            delegate:
                Item {
                Text{

                    text: nom + " " +prenom // our roles 
                }

                Image{
                    source : image // image : url in our table 
                }
            }


        }

    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...