如何迭代QVariant?

时间:2015-09-29 23:00:20

标签: c++ qt qml

我确实将我的QTCreator更新为5.5并创建了一个新项目,在实现中我没有得到QML中的QList值(来自信号调用):

类别:

...

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

    QApplication application(argc, argv);

    const QString mainQmlApp = QStringLiteral("qrc:///Exemplo.qml");
    QQuickView view;

    qmlRegisterType<ExemploController>("org.qtproject.example", 1, 0, "ExemploController");

    view.setSource(QUrl(mainQmlApp));
    view.setResizeMode(QQuickView::SizeRootObjectToView);

    QObject::connect(view.engine(), SIGNAL(quit()), qApp, SLOT(quit()));
    view.setGeometry(QRect(100, 100, 400, 400));
    view.show();

    return application.exec();
}

控制器:

标题文件:

...
class ExemploController : public QWidget
{
    Q_OBJECT
public:
    explicit ExemploController(QWidget *parent = 0);
    Q_INVOKABLE void getMatrix();

signals:
    void receiveMatrix(QList <QList <double> > matrix);

public slots:
};

实现:

...
QList <QList <double> > net;
for(int i = 0; i < 6; i++){
    QList<double> colorMatrix;
    colorMatrix << 1 << 2 << 1 << 2 << 2 << 2;
    net << colorMatrix;
}
emit receiveMatrix(net);
...

并且......在QML中:

...
ExemploController {
    id: exemplo
    onReceiveMatrix: { 
        console.log(matrix+" "+matrix.length); //this returns 'qml: QVariant(QList<QList<double> >) undefined'
    }
}
...

我如何获得这些价值?

1 个答案:

答案 0 :(得分:1)

我们有QVariantListQVariantMap,可以更轻松地在QML中迭代值。您可以尝试使用QVariantList代替QList

QVariantList and QVariantMap to JavaScript Array and Object