小编hek*_*kri的帖子

集成C++和QML.Qt 5.4

我正在阅读过去几个小时的Qt文档,试图想出一种方法来创建一个用Qt Quick UI(QML)与C++代码(函数等)进行通信(交互)的UI.

我已经在这里阅读了5或6个类似的问题,但我有点困惑,我有问题找出从哪里开始或先做什么.如果有人能花时间并列出完成这项工作所需的步骤,我会非常感激.

到目前为止我做了什么.我试着做...>添加新的Item> C++类但是我失败了,错误说:"无法将一个或多个文件添加到项目中">似乎创建了文件(.. .cpp和.h),它们位于其他项目文件但未包含在项目中的文件夹中.我想做的只是简单的事情,比如通过C++函数或任何其他可能的方式更改textedit的文本.

//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: …
Run Code Online (Sandbox Code Playgroud)

c++ qml qt5.4

6
推荐指数
1
解决办法
6182
查看次数

如何从C ++设置QML属性

我正在尝试做一个简单的任务,例如从C ++更改某些QML对象的属性(文本:),但我失败了。任何帮助表示赞赏。

我没有收到任何错误,出现了窗口,只是text属性没有(至少我认为)应有的变化。我在这里没做错什么吗?!

我正在尝试的是这样的:

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickView>
#include <QQuickItem>
#include <QQmlEngine>
#include <QQmlComponent>
#include <QString>

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

    QQmlApplicationEngine engine;


    QQmlComponent component(&engine, QUrl::fromLocalFile("main.qml"));
    QObject *object = component.create();

     engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    QString thisString = "Dr. Perry Cox";

    object->setProperty("text", thisString);  //<--- tried  instead of thisString putting "Dr. ..." but nope.
    delete object;



    return app.exec();
}
Run Code Online (Sandbox Code Playgroud)

main.qml

import QtQuick 2.2
import QtQuick.Window 2.1

Window {
    visible: true
    width: 360
    height: 360

    MouseArea {
        anchors.fill: parent …
Run Code Online (Sandbox Code Playgroud)

c++ qml qt5.4

3
推荐指数
1
解决办法
3466
查看次数

标签 统计

c++ ×2

qml ×2

qt5.4 ×2