Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

signals and slots in QT Quick , firstqml to cpp and cpp to second qml



  • hello,
    I am trying to make an application where input.qml will send some data via a signal to the main.cpp and it will connect to handler.cpp via the connect function, and in return the main.cpp will send the received data to another file main.qml via signals and slots and it displays it. This is just the basic concept, i would like to know how to proceed.





  • @6thC please look at my code

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QtQuick>
    #include "handler.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        QObject *topLevel = engine.rootObjects().value(0);
        QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
        QQmlApplicationEngine engine1;
        engine1.load(QUrl(QStringLiteral("qrc:/inputfile.qml")));
        handler handler;
        QObject *topLevel1 = engine1.rootObjects().value(0);
        QQuickWindow *window1 = qobject_cast<QQuickWindow *>(topLevel1);
    
        QObject::connect(window1, SIGNAL(sendText(QString,QString,QString,QString)),
                         &handler, SLOT(handleText(QString,QString,QString,QString)));
        QObject::connect(&handler, SIGNAL(writeText(QVariant,QVariant,QVariant,QVariant)),
                             window, SLOT(writeText(QVariant,QVariant,QVariant,QVariant)));
    
        return app.exec();
    }
    

    i am using QQmlApplicationEngine and it is working fine, but I want to use QQuick view and it is giving me error, can you tell me why that is happening

    QQuickView view;
    view.setSource(QUrl(QStringLiteral("qrc:/main.qml")));
    QQuickItem* x = view.rootObject();
    QQuickView view1;
    view.setSource(QUrl(QStringLiteral("qrc:/inputfile.qml")));
    QQuickItem* x1 = view.rootObject();
    

    this is the part of code when replaced with the qml file giving an error. please let me know, thanks


  • Lifetime Qt Champion

    @Nazeeruddin said in signals and slots in QT Quick , firstqml to cpp and cpp to second qml:

    it is giving me error

    What about posting the actual error?



  • @jsulm

    libpng warning: iCCP: known incorrect sRGB profile
    QQuickView does not support using a window as a root item. 
    
    If you wish to create your root window from QML, consider using QQmlApplicationEngine instead. 
    
    QQuickView does not support using a window as a root item. 
    
    If you wish to create your root window from QML, consider using QQmlApplicationEngine instead. 
    
    QObject::connect: Cannot connect (nullptr)::sendText(QString,QString,QString,QString) to handler::handleText(QString,QString,QString,QString)
    QObject::connect: Cannot connect handler::writeText(QVariant,QVariant,QVariant,QVariant) to (nullptr)::writeText(QVariant,QVariant,QVariant,QVariant)
    

  • Lifetime Qt Champion

    @Nazeeruddin said in signals and slots in QT Quick , firstqml to cpp and cpp to second qml:

    QQuickView does not support using a window as a root item.

    If you wish to create your root window from QML, consider using QQmlApplicationEngine instead.

    This tells you what the problem is.



  • @jsulm
    Can you please explain what it means, the thing is I am new to QT and want a clear idea, Thanks


  • Lifetime Qt Champion

    @Nazeeruddin "QQuickView does not support using a window as a root item" - well, it tells you that if you use QQuickView (what you do) you can't have Window as top element in your QML (root element).

    "If you wish to create your root window from QML, consider using QQmlApplicationEngine instead." - and this tells you what you can use instead of QQuickView.



  • I'd also suggest using the https://wiki.qt.io/New_Signal_Slot_Syntax
    The old syntax isn't as clean/concise - also doesn't provide you with compile time checks/safeties.

    If you are like me: you'll break things more often than you want to and letting the compiler do that work instead means you don't have to.

    Honestly, I'd start with the example projects bundled inside Qt Creator. Or even just a new application based on QtQuick template will get you a QML app started. Then you can use those integration links to do more.

    The examples apps will get you something basic that starts and you can witness it working, take a copy of and start pulling that apart to understand.


Log in to reply