Cpper
C/C++高级工程师 Android高级软件工程师 IT集成工程师 音频工程师 熟悉c,c++,java,c#,py,js,asp等多种语言 程序猿
    auto box = new QVBoxLayout();
    qml = new QQuickWidget();
    setLayout(box);

    qml->setResizeMode(QQuickWidget::SizeRootObjectToView);
    qml->setClearColor(Qt::gray);

    QMLObjectInterface* face = new QMLObjectInterface(this);
    qml->engine()->rootContext()->setContextProperty("cplusplusObject",face);

    qml->setSource(QUrl("QML.qml"));
    foreach(auto error, qml->errors())
        qDebug() << "error:" << error;

    auto object = qml->rootObject();
    object->setProperty("width",480);
   
    auto items = object->findChildren<QQuickItem*>("button");
   
    if (!items.isEmpty())
        items[0]->setProperty("text",QStringLiteral("按键"));
 

    box->addWidget(qml);
QML
import QtQuick 2.11
import QtQuick.Controls 2.1
import QtQuick.Controls.Styles 1.4
import QtQuick.Layouts 1.3
import QtQuick.Window 2.11
import "qmls" as QMLS
import cplusplus 1.0

Rectangle
{
    id: base
    width:640
    height:480

    ColumnLayout
    {
        anchors.fill:parent
        anchors.margins: 6
        spacing: 4

        Rectangle
        {
            id:image
            Layout.fillWidth: true
            Layout.fillHeight: true
            color:"#cdcdc0"

            TextEdit
            {
                id:text
                anchors.centerIn: parent
                width:parent.width
                text:"请点击下方按键"
                verticalAlignment: Text.AlignVCenter
                horizontalAlignment: Text.AlignHCenter
                wrapMode: Text.WordWrap
                font.pointSize: 12
            }
        }

        RowLayout
        {
            Rectangle
            {
                Layout.fillWidth: true
            }

            Button
            {
                id:button
                property int index: 0
                property string buttonText:"Click"
                text:buttonText
                objectName: "button"
                onClicked:
                {
                    text.text = cplusplusObject.getString()
                }
            }

        }
    }
}
posted on 2019-08-15 09:40 ccsdu2009 阅读(104) 评论(0)  编辑 收藏 引用 所属分类: QT编程

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理