簡體   English   中英

在C ++中調用QQuickItem(TextArea)的方法

[英]Invoke methods of QQuickItem (TextArea) in C++

是否可以在C ++中調用QQuickItem的方法。

我的想法是使用Qml創建一個帶有圖形UI的程序。 我想在這個UI上登錄。 現在我在其上放置了一個TextArea,並通過append()方法添加日志條目。 使用此方法非常簡單,但我不知道如何在C ++中執行此操作。 這是我的計划的開始。 我通過一個額外的“JavaScript”函數解決了這個程序,但我對此並不滿意。

main.qml

import QtQuick 2.0
import QtQuick.Controls 1.0

Item {
    id: root
    width: 400
    height: 400

    signal requestMessage()

    function addLine() {
        log.append(addField.text.toString())
        addField.text = ""
    }

    function cMessage(msg) {
        log.append(msg)
    }


    TextArea {
        id: log
        x: 20
        y: 28
        objectName: "log"
        width: 340
        height: 200

        anchors.right: parent.right
        anchors.rightMargin: 20
        anchors.left: parent.left
        anchors.leftMargin: 20
    }

    TextField {
        id: addField
        x: 20
        y: 252
        height: 25

        anchors.right: addButton.left
        anchors.rightMargin: 10
        anchors.left: parent.left
        anchors.leftMargin: 20
    }

    Button {
        id: addButton
        x: 284
        y: 252
        width: 96
        height: 27

        text: "Hinzufügen"

        anchors.right: parent.right
        anchors.rightMargin: 20

        MouseArea {
            anchors.fill: parent
            onClicked: addLine()
        }
    }

    Button {
        id: bindingButton
        x: 239
        y: 290

        text: "Nachricht von C++"

        anchors.right: parent.right
        anchors.rightMargin: 20

        MouseArea {
            anchors.fill: parent
            onClicked: requestMessage()
        }
    }
}

main.cpp中

#include <QGuiApplication>
#include <qtquick2applicationviewer.h>
#include <QQuickItem>
#include <QObject>
#include "myclass.h"

int main(int argc, char *argv[])
{
    QGuiApplication a(argc, argv);
QtQuick2ApplicationViewer viewer;
viewer.setMainQmlFile(QStringLiteral("qml/Bindings/main.qml"));
QQuickItem *item = viewer.rootObject();

MyClass myClass;
myClass.setViewer(&viewer);
QObject::connect(item, SIGNAL(requestMessage()), &myClass, SLOT(treatMessage()));

viewer.show();
return a.exec();

}

最后是myclass.h

#ifndef MYCLASS_H
#define MYCLASS_H

#include <QObject>
#include <QQuickItem>
#include <qtquick2applicationviewer.h>

class MyClass : public QObject
{
    Q_OBJECT
public:
    explicit MyClass(QObject *parent = 0);
    void setViewer(QtQuick2ApplicationViewer *newViewer) {
        this->viewer = newViewer;
    }

signals:

public slots:
    void treatMessage() {
        QQuickItem *root = viewer->rootObject();
        QVariant message = "hello from the other side";
        QMetaObject::invokeMethod(root, "cMessage", Q_ARG(QVariant, message));
    }

protected:
    QtQuick2ApplicationViewer * viewer = 0;
};

#endif // MYCLASS_H

那么有人知道一種更優雅地執行此任務的方法嗎? 或者任何人都可以告訴我如何調用append()方法?

問候

您絕對可以從QML方面調用QML項目上的方法。 請參閱此處的文檔: http//qt-project.org/doc/qt-5.1/qtqml/qtqml-cppintegration-interactqmlfromcpp.htmls

你已經完成了大部分工作,你只需要通過以下方式找到你的實際TextArea:

  1. 將TextArea上的objectName屬性設置為“log”。
  2. rootObject()上調用findChild(QStringLiteral("log")) 請參閱: http//qt-project.org/doc/qt-5.1/qtqml/qtqml-cppintegration-interactqmlfromcpp.html#accessing-loaded-qml-objects-by-object-name
  3. 一旦有了對它的引用,就可以在其上使用QMetaObject::invokeMethod來調用append方法。 請參閱: http//qt-project.org/doc/qt-5.1/qtqml/qtqml-cppintegration-interactqmlfromcpp.html#invoking-qml-methods

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM