【问题标题】:How to send parameters from the QML View to the Model with Qt?如何使用 Qt 将参数从 QML 视图发送到模型?
【发布时间】:2014-12-12 08:40:20
【问题描述】:

我正在尝试从 QML 视图向我的模型发送参数,但我被卡住了。

请注意,现在我可以将 QML 视图与模型连接起来,但为了不重复我的代码,我想从视图中发送一些参数(一个 QString),并在模型中决定发送回什么到视图。

文件解析器.h

#ifndef FILEPARSER_H
#define FILEPARSER_H

#include <QObject>

class FileParser : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QString file READ file WRITE setFile NOTIFY fileChanged)

public:
    explicit FileParser(QObject *parent = 0);
    FileParser(const QString &file, QObject *parent=0);

    QString file() const;
    void setFile(const QString &file);

    QString fileFinder(QString file);

signals:
    void fileChanged();

private:
    QString m_file;

};

#endif // FILEPARSER_H

文件解析器.cpp

#include "FileParser.h"

FileParser::FileParser(QObject *parent) :
    QObject(parent)
{
}

FileParser::FileParser(const QString &file, QObject *parent)
    : QObject(parent), m_file(file)
{
}

QString FileParser::file() const
{
    return m_file;
}

void FileParser::setFile(const QString &file)
{
    if (m_file != file){
        m_file = file;
        emit fileChanged();
    }
}

QString FileParser::fileFinder(QString file)
{
    if (file == “fileA“){
        return “test file A“;
    }
    return “test file B“;
}

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>

#include <qqmlcontext.h>
#include <QtQuick/qquickview.h>
#include <QString>

#include "fileparser.h"

QString getFile(QString file)
{
    FileParser *fileParser = new FileParser();
    return fileParser->fileFinder(file);
}

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

    QQuickView view;
    view.setResizeMode(QQuickView::SizeRootObjectToView);
    QQmlContext *ctxt = view.rootContext();
    ctxt->setContextProperty("_parsedText", getFile(“Default“));

    view.setSource(QUrl("qrc:main.qml"));
    view.show();

    return app.exec();
}

最后 QML 看起来像这样:

main.qml

import QtQuick 2.2

Item {
    width: 400; height: 400

    Text {
        x: 0; y: parent.height
        anchors.fill: parent
        text: _parsedText // returns "test file B", now how can I send a parameter???
    }
}

【问题讨论】:

    标签: c++ qt model-view-controller arguments qml


    【解决方案1】:

    您只是将 getFile() 返回的字符串公开为上下文属性。尝试将整个解析器设置为上下文属性,并在 QML 代码中访问 file 属性。

    // main code
    int main(int argc, char *argv[])
    {
      QGuiApplication app(argc, argv);
    
      QQuickView view;
      view.setResizeMode(QQuickView::SizeRootObjectToView);
      QQmlContext *ctxt = view.rootContext();
      FileParser parser("Default");
      ctxt->setContextProperty("parser", &parser);
    
      view.setSource(QUrl("qrc:main.qml"));
      view.show();
    
      return app.exec();
    }
    

    这样 QML 代码可以将 file 属性绑定到其他属性并监视文件更改。当您想更改 file 属性时,只需为其分配一个新值即可。

    // QML code
    import QtQuick 2.2
    
    Item {
      width: 400; height: 400
    
      TextInput { // TextInput to allow an user change the text
          x: 0; y: parent.height
          anchors.fill: parent
          text: parser.file // Read and watch for changes
          onTextChanged: parser.file = text // this will call parser.setFile()
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-26
      • 2012-07-15
      • 1970-01-01
      • 2018-05-20
      • 1970-01-01
      相关资源
      最近更新 更多