Connect qt with qml

  • Hello. I am making an easy program. I have a qml file with one button an one text field. When I write in the text field and click the button I want to the cpp part write a message in console. My problem is Qt program returns me
    QObject::connect: No such slot QApplication::newValue() in ../Prueba/main.cpp:27

    My code is:

    @Rectangle {

    signal valueIntroduced();
    width: 440;
    height: 380;
    TextField {...}
    Button {
        id: bot;
        width: 100;
        height: 50;
        anchors.centerIn: parent;
        onClicked: {

    @QApplication app(argc, argv);

    QQmlEngine *engine_ = new QQmlEngine();
    QQuickView *view = new QQuickView(engine_, NULL);
    Control *obj = new Control();
    view->rootContext()->setContextProperty( "obj_", obj );
    QObject::connect(view->rootObject(), SIGNAL(valueChanged(QString)),
                     &app, SLOT(newValue()) );
    return app.exec();


    @class Control : public QObject

    Q_PROPERTY(QString input READ getInput WRITE setInput NOTIFY inputChanged)

    explicit Control(QObject *parent = 0);

    public slots:
    void newValue() { qDebug() << "The text field has been modified"; }

    QString m_input;

    How can I fix my signal? Why fails?

    Thanks a lot.

  • Moderators

    Hi, and welcome to the Qt Dev Net!

    QObject::connect(view->rootObject(), SIGNAL(valueChanged(QString)),
    &app, SLOT(newValue()) );
    [/quote]Which class should receive the signal? You need to connect to obj, not &app.

  • It is already running. Thanks a lot.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.