简体   繁体   English

将 QStandardItemModel 从 C++ 传递给 QtQuick / QML TableView 并显示它

[英]Pass QStandardItemModel from C++ to QtQuick / QML TableView and display it

I'm currently trying to pass a QStandardItemModel to a QtQuick TableView and then display it.我目前正在尝试将QStandardItemModel传递给 QtQuick TableView ,然后显示它。 This is basically my code (just a simplified extract, so I hope I haven't added any extra mistakes here).这基本上是我的代码(只是一个简化的摘录,所以我希望我没有在这里添加任何额外的错误)。

The C++ / Qt part: C++/Qt 部分:

foo.cpp

[...]

QStandardItemModel *Foo::getModel()
{
    QStandardItemModel *model = new QStandardItemModel(this);
    QList<QStandardItem*> standardItemList;

    QList<QString> data;
    data.append("Cat");
    data.append("Dog");
    data.append("Mouse");

    foreach (QString cell, comInputData->getHeadings()) {
        QStandardItem *item = new QStandardItem(cell);
        standardItemList.append(item);
    }

    // we only add one row here for now; more will come later
    model->appendRow(standardItemList);
    standardItemList.clear();
    return model;
}

[...]

main.cpp

Foo f;
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("myModel", f.getModel());
engine.load(QUrl(QStringLiteral("qrc:///InputView.qml")));

The QtQuick / QML part: QtQuick / QML 部分:

InputView.qml

TableView {
    id: monitorInputVectorsTable
    [... positioning and sizing ...] 
    model: myModel
}

I guess, I'm still missing some important parts in the QML part, am I?我想,我仍然缺少 QML 部分中的一些重要部分,是吗? I found some examples with inline-model-defintions like this:我发现了一些像这样的内联模型定义的例子:

ListModel {
    id: libraryModel
    ListElement{ title: "A Masterpiece" ; author: "Gabriel" }
    ListElement{ title: "Brilliance"    ; author: "Jens" }
}

... displayed this way (the following added inside the TableView-item): ...以这种方式显示(在 TableView-item 中添加了以下内容):

TableViewColumn{ role: "title"  ; title: "Title" ; width: 100 }
TableViewColumn{ role: "author" ; title: "Author" ; width: 200 }

My guess: I need to add such a line as well.我的猜测:我也需要添加这样一行。 However, I could not figure out where to get the role from the C++ QStandardItemModel ?但是,我无法弄清楚从 C++ QStandardItemModel获得角色的位置? Is it even necessary to set a role?甚至有必要设置角色吗? At least the QWidgets examples with the "classic" QTreeView just set the model and everything was fine...至少带有“经典” QTreeView的 QWidgets 示例只是设置了模型,一切都很好......

You can subclass QStandardItemModel and re-implement roleNames() to define your desired role names :您可以QStandardItemModel并重新实现roleNames()以定义所需的角色名称:

#include <QStandardItemModel>

class MyModel : public QStandardItemModel
{

public:

    enum Role {
        role1=Qt::UserRole,
        role2,
        role3
    };


    explicit MyModel(QObject * parent = 0): QStandardItemModel(parent)
    {
    }
    explicit MyModel( int rows, int columns, QObject * parent = 0 ): QStandardItemModel(rows, columns, parent)
    {
    }

    QHash<int, QByteArray> roleNames() const
    {
         QHash<int, QByteArray> roles;
         roles[role1] = "one";
         roles[role2] = "two";
         roles[role3] = "three";

         return roles;
    }
};

After adding items you can set data to the model like :添加项目后,您可以将数据设置为模型,例如:

model->setData(model->index(0,0), "Data 1", MyModel::role1);
model->setData(model->index(0,1), "Data 2", MyModel::role2);
model->setData(model->index(0,2), "Data 3", MyModel::role3);

Now in the qml you can access different columns by the role names :现在在 qml 中,您可以通过角色名称访问不同的列:

TableView {

    TableViewColumn {title: "1"; role: "one"; width: 70 }
    TableViewColumn {title: "2"; role: "two"; width: 70   }
    TableViewColumn {title: "3"; role: "three"; width: 70 }

    model: myModel

}

Adding data to the model as in Nejats answer did not work in my case (Qt 5.5).在我的情况下(Qt 5.5),像 Nejats 回答那样向模型添加数据不起作用。 The data had to be added as follows:数据必须添加如下:

QStandardItem* it = new QStandardItem();
it->setData("data 1", MyModel::role1);
it->setData("data 2", MyModel::role2);
it->setData("data 3", MyModel::role3);
model->appendRow(it);

A complete example for the answer of Nejat with this modificaiton follows (also avilable here: https://github.com/psimona/so-qt-QStandardItemModel-Example )下面是 Nejat 对此修改的回答的完整示例(也可在此处获得: https : //github.com/psimona/so-qt-QStandardItemModel-Example

main.cpp

#include <QApplication>
#include <QQmlApplicationEngine>
#include <QtQml>

// QStandardItemModel derived class
#include <QStandardItemModel>
class MyModel : public QStandardItemModel {

public:
    enum Role {
        role1=Qt::UserRole,
        role2,
        role3
    };

    explicit MyModel(QObject * parent = 0): QStandardItemModel(parent){}
    explicit MyModel( int rows, int columns, QObject * parent = 0 )
        : QStandardItemModel(rows, columns, parent){}

    QHash<int, QByteArray> roleNames() const{
         QHash<int, QByteArray> roles;
         roles[role1] = "one";
         roles[role2] = "two";
         roles[role3] = "three";
         return roles;
    }
};


// Main
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);


    MyModel* model = new MyModel;


    QStandardItem* it = new QStandardItem();
    it->setData("data 1", MyModel::role1);
    it->setData("data 2", MyModel::role2);
    it->setData("data 3", MyModel::role3);
    model->appendRow(it);

    it = new QStandardItem();
    it->setData("more data 1", MyModel::role1);
    it->setData("more data 2", MyModel::role2);
    it->setData("more data 3", MyModel::role3);
    model->appendRow(it);

    QQmlApplicationEngine engine;
    // register cpp model with QML engine
    engine.rootContext()->setContextProperty("myModel", model);

    // Load qml file in QML engine
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

main.qml

import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2

ApplicationWindow {
    title: qsTr("Hello World")
    width: 640
    height: 480
    visible: true

    TableView {

        anchors.fill: parent

        TableViewColumn {title: "1"; role: "one"; width: 150 }
        TableViewColumn {title: "2"; role: "two"; width: 150   }
        TableViewColumn {title: "3"; role: "three"; width: 150 }

        model: myModel

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM