简体   繁体   中英

how to implement the macros SIGNAL and SLOT as functions QT

Well, what i want is to implement functions that works like the macros SIGNAL and SLOT but with my own implementation, so the goal is that the function (or my own macro if i can create it) receive a parameter like an String "mycustomsignal" and he return a const char* "2mycustomsignal(double,int)" so i do that using a QMap for store this asociation QMap<"signal name","signal signature">, i fill it in another function.

in this case my QMap is this->signals so i search the signature "mycustomsignal(something)" with the key "mycustomsignal" and prepend the code that QObject::connect recognize for signals and i get "2mycustomsignal(something)" so i convert it in const char* because QObject::connect have this parameters in this format and i want to use also in conjuntion with SIGNAL and SLOT MACROS like this:

QObject::connect(customwidget, customwidget->getSignal("somesignal"), 
                 somewidget, SLOT(someslot()));

the function that i use is (only for undertand what i do):

const char* SomeClass::getSignal(QString signalName) {
    QString signalsignature = this->signals.value(signalName); 
    signalsignature.prepend(QString::number(QSIGNAL_CODE)); 
    QByteArray ba = signalsignature.toLatin1();
    return signalformated; //Here is the lost of data because QByteArray only exist in the function
}

but this return a pointer to local and the source of the data is destroyed when the function ends, so how i could do this with a function or creating my own MACRO?

Thanks for any help or suggestion.

You have to return QByteArray from your method, return ba; , then get the const char* from the return value:

QObject::connect(customwidget, customwidget->getSignal("somesignal").constData(),
                 somewidget, SLOT(someslot()));

If you really want to return char pointer, then you have to keep the QByteArray around, for example by adding it to a QList member variable of the same object, so it will get destructed when the instance gets destructed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM