简体   繁体   English

QML / C ++在运行时更改属性

[英]QML/C++ Change Property During Run-time

I am trying to setup my program to take data from an input field in QML and then pass that data to C++, which will be used to make in a property change. 我正在尝试设置程序以从QML中的输入字段获取数据,然后将该数据传递给C ++,后者将用于进行属性更改。 For example if the user types red in the input field the rectangle containing the input field should turn red. 例如,如果用户在输入字段中键入红色,则包含输入字段的矩形应变为红色。 The data is being received in C++ but the properties are not changing on the rectangle. 数据已用C ++接收,但属性在矩形上未更改。

Here is my code. 这是我的代码。 Any help is appreciated. 任何帮助表示赞赏。

main.qml main.qml

Rectangle{
id: textbox
radius: 15.0
height: 300
width: 300
color: "white"
border.color: "lightblue"
border.width: 5
signal qmlSignal(string msg)
property alias textColor: colorText.color

TextInput
{
    id: inputText
    anchors.horizontalCenter: textbox.horizontalCenter
    anchors.verticalCenter: textbox.verticalCenter
    anchors.bottomMargin: 25
    color : "black"
    text : "type something..."
    font.pointSize: 20
    maximumLength: 17
    inputMethodHints: Qt.ImhNoPredictiveText
    selectByMouse: true

    onAccepted: { inputText.focus = false; 
        Qt.inputMethod.hide(); 
        textbox.qmlSignal(inputText.text); 
        console.log(colorText.color) }

 }
}

main.cpp main.cpp

#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include <QtQuick>
#include <QObject>
#include <myclass.h>

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

QtQuick2ApplicationViewer viewer;
viewer.setMainQmlFile(QStringLiteral("qml/Test3/main.qml"));

QObject *item = viewer.rootObject();

MyClass test;

QObject::connect(item, SIGNAL(qmlSignal(QString)), &test, SLOT(cppSlot(QString)));

viewer.showExpanded();

return app.exec();
}

myclass.h myclass.h

#include<QObject>
#include<QDebug>
#include<QtQuick>
#include"qtquick2applicationviewer.h"

class MyClass: public QObject
{
Q_OBJECT

public:
MyClass();

public slots:
void cppSlot(const QString &msg)
{
    qDebug() << "Called the C++ slot with message:" << msg;
    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile(QStringLiteral("qml/Test3/main.qml"));

    QObject *item = viewer.rootObject();
    item->setProperty("color", "red");
}
};

To solve your problem I believe you'll want to review the lessons on extending QML with bindings. 为了解决您的问题,我相信您将需要回顾有关使用绑定扩展QML的课程。 Specifically see Chapter 3: Adding Property Bindings as this shows how to create bindings between C++ objects and QML using the Q_PROPERTY macro. 具体请参见第3章:添加属性绑定,因为这显示了如何使用Q_PROPERTY宏在C ++对象和QML之间创建绑定。

In fact I would highly recommend you complete all the chapters for QML that come with the Qt installation of Qt Creator. 实际上,我强烈建议您完成Qt Creator的Qt安装随附的QML所有章节。 The tutorial chapters can be access via the Welcome Page 可以通过“欢迎页面”访问教程的章节

  1. Select Examples 选择范例
  2. Enter " Chapter " in the search 在搜索中输入“ 章节
  3. The six chapters should be listed 应列出六章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM