Friday, 15 February 2013

c++ - Qt: connecting signals and slots from text -



c++ - Qt: connecting signals and slots from text -

how connect signal , slot of 2 objects when object names, signals, , slots specified in text file?

getting right objects names isn't issue, since can loop through array , compare names spot in file, there has sort of way can homecoming signal , slot file , utilize in connect function, like:

connect(rtnobj1(line),signal(rtnsignal(line)),rtnobj2(line),slot(rtnslot(line)));

where rtn functions homecoming object name/signal/slot, , "line" current qstring line file.

the way know of literally coding in every single combination , comparing qstrings if statements, incredibly tedious, amount of combinations incredibly high.

note: here's simplified illustration demonstrating essentially how issue exists.

frame 1: 4 qcomboboxes. first , 3rd hold object names, sec holds signals, 4th holds slots. every item of course of study qstring within these lists. hitting button appends new line file, writing text selected each box.

frame 2: has has required objects. reading file, match objects defined in list against ones created, , connect them file describes.

it's easy plenty create object based on info file holds, how 1 create/pull signal , slot file?

edit: unless, 1 able connect this?

connect(objecta, "", objectb, "");

because found out code compile that, whenever seek putting in slot or signal names error like:

qobject::connect: utilize signal macro bind tile::clicked

your problem solvable 1 of next static qobject::connect() method:

qmetaobject::connection qobject::connect( const qobject *sender, const qmetamethod &signal, const qobject *receiver, const qmetamethod &method, qt::connectiontype type = qt::autoconnection)

first of all, need pointers sender , receiver objects. there several ways how can store object pool. suggest maintain objects in qhash:

qhash<qstring, qobject *> m_objects; // utilize qobject generic illustration

now, it's possible find pointer object connecting in efficient way.

the next step obtaining qmetamethod objects sender's signal , receiver's slot corresponding qmetaobject objects. utilize qobject::metaobject() qmetaobject instances.

here's finish code of function connects 2 object using string parameters:

void dynamicconnect(const qstring &sendername, const qstring &signalname, const qstring &receivername, const qstring &slotname) { qobject *emitter = m_objects.value(sendername); int index = emitter->metaobject() ->indexofsignal(qmetaobject::normalizedsignature(qprintable(signalname))); if (index == -1) { qwarning("wrong signal name!"); return; } qmetamethod signal = emitter->metaobject()->method(index); qobject *receiver = m_objects.value(receivername); index = receiver->metaobject() ->indexofslot(qmetaobject::normalizedsignature(qprintable(slotname))); if (index == -1) { qwarning("wrong slot name!"); return; } qmetamethod slot = receiver->metaobject()->method(index); qobject::connect(emitter, signal, receiver, slot); }

c++ qt signals signals-slots slots

No comments:

Post a Comment