从c++ Qt 5.2.1修改QML文本框中的文本

时间:2022-07-06 00:00:06

I'm a little new to QML and C++ in general, but I'm learning quick. I'm a bit stumped on something though. I'm trying to make an application that reads text from a QML TextInput text field, and writes the data to a text area. I'm not sure what is going wrong, but upon reading the text I get the error "Unable to assign QQuickTextInput to QString" - and I'm a bit stumped on how to write back to a text area. If someone could shed some light, that would be appreciated. I've already been through the QML/C++ binding page, and that doesn't seem to help. I'm wondering if that page was written for an older version of Qt... Here's my code thus far:

总的来说,我对QML和c++有点陌生,但我学得很快。不过我有点不太明白。我正在尝试创建一个应用程序,从QML TextInput文本字段中读取文本,并将数据写入文本区域。我不确定是哪里出了问题,但在阅读本文时,我发现了错误“无法将QQuickTextInput分配给QString”——而且我在如何回写文本区域方面有些困难。如果有人能给我一些启发,那将是值得感激的。我已经浏览了QML/ c++绑定页面,这似乎没有帮助。我想知道那页是不是为一个旧版本的Qt写的……下面是我的代码:

QML:

QML:

 import QtQuick 2.0
import QtMultimedia 5.0
import QtQuick.Controls 1.1
import QtQuick.Window 2.1
import QtWinExtras 1.0
import QtQuick.Layouts 1.1

Rectangle {
    id: main
    width: 640
    height: 480
    signal onClicked_button

    TextInput {
        id: textbocks
        objectName: textbocks
        x: 280
        y: 230
        width: 80
        height: 20
        text: "Text Input"
        font.pixelSize: 12

        Button {
            id: button1
            x: -10
            y: 45
            text: "Button"
            onClicked:main.onClicked_button()
        }
    }

    TextField {
        id: textField1
        x: 257
        y: 329
        placeholderText: qsTr("Text Field")
    }
}

header (class - originally designed to write back to text input):

标题(类-最初设计用来写回文本输入):

#ifndef TEST_H
#define TEST_H

#include <QObject>
#include <QtCore>
#include <QString>
#include <QQuickView>
#include <QQuickItem>
#include <QQmlComponent> // for accessing QML from C++
#include <QQmlContext>   // for accessing C++ from QML

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

signals:

public slots:

    void test_button(){
        QQuickView *view = new QQuickView(QUrl("test.qml"));
        view->show();
        QQuickItem *item = view->rootObject();

        QObject *item = object->findChild<QObject*>("textbocks");
        textbox->setProperty("text", str);

        delete textbox;
        return;}
private:
    bool switched;
    bool textOutput;
    QString str = "Hello World";
};

#endif // TEST_H

And main .cpp file:

和主要. cpp文件:

#include "test.h"
#include <QGuiApplication>
#include <QtCore>
#include <QObject>
#include <QQuickItem>
#include <QQmlComponent>
#include <QQmlContext>
#include <QQuickView>
#include <QDeclarativeEngine>
#include <QDeclarativeComponent>
#include <QDeclarativeContext>


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


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


    QGuiApplication app(argc, argv);

    QQuickView *view = new QQuickView(QUrl("test.qml"));
    view->show();
    QQuickItem *item = view->rootObject();

    test *funcs = new test();

    // connect button signals to their slots:
    QObject::connect(item, SIGNAL(buttonClicked_button()), funcs, SLOT(test_button()));







    delete funcs;
    return 0;
}

Any help would be appreciated. Some of this is still a bit abstract for me... Thanks!!

如有任何帮助,我们将不胜感激。其中一些对我来说仍然有点抽象……谢谢! !

2 个解决方案

#1


2  

as I noticed you are creating two QQuickViews from the same QML file "test.qml" :

正如我注意到的,您正在从同一个QML文件“测试”中创建两个qquickview。qml”:

1.In the main function (main.cpp)

1。在主函数中(main.cpp)

2.and in the TEST_BUTTON function (test.h)

2。在TEST_BUTTON函数(test.h)中

which are different QObject . Another issues are that :

它们是不同的QObject。另一个问题是:

  • you are assigning an object of type TextInput to the objectName property (test.qml) which is a String

    您正在将TextInput类型的对象分配给objectName属性(test.qml),该属性是一个字符串

  • you are deleting (as Yekmen mentioned earlier) the object (textbox (test.h) = textbocks (test.qml)) which in the mean time you are assigning to it the string str="hello world" via setProperty() function . Ahh forgot one, the slot name in C++ side which must be identical to the one declared in QML

    您正在删除(正如前面提到的Yekmen)对象(textbox (test.h) = textbocks (test.qml)),同时通过setProperty()函数将字符串str="hello world"分配给对象。啊,忘记了一个,c++端的槽名,它必须与QML中声明的槽名相同

#2


0  

I think you should look this method :

我认为你应该看看这个方法:

 Connections {
 target: area
 onClicked: foo(parameters)

}

}

For example : area is your c++ object.( qmlRegisterType(myclass) ) area send a signal and after you can lauch your function. I think is more easy.

例如:area就是你的c++对象。(qmlRegisterType(myclass))区域会发送一个信号,然后在您能够称赞您的功能之后。我觉得比较简单。

#1


2  

as I noticed you are creating two QQuickViews from the same QML file "test.qml" :

正如我注意到的,您正在从同一个QML文件“测试”中创建两个qquickview。qml”:

1.In the main function (main.cpp)

1。在主函数中(main.cpp)

2.and in the TEST_BUTTON function (test.h)

2。在TEST_BUTTON函数(test.h)中

which are different QObject . Another issues are that :

它们是不同的QObject。另一个问题是:

  • you are assigning an object of type TextInput to the objectName property (test.qml) which is a String

    您正在将TextInput类型的对象分配给objectName属性(test.qml),该属性是一个字符串

  • you are deleting (as Yekmen mentioned earlier) the object (textbox (test.h) = textbocks (test.qml)) which in the mean time you are assigning to it the string str="hello world" via setProperty() function . Ahh forgot one, the slot name in C++ side which must be identical to the one declared in QML

    您正在删除(正如前面提到的Yekmen)对象(textbox (test.h) = textbocks (test.qml)),同时通过setProperty()函数将字符串str="hello world"分配给对象。啊,忘记了一个,c++端的槽名,它必须与QML中声明的槽名相同

#2


0  

I think you should look this method :

我认为你应该看看这个方法:

 Connections {
 target: area
 onClicked: foo(parameters)

}

}

For example : area is your c++ object.( qmlRegisterType(myclass) ) area send a signal and after you can lauch your function. I think is more easy.

例如:area就是你的c++对象。(qmlRegisterType(myclass))区域会发送一个信号,然后在您能够称赞您的功能之后。我觉得比较简单。