Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using QMetaObject::connectSlotsByName both ways
Forum Updated to NodeBB v4.3 + New Features

Using QMetaObject::connectSlotsByName both ways

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 1.4k Views 1 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.
  • S Offline
    S Offline
    SQEW
    wrote on last edited by
    #1

    Hi,

    I have both a C++ class (derived from QObject) and a QQuickItem, which represents a QML file.
    Both have slots and signals which need to be interconnected.
    Doing this manually (using QObject::connect) works fine.

    I'd like to use QMetaObject::connectSlotsByName for this, for several reasons, most of all convenience.
    Both objects have a different objectName set.

    @void Backend::doStuff(QQuickItem* item)
    {
    item->setParent(this); // crucial
    QMetaObject::connectSlotsByName(this);
    }
    @

    This will connect all signals from the QQuickItem to the relevant slots in the Backend object.
    For example if the QML file has a signal called test(), it will automatically connect to Backend::on_Frontend_test() (given that the QQuickItem has the objectName FrontEnd)

    Thing is, it needs to happen the other way around too - I'd like to have every signal from Backend connected to the Frontend slots the same way.

    I've created a signal in the Backend class (let's call it whatever()) and implemented a function called
    on_Backend_whatever() in the qml file, but this function is not called when the signal is emitted.

    I'm guessing the parent>child relationship is the problem here and I'm not sure how to handle it.

    Just for laughs, I've tried

    @void Backend::doStuff(QQuickItem* item)
    {
    this->setParent(item);
    QMetaObject::connectSlotsByName(item);
    item->setParent(this); // crucial
    QMetaObject::connectSlotsByName(this);
    }
    @

    This compiles, but makes my application crash when the method is called.

    So how to do it?

    Regards

    1 Reply Last reply
    0

    • Login

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