Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Converting code to new Qt5 syntax of connect

Converting code to new Qt5 syntax of connect

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 647 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vlada
    wrote on last edited by vlada
    #1

    Hi,

    here is a piece of my code:

    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
    QmltoCpp *qmltocpp = new QmltoCpp(); // my C++/QML interface
    QObject *rootObject = engine.rootObjects().first();
    QObject::connect(qmltocpp, SIGNAL(showMessage(QVariant)), rootObject, SLOT(showMessage(QVariant)));
    return app.exec();
    

    I would like to update this to the new Signal/Slot syntax. But I have no idea how.

    This doesn't work:

    QObject::connect(qmltocpp, &QmltoCpp::showMessage, rootObject, &QObject::showMessage);
    

    Do I need to subclass a QObject and create new class for my rootObject? Would it help at all?

    raven-worxR 1 Reply Last reply
    0
    • V vlada

      Hi,

      here is a piece of my code:

      QGuiApplication app(argc, argv);
      QQmlApplicationEngine engine;
      engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
      QmltoCpp *qmltocpp = new QmltoCpp(); // my C++/QML interface
      QObject *rootObject = engine.rootObjects().first();
      QObject::connect(qmltocpp, SIGNAL(showMessage(QVariant)), rootObject, SLOT(showMessage(QVariant)));
      return app.exec();
      

      I would like to update this to the new Signal/Slot syntax. But I have no idea how.

      This doesn't work:

      QObject::connect(qmltocpp, &QmltoCpp::showMessage, rootObject, &QObject::showMessage);
      

      Do I need to subclass a QObject and create new class for my rootObject? Would it help at all?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @vlada said in Converting code to new Qt5 syntax of connect:

      QObject::connect(qmltocpp, &QmltoCpp::showMessage, rootObject, &QObject::showMessage);

      it doesn't work because the QObject class doesn't have a method with the signature showMessage(QVariant)
      You could cast the QObject to your class if you are sure that its of a certain type, if not it will crash.

      So the code might look like this:

      MyType* rootObject = qobject_cast<MyType*>(engine.rootObjects().first());
      QObject::connect(qmltocpp, &QmltoCpp::showMessage, rootObject, &MyType::showMessage);
      

      alternatively (but with actually no benefit to the "old" syntax):

      MyType* rootObject = qobject_cast<MyType*>(engine.rootObjects().first());
      QObject::connect(qmltocpp, &QmltoCpp::showMessage, [rootObject](const QVariant & v) {
           QMetaObject::invokeMethod( rootObject, "showMessage", Qt::DirectConnection, Q_ARG(QVariant,v) );
      });
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved