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:

    main.qml
    @Rectangle {

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

    main.cpp
    @QApplication app(argc, argv);

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

    @

    control.h
    @class Control : public QObject
    {
    Q_OBJECT

    Q_PROPERTY(QString input READ getInput WRITE setInput NOTIFY inputChanged)
    

    public:
    explicit Control(QObject *parent = 0);

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

    private:
    QString m_input;
    };
    @

    How can I fix my signal? Why fails?

    Thanks a lot.


  • Moderators

    Hi, and welcome to the Qt Dev Net!

    [quote]
    @
    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.