Good day! I can not solve the problem:

I develop the menu on QTQuick 5.7 There are several QML forms: main.qml, Page.qml, Options.qml ...

enter image description here

There is a button in main.qml:

Button { id: button1 x: 178 y: 227 text: qsTr("PressMe") onClicked: { button1.text = "Clicked" var result = testclasses.getSomeProperty() } } 

When you press a button, a certain testclasses function is called. getSomeProperty ()

The class with this function is defined in main.h:

 class TestClass : public QObject { Q_OBJECT public: Q_INVOKABLE void getSomeProperty(); }; 

The method is implemented as:

  void TestClass::getSomeProperty() { qDebug() << "Event is here"; QQuickView view; view.setSource(QUrl(QLatin1String("qrc:/Page1Form.ui.qml")) ); view.show(); QObject *object = view.rootObject(); QObject *txttxt = object->findChild<QObject*>("texttochange"); if (txttxt){ qDebug() << txttxt->property("text"); txttxt->setProperty("text", "текст изменен"); qDebug() << txttxt->property("text"); } } 

An instance of the class is created in Main:

  int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; QQmlContext* context = engine.rootContext(); TestClass tc; context->setContextProperty("testclasses", &tc); engine.load(QUrl(QLatin1String("qrc:/main.qml"))); app.exec(); } 

Made by examples from the Internet. As a result, when you press an infinite number of times on the button, the log is displayed in this way

Debug \ debug \ Memograph.exe ... QML debugging is enabled. Only use this in a safe environment. Event is here QVariant (QString, "Home page") QVariant (QString, "text changed") Event is here QVariant (QString, "Home page") QVariant (QString, "text changed") Event is here QVariant (QString, " Start Page ") QVariant (QString," Text Changed ")

That is, our text seems to be changing, but in fact nothing changes. Tell me how to correctly change the attributes of qml forms from C ++ code. I work with the PLO very recently, everything is hard, but it must be done.

    1 answer 1

    corrected, earned:

    myclass.h:

      #ifndef MYCLASS_H #define MYCLASS_H #include <QObject> #include <QDebug> class MyClass : public QObject { Q_OBJECT public: MyClass(QObject *QMLObject) : viewer(QMLObject) {} signals: public slots: void buttonClicked(const QString& in); protected: QObject *viewer; }; #endif // MYCLASS_H 

    main.cpp

      #include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include "myclass.h" int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); QObject* root = engine.rootObjects()[0]; MyClass myClass(root); engine.rootContext()->setContextProperty("_myClass", &myClass); return app.exec(); } 

    myclass.cpp

      #include "myclass.h" void MyClass::buttonClicked(const QString& in) { qDebug() << in;//Просто выведем информацию строки in в консоль Debug. Просто так. // //Найдем строки ввода QObject* textinput1 = viewer->findChild<QObject*>("inptext"); QString str1=(textinput1->property("text")).toString(); qDebug() << str1; textinput1->setProperty("text", "Yeah"); ////////************ QObject* rect = viewer->findChild<QObject*>("rectcolortochange"); QString str12=(rect->property("width")).toString(); qDebug() << str12; rect->setProperty("width", 100); } 

    Well, in order to trigger this event, for example, on a button click handler, I do so

      import QtQuick 2.4 OptionsFormForm { button1.onClicked: { _myClass.buttonClicked("Worked?") } } 

    This is not the first time in my life - the question should be voiced, the answer to it is the same, although it has been toiling for two days

    • Moreover, the properties can be changed for any form in the project, so long as the objectName attribute is - Andrey Shmelev