Search
 
SCRIPT & CODE EXAMPLE
 

CPP

QMetaObject_invokeMethod

#ifndef RECEIVER_H
#define RECEIVER_H

#include <QObject>
#include <QDebug>

struct SomeData {
    QString str;
    int id;
};
//Q_DECLARE_METATYPE(SomeData);

class Receiver : public QObject
{
    Q_OBJECT
public:
    explicit Receiver(QObject *parent = nullptr) : QObject(parent) {}

    void doSmth(SomeData data) {
        qDebug() << data.str;
    }

signals:

};

#endif // RECEIVER_H
Comment

QMetaObject invokeMethod

#ifndef RECEIVER_H
#define RECEIVER_H

#include <QObject>
#include <QDebug>

struct SomeData {
    QString str;
    int id;
};
//Q_DECLARE_METATYPE(SomeData);

class Receiver : public QObject
{
    Q_OBJECT
public:
    explicit Receiver(QObject *parent = nullptr) : QObject(parent) {}

    void doSmth(SomeData data) {
        qDebug() << data.str;
    }

signals:

};

#endif // RECEIVER_H
Comment

QMetaObject invokeMethod

#ifndef RECEIVER_H
#define RECEIVER_H

#include <QObject>
#include <QDebug>

struct SomeData {
    QString str;
    int id;
};
//Q_DECLARE_METATYPE(SomeData);

class Receiver : public QObject
{
    Q_OBJECT
public:
    explicit Receiver(QObject *parent = nullptr) : QObject(parent) {}

    void doSmth(SomeData data) {
        qDebug() << data.str;
    }

signals:

};

#endif // RECEIVER_H
Comment

QMetaObject_invokeMethod

#ifndef RECEIVER_H
#define RECEIVER_H

#include <QObject>
#include <QDebug>

struct SomeData {
    QString str;
    int id;
};
//Q_DECLARE_METATYPE(SomeData);

class Receiver : public QObject
{
    Q_OBJECT
public:
    explicit Receiver(QObject *parent = nullptr) : QObject(parent) {}

    void doSmth(SomeData data) {
        qDebug() << data.str;
    }

signals:

};

#endif // RECEIVER_H
Comment

QMetaObject_invokeMethod

#ifndef RECEIVER_H
#define RECEIVER_H

#include <QObject>
#include <QDebug>

struct SomeData {
    QString str;
    int id;
};
//Q_DECLARE_METATYPE(SomeData);

class Receiver : public QObject
{
    Q_OBJECT
public:
    explicit Receiver(QObject *parent = nullptr) : QObject(parent) {}

    void doSmth(SomeData data) {
        qDebug() << data.str;
    }

signals:

};

#endif // RECEIVER_H
Comment

QMetaObject invokeMethod

#ifndef RECEIVER_H
#define RECEIVER_H

#include <QObject>
#include <QDebug>

struct SomeData {
    QString str;
    int id;
};
//Q_DECLARE_METATYPE(SomeData);

class Receiver : public QObject
{
    Q_OBJECT
public:
    explicit Receiver(QObject *parent = nullptr) : QObject(parent) {}

    void doSmth(SomeData data) {
        qDebug() << data.str;
    }

signals:

};

#endif // RECEIVER_H
Comment

QMetaObject invokeMethod

#ifndef RECEIVER_H
#define RECEIVER_H

#include <QObject>
#include <QDebug>

struct SomeData {
    QString str;
    int id;
};
//Q_DECLARE_METATYPE(SomeData);

class Receiver : public QObject
{
    Q_OBJECT
public:
    explicit Receiver(QObject *parent = nullptr) : QObject(parent) {}

    void doSmth(SomeData data) {
        qDebug() << data.str;
    }

signals:

};

#endif // RECEIVER_H
Comment

QMetaObject_invokeMethod

#ifndef RECEIVER_H
#define RECEIVER_H

#include <QObject>
#include <QDebug>

struct SomeData {
    QString str;
    int id;
};
//Q_DECLARE_METATYPE(SomeData);

class Receiver : public QObject
{
    Q_OBJECT
public:
    explicit Receiver(QObject *parent = nullptr) : QObject(parent) {}

    void doSmth(SomeData data) {
        qDebug() << data.str;
    }

signals:

};

#endif // RECEIVER_H
Comment

QMetaObject_invokeMethod

#ifndef CALLER_H
#define CALLER_H

#include <QObject>
#include <QMap>
#include <utility>
#include <map>

#include "receiver.h"
#define CALL_MEMBER_FN(object,ptrToMember)  ((object)->*(ptrToMember))
typedef void (Receiver::*callback)(SomeData);

class Caller : public QObject
{
    Q_OBJECT
public:
    explicit Caller(QObject *parent = nullptr) : QObject(parent) { }

    void registerCallback(int id, Receiver* receiver, callback c)
    {
        auto pair = std::make_pair(receiver, c);
        _callbackMap.emplace(id, pair);
    }

    bool call(const SomeData data)
    {
        auto &receiver = _callbackMap.at(data.id);
        return QMetaObject::invokeMethod(receiver.first, [data, receiver] () {
            // method 1
            std::invoke(receiver.second, receiver.first, data);
            // method 2 (better not to use a MACRO)
            CALL_MEMBER_FN(receiver.first, receiver.second)(data);
        }, Qt::QueuedConnection);
    }

    bool call_invoke(const SomeData data)
    {
        auto &receiver = _callbackMap.at(data.id);
        std::invoke(receiver.second, receiver.first, data);
        return true;
    }

signals:

private:
    std::map<int,std::pair<Receiver*,callback>> _callbackMap;
};

#endif // CALLER_H
Comment

QMetaObject_invokeMethod

#ifndef CALLER_H
#define CALLER_H

#include <QObject>
#include <QMap>
#include <utility>
#include <map>

#include "receiver.h"
#define CALL_MEMBER_FN(object,ptrToMember)  ((object)->*(ptrToMember))
typedef void (Receiver::*callback)(SomeData);

class Caller : public QObject
{
    Q_OBJECT
public:
    explicit Caller(QObject *parent = nullptr) : QObject(parent) { }

    void registerCallback(int id, Receiver* receiver, callback c)
    {
        auto pair = std::make_pair(receiver, c);
        _callbackMap.emplace(id, pair);
    }

    bool call(const SomeData data)
    {
        auto &receiver = _callbackMap.at(data.id);
        return QMetaObject::invokeMethod(receiver.first, [data, receiver] () {
            // method 1
            std::invoke(receiver.second, receiver.first, data);
            // method 2 (better not to use a MACRO)
            CALL_MEMBER_FN(receiver.first, receiver.second)(data);
        }, Qt::QueuedConnection);
    }

    bool call_invoke(const SomeData data)
    {
        auto &receiver = _callbackMap.at(data.id);
        std::invoke(receiver.second, receiver.first, data);
        return true;
    }

signals:

private:
    std::map<int,std::pair<Receiver*,callback>> _callbackMap;
};

#endif // CALLER_H
Comment

QMetaObject invokeMethod

#ifndef RECEIVER_H
#define RECEIVER_H

#include <QObject>
#include <QDebug>

struct SomeData {
    QString str;
    int id;
};
//Q_DECLARE_METATYPE(SomeData);

class Receiver : public QObject
{
    Q_OBJECT
public:
    explicit Receiver(QObject *parent = nullptr) : QObject(parent) {}

    void doSmth(SomeData data) {
        qDebug() << data.str;
    }

signals:

};

#endif // RECEIVER_H
Comment

PREVIOUS NEXT
Code Example
Cpp :: _ZNSolsEi 
Cpp :: Lapindromes codechef solution in c++ 
Cpp :: check if an item is in a vector c++ 
Cpp :: c++ how to print out 
Cpp :: initializer before void c++ 
Cpp :: multiply two arbitrary integers a and b (a greater than b) 
Cpp :: qrandomgenerator bounded 
Cpp :: ex:c++ gcc start adress 
Cpp :: opencv read gif c++ 
Cpp :: Stack Modified 
Cpp :: c++ find string in string 
Cpp :: strip whitespace c++ 
Cpp :: concatenate 2 vectors in c++ 
Cpp :: template function in class c++ 
Cpp :: constructor overloading in c++ 
Cpp :: x += c++ 
Cpp :: hello world cpp 
Cpp :: make an x using asterisk c++ 
C :: pointer to a structure in c 
C :: .gitkeep file 
C :: como programar a area de um triangulo em c 
C :: how to print helloq world in c 
C :: yourkill071 
C :: successeur nombre chaine 
C :: scan numbers into array c 
C :: sdl2 c programming 
C :: c binary search 
C :: for loop in c 
C :: callback c++ c 
C :: memcpy c 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =