QObject :: connect: cannot set arguments like MyClass * const

I have a class like this:

#include <QObject>
namespace taservices
{
    class ProcessHandle : public QObject
    {
        Q_OBJECT
    public:
        ProcessHandle(const void* const processContextPointer, const QString& process_id = "", QObject *parent = 0);
        ProcessHandle();
    signals:
        void progress(const ProcessHandle* const self, const int value);
    private:
        static void registerAsMetaType();
}

I have a signal:

void progress(const ProcessHandle* const self, const int value);

I want to connect it through QueuedConnedtion. I keep getting this message:

QObject::connect: Cannot queue arguments of type 'ProcessHandle*const'
(Make sure 'ProcessHandle*const' is registered using qRegisterMetaType().)

I will register my class as follows after the declaration:

Q_DECLARE_METATYPE(taservices::ProcessHandle*);

I also added this static method, which I call from the constructor:

void ProcessHandle::registerAsMetaType()
{
    static bool called = false;
    if(!called) {
        called = true;
        qRegisterMetaType<ProcessHandle*>("taservices::ProcessHandle*");
    }
}

I also tried registering a pointer const:

qRegisterMetaType<ProcessHandle*const>("taservices::ProcessHandle*const");

This causes the following error:

error C2440: 'return' : cannot convert from 'taservices::ProcessHandle *const *' to 'void *'

So how do I get my class to work with connections in the queue?

+4
source share
2 answers

const. - , , - ( , , !). moc, , .

:

Q_SIGNAL void progress(const ProcessHandle* self, int value);

const. Qt - ​​ .

. , connect.

:

// https://github.com/KubaO/stackoverflown/tree/master/questions/const-slot-arg-42163294
#include <QtCore>

struct ProcessHandle {};

struct Object : QObject {
    int counter = 0;
    Q_SIGNAL void newValue(const ProcessHandle*, int val);
    Q_SLOT void useValue(const ProcessHandle* const ph, const int val) {
        qDebug() << ph << val;
        Q_ASSERT(ph == nullptr && val == 42);
        ++counter;
    }
    Q_OBJECT
};

int main(int argc, char ** argv) {
    QCoreApplication app{argc, argv};
    Object obj;
    QObject::connect(&obj, &Object::newValue, &obj, &Object::useValue, Qt::QueuedConnection);
    QObject::connect(&obj, &Object::newValue, &app, &QCoreApplication::quit, Qt::QueuedConnection);
    emit obj.newValue(nullptr, 42);
    app.exec();
    Q_ASSERT(obj.counter == 1);
}

#include "main.moc"
+3

, , :

qRegisterMetaType<ProcessHandle*>("ProcessHandle*");
qRegisterMetaType<ProcessHandle*>("ProcessHandle*const");

const , .

+3

Source: https://habr.com/ru/post/1669393/


All Articles