c++ - QML 新手在将 C++ 后端与 QML 前端连接起来时需要一些帮助

标签 c++ qt qml qt-quick

我有 C++ 代码,用于从 XML 文件保存和检索数据。我有一些内置在 QML 中的表单,我想以这样一种方式连接:当在我的 QML 中输入数据时,处理在 C++ 中处理,并通过 QML 表单搜索产品,在 C++ 中处理和产品列表项目被交还给 QML 进行展示。

class ProcessRequests : public QObject
{
    Q_OBJECT
    Q_PROPERTY(string username READ username WRITE username)
    Q_PROPERTY(string useremail READ useremail WRITE useremail)
    Q_PROPERTY(string usercomplaint READ usercomplaint WRITE usercomplaint)


    public:

    ProcessRequests()
    {}
    ~ProcessRequests(){}

    Q_INVOKABLE void SubmitComplaint(){
     //TODO: Add Xml code to save the property values to file 

    }

};

最佳答案

我认为你的问题是 QML 和 C++ 代码之间的相互通信,你可以用这样的代码来做到这一点:

//Product.cpp
QmlApplicationViewer viewer;

QDeclarativeEngine *engine = viewer.engine();
QDeclarativeContext *context = engine->rootContext();

context->setContextProperty("Product", this);

//Your QML File
Product.YourFunction(args);

关于c++ - QML 新手在将 C++ 后端与 QML 前端连接起来时需要一些帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9136086/

相关文章:

c++ - 检测后如何切割人脸

c++ - 计算常量二维 c 数组中的行数

c++ - 如何指定库路径的偏好?

qt - 有什么办法可以延迟Qt QEvent吗?

c++:在不同链接库中定义的相同函数的运行时冲突

c++ - QML:如何从 C++ 代码正确地将属性传递给 PluginParameter 值?

qt - 如何在 BB 10 Cascades 中省略文本?

c++ - 如何从 C++ 源代码创建 DLL,以及如何在其他源代码中使用它们?

c++ - 如何与复选框操作交互? (带有 QStandardItemModel 的 QTableView)

qt - 如何将 C++ 模型暴露给 QML