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. Connect cpp signal to qml registered cpp class
Forum Updated to NodeBB v4.3 + New Features

Connect cpp signal to qml registered cpp class

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 3 Posters 247 Views
  • 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.
  • A Offline
    A Offline
    AnkushVerma
    wrote on last edited by
    #1

    I have to two classes one is dataGenerator(generates data) and other is dataDisplay "exposed to QML using qmlRegisterType<dataDisplay>("dataDisplay", 1, 0, "dataDisplay") "

    class dataGenerator{
    signals:
    void DataChunkGenerated(QVector<double> &dataChunk);
    }
    class dataDisplay
    {
    public slots:
    void updateData(const QVector<double> &dataChunk);
    }
    how to connect signal DataChunkGenerated(QVector<double> &dataChunk); from dataGenerator to slot in dataDisplay.updateData(const QVector<double> &dataChunk) which takes QVector<double> &dataChunk as parameter

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rangantha B V
      wrote on last edited by
      #2

      There are two possible solutions for connect this signal and slot,

      1.Change the signal definition to use const QVector<double> &, making it compatible with the slot.

      connect(&dataGenerator, &DataGenerator::DataChunkGenerated,
      &dataDisplay, &DataDisplay::updateData);

      2.If modifying the signal is not an option, use a lambda function to convert

      connect(&dataGenerator, &DataGenerator::DataChunkGenerated,
      &dataDisplay, [&](QVector<double> &dataChunk) {
      dataDisplay.updateData(dataChunk);
      });

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Shankarlinga M
        wrote on last edited by Shankarlinga M
        #3
        1. Modify dataGenerator Signal:
          Qt does not allow non-const references (&) in signals for queued connections. Modify the signal to use a const reference:

        class dataGenerator : public QObject {
        Q_OBJECT
        signals:
        void DataChunkGenerated(const QVector<double> &dataChunk); // Use const reference
        };
        2. Ensure dataDisplay Slot Matches the Signature:
        Your dataDisplay class already has:

        class dataDisplay : public QObject {
        Q_OBJECT
        public slots:
        void updateData(const QVector<double> &dataChunk);
        };
        This matches the modified signal.

        1. Connect the Signal to the Slot in C++:
          You can establish the connection in main.cpp or wherever you create these objects:

        dataGenerator *generator = new dataGenerator();
        dataDisplay *display = new dataDisplay();

        // Connecting the signal from dataGenerator to the slot in dataDisplay
        QObject::connect(generator, &dataGenerator::DataChunkGenerated,
        display, &dataDisplay::updateData);

        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