集成C++和QML。Qt 5.4 我在阅读QT文档,试图找出一种方法,用Qt快速UI QML创建的UI与C++代码函数交互…等等

集成C++和QML。Qt 5.4 我在阅读QT文档,试图找出一种方法,用Qt快速UI QML创建的UI与C++代码函数交互…等等,c++,qml,qt5.4,C++,Qml,Qt5.4,我已经阅读了5到6个类似的问题,但是我有点困惑,我不知道从哪里开始,或者先做什么。 如果有人能抽出时间,列出实现这一目标所需的步骤,我将不胜感激 我到目前为止所做的一切。我尝试做…>添加新的项目> C++类,但是我失败了,错误地说:没有向项目添加一个或多个文件。cpp和.h已创建,它们位于其他项目文件所在的文件夹中,但未包含在项目中。 我想做的只是一些简单的事情,比如通过C++函数或其他任何方式来改变文本的文本。 //Test.qml main.qml import QtQuick 2.

我已经阅读了5到6个类似的问题,但是我有点困惑,我不知道从哪里开始,或者先做什么。 如果有人能抽出时间,列出实现这一目标所需的步骤,我将不胜感激

我到目前为止所做的一切。我尝试做…>添加新的项目> C++类,但是我失败了,错误地说:没有向项目添加一个或多个文件。cpp和.h已创建,它们位于其他项目文件所在的文件夹中,但未包含在项目中。 我想做的只是一些简单的事情,比如通过C++函数或其他任何方式来改变文本的文本。 //Test.qml main.qml

    import QtQuick 2.1
    import QtQuick.Window 2.0

Rectangle {
     id: rootRect
    width: Screen.width/2
    height: Screen.height/2
    color: "gray"


    Button{}

    Rectangle{
        id: textField
        width: 120
        height: 40
        color: "white"
        x:274; y: 61
        border.color: "blue"
        border.width: 4
        radius: 2

    }

    TextEdit {

        id: display
        x: 274
        y: 61
        width: 80
        height: 20
        text: qsTr("Text Edit")
        font.pixelSize: 22
        color: "black"
        anchors.centerIn: textField

    }

    Rectangle{
        id: inputField
        width: textField.width
        height: textField.height
        border.color: "green"
        border.width: 3
        color: "white"
        x: 140; y: 61
    }

    TextEdit{
        id: input
        color: "red"
        font.pixelSize: 30
        anchors.centerIn: inputField
        text: "Some Text"


    }

}
//按钮.cpl

import QtQuick 2.0
import QtQuick.Window 2.0


Item {

    property string defaultText: "New Text"


    Rectangle{
    id: button
    width: rootRect.width/6
    height: rootRect.height/8
    color: "black"
    x: 200; y: 200
    radius: 10

    }

    MouseArea{
        id: buttonClickArea
        width: 0
        anchors.rightMargin: 0
        anchors.bottomMargin: 0
        anchors.fill: button

        onClicked: {

                display.text = defaultText
        }
    }

}

感谢您抽出时间阅读此内容和/或任何回复。

使用Qt 5.4.0和Qt Creator 3.3.0,创建新项目:

单击新建项目 Qt快速应用 单击选择。。。 命名项目并选择放置它的位置 单击下一步 从下拉菜单中选择Qt Quick 2.4 单击下一步 选择所需的套件 单击下一步 单击“完成” 现在,您将看到open main.qml文件,其中包含以下代码:

import QtQuick 2.4
import QtQuick.Window 2.2

Window {
    visible: true
    MainForm {
        anchors.fill: parent
        mouseArea.onClicked: {
            Qt.quit();
        }

    }
}
将其更改为:

import QtQuick 2.4
import QtQuick.Window 2.2

//### New Code ###

import QtQuick.Controls 1.3

//################

Window {
    id: window1
    visible: true

    //### New Code ###

    width: 400
    height: 500

    TextArea {
        id: textArea
        readOnly: true
        anchors.bottom: textInput.top
        anchors.bottomMargin: 6
        anchors.right: parent.right
        anchors.rightMargin: 8
        anchors.left: parent.left
        anchors.leftMargin: 7
        anchors.top: parent.top
        anchors.topMargin: 7
    }

    TextField {
        id: textInput
        y: 470
        height: 23
        anchors.right: sendButton.left
        anchors.rightMargin: 6
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 7
        anchors.left: parent.left
        anchors.leftMargin: 7
    }

    Button {
        id: sendButton
        x: 328
        y: 470
        width: 64
        height: 23
        text: qsTr("Send")
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 7
        anchors.right: parent.right
        anchors.rightMargin: 8

        onClicked: {
            CppClass.sendMessage(textInput.text, textArea);
            textInput.text = "";
        }
    }

    //################
}
#ifndef CPPCLASS_H
#define CPPCLASS_H

#include <QObject>

//### New Code ###

#include <QQuickItem>
#include <QQuickTextDocument>
#include <QTextDocument>

//################

class CppClass : public QObject
{
    Q_OBJECT
public:
    explicit CppClass(QObject *parent = 0);
    ~CppClass();

    //### New Code ###

    Q_INVOKABLE void sendMessage(const QString &msg, QQuickItem *textArea);

    //################

signals:

public slots:
};

#endif // CPPCLASS_H
#include "cppclass.h"

CppClass::CppClass(QObject *parent) : QObject(parent)
{

}

CppClass::~CppClass()
{

}
//### New Code ###

void CppClass::sendMessage(const QString &msg, QQuickItem *textArea)
{
    QTextDocument *textDocument = textArea->property("textDocument").value<QQuickTextDocument*>()->textDocument();

    textDocument->setHtml(textDocument->toHtml() + "\n<b>Text sent to Cpp side:</b> <i>" + msg + "</i>");
}

//################
#include <QGuiApplication>
#include <QQmlApplicationEngine>

//### New Code ###

#include <QQmlContext>

#include "cppclass.h"

//################

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

    QQmlApplicationEngine engine;

    //### New Code ###

    CppClass cppClass;

    engine.rootContext()->setContextProperty("CppClass", &cppClass);

    //################

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

    return app.exec();
}

将C++类添加到项目中:

在项目查看器中用鼠标右键单击项目名称 单击添加新。。。 如果没有选择C++类 单击选择。。。 在类名称字段中输入CppClass 将基类设置为QObject 单击下一步 单击“完成” 打开cppclass.h并将其更改为:

import QtQuick 2.4
import QtQuick.Window 2.2

//### New Code ###

import QtQuick.Controls 1.3

//################

Window {
    id: window1
    visible: true

    //### New Code ###

    width: 400
    height: 500

    TextArea {
        id: textArea
        readOnly: true
        anchors.bottom: textInput.top
        anchors.bottomMargin: 6
        anchors.right: parent.right
        anchors.rightMargin: 8
        anchors.left: parent.left
        anchors.leftMargin: 7
        anchors.top: parent.top
        anchors.topMargin: 7
    }

    TextField {
        id: textInput
        y: 470
        height: 23
        anchors.right: sendButton.left
        anchors.rightMargin: 6
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 7
        anchors.left: parent.left
        anchors.leftMargin: 7
    }

    Button {
        id: sendButton
        x: 328
        y: 470
        width: 64
        height: 23
        text: qsTr("Send")
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 7
        anchors.right: parent.right
        anchors.rightMargin: 8

        onClicked: {
            CppClass.sendMessage(textInput.text, textArea);
            textInput.text = "";
        }
    }

    //################
}
#ifndef CPPCLASS_H
#define CPPCLASS_H

#include <QObject>

//### New Code ###

#include <QQuickItem>
#include <QQuickTextDocument>
#include <QTextDocument>

//################

class CppClass : public QObject
{
    Q_OBJECT
public:
    explicit CppClass(QObject *parent = 0);
    ~CppClass();

    //### New Code ###

    Q_INVOKABLE void sendMessage(const QString &msg, QQuickItem *textArea);

    //################

signals:

public slots:
};

#endif // CPPCLASS_H
#include "cppclass.h"

CppClass::CppClass(QObject *parent) : QObject(parent)
{

}

CppClass::~CppClass()
{

}
//### New Code ###

void CppClass::sendMessage(const QString &msg, QQuickItem *textArea)
{
    QTextDocument *textDocument = textArea->property("textDocument").value<QQuickTextDocument*>()->textDocument();

    textDocument->setHtml(textDocument->toHtml() + "\n<b>Text sent to Cpp side:</b> <i>" + msg + "</i>");
}

//################
#include <QGuiApplication>
#include <QQmlApplicationEngine>

//### New Code ###

#include <QQmlContext>

#include "cppclass.h"

//################

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

    QQmlApplicationEngine engine;

    //### New Code ###

    CppClass cppClass;

    engine.rootContext()->setContextProperty("CppClass", &cppClass);

    //################

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

    return app.exec();
}
main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>

#include <QQmlContext>

#include "cppclass.h"

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

    CppClass cppClass;

    QQmlApplicationEngine engine;

    engine.rootContext()->setContextProperty("CppClass", &cppClass);

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

    return app.exec();
}

我没有看到任何尝试编写C++部分。至于你的错误,看起来你试图把C++类文件添加到Qt快速UI项目中。此类型的项目仅包含QML部分,并使用QMLSene实用程序运行。您需要的是Qt快速应用程序。项目创建后,您将准备使用QML和C++文件的项目。现在,您可以向项目中添加C++类。请参阅如何将C++插件插入QMLBTW中,QtQuy中已经有按钮项。控件1.3几乎等同于@ FulbIIS所说的:新文件或项目…>Qt快速应用。这里有QML和C++类。您在其他答案上找到的代码可以复制到该源代码中。@BaCaRoZzo和filibis。谢谢你们两位的评论。好像我试图学习的家伙忘记提到集成C++,我们需要创建Qt快速应用程序,因为他开始的所有其他项目都是Qt QuestUI纯QML。FulbIi我已经准备好C++代码,只是想找到一种方法来连接它。至于文档,我发现它非常混乱…对于ex.在两个非常相似的示例中,包含了不同的内容…等等。我想我必须从一开始就开始挖掘文档。如果您能将这两个示例链接起来,我们可以尝试帮助您和/或为您指出不同或更详细的示例:这对我很有帮助:我可以理解如何访问TextArea并从TextField设置文本。您还可以指导我如何更改QML文本的文本吗?谢谢你!!!Text{id:initailizedlmsg Text:initailize_lbl}@DynamicRemo我已经更新了我的答案,以展示如何实现您的目标。非常感谢。你救了我一天