问题描述
嗯,我想要的是实现类似于SIGNAL和SLOT宏的函数,但是使用我自己的实现,所以目标是函数(或我自己的宏,如果我可以创建它)接收一个参数像String mycustomsignal和他返回一个const char *2mycustomsignal(double,int)所以我这样做使用QMap存储这个asociation QMap<信号名称,信号签名>,我填写另一个函数。 / p>
在这种情况下,我的QMap是 this-> signals
所以我搜索签名mycustomsignal键mycustomsignal和前面的代码,QObject :: connect识别的信号,我得到2mycustomsignal(something)所以我转换它在const char *因为QObject :: connect有这个参数的格式,我想使用也与SIGNAL和SLOT MACROS结合,如下:
QObject :: connect(customwidget,customwidget-> getSignal ),
somewidget,SLOT(someslot()));
我使用的函数是(仅对于我和我做什么):
const char * SomeClass :: getSignal(QString signalName){
QString signalsignature = this-> signals.value(signalName);
signalsignature.prepend(QString :: number(QSIGNAL_CODE));
QByteArray ba = signalsignature.toLatin1();
return signalformated; //这里是丢失的数据,因为QByteArray只存在于函数
}
这个返回一个指向本地的指针,当函数结束时,数据的源被销毁,那么我怎么能用一个函数或创建我自己的MACRO呢?
谢谢
您必须从您的方法中返回QByteArray, return ba;
,然后从返回值获取 const char *
:
code> QObject :: connect(customwidget,customwidget-> getSignal(somesignal)。constData(),
somewidget,SLOT(someslot()));
如果你真的想返回char指针,那么你必须保持QByteArray将它添加到同一对象的QList成员变量中,因此当实例被销毁时,它将被销毁。
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.
这篇关于如何实现宏SIGNAL和SLOT作为函数QT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!