Important: Please read the Qt Code of Conduct -

Have truble with binding

  • Hi community!
    I have some truble with binding.
    I whant to create a very simple programm for example, just for me. This program will show text wich i input in textbox 1 in textbox 2. I use pattern MVVM.
    My exaple :
    Thanks for help.

  • Moderators

    Hi, and welcome to the Qt Dev Net!

    Can you please post your code directly in the forum? Just add '@' before and after the code, and it will be formatted nicely. That will make it easier for everyone to read. :)

  • Yes, of course.
    My main:
    @ #include <qmlapplicationviewer.h>
    #include <QApplication>
    #include <QGuiApplication>
    #include <QQmlEngine>
    #include <qdeclarativecontext.h>
    //#include <qqmlcontext>
    #include <qqml.h>
    #include <QtQuick/qquickitem.h>
    #include <QtQuick/qquickview.h>
    #include "MainModel.h"
    #include "MainViewModel.h"

    int main(int argc, char *argv[])
    QApplication app (argc, argv); //
    // QmlApplicationViewer viewer;
    // viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    QmlApplicationViewer viewer;

    MainModel mainModel; //

    // viewer.rootContext()->setContextProperty("model", new MainViewModel(mainModel));
    // viewer.setMainQmlFile(QLatin1String("Button.qml"));
    // viewer.showExpanded();

     QDeclarativeContext *ctxt = viewer.rootContext();
     ctxt->setContextProperty("myModel", QVariant::fromValue(mainModel));
    // viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    // viewer.setMainQmlFile&#40;QLatin1String("Button.qml"&#41;&#41;;
    // viewer.showExpanded();
    QDeclarativeView *view = new  QDeclarativeView; //
    view->setSource(QUrl::fromLocalFile&#40;"Button.qml"&#41;&#41;; //

    // //view->rootContext()->setContextProperty("model", new MainViewModel(mainModel));
    view->show(); //

     return app.exec&#40;&#41;;


    Sorry for comments in code.

  • Moderators

    What do you mean by "problem with binding"? Please explain in more detail.

    By the way, you should use classes that start with "QQml" or "QQuick". "QDeclarative is old and deprecated. See

Log in to reply