[Qt-interest] Connect objects from XML description

Sean Harmer sean.harmer at maps-technology.com
Thu Dec 17 10:17:33 CET 2009


Hi,

On Thursday 17 December 2009 08:28:15 Eirik Ulvik wrote:
>  Stephen Jackson skrev:
> 2009/12/14 Eirik Ulvik :
> 
> Hi list,
> I would like to describe a connection between QObjects from an XML file.
> A sample file could look something like this:
> <connections>
>  <obj class="Thread1" id="obj1" />
>  <obj class="Thread2" id="obj2" />
>  <connect sender="obj1" signal="Thread1Msg(QString msg)" receiver="obj2"
> slot="receiveMsg(QString msg)" />
> </connections>
> 
> 
> 
> [snip]
> 
> 
>  QDomNodeList conns = doc.elementsByTagName("connect");
>  for(int i = 0; i < conns.size(); i++) {
>    QDomElement e     = conns.item(i).toElement();
>    QString sender       = e.attribute(QString("sender"));
>    QString signal         = e.attribute(QString("signal"));
>    QString receiver     = e.attribute(QString("receiver"));
>    QString slot           = e.attribute(QString("slot"));
> 
>   QByteArray tmp_sig = signal.toLatin1();
>    const char *sig = tmp_sig.data();
>    QByteArray tmp_slo = slot.toLatin1();
>    const char *slo = tmp_slo.data();
>    connect(obj_map[sender], SIGNAL(Thread1Msg(QString msg)),
> obj_map[receiver], SLOT(Thread1Msg(QString msg)));
>  }
> }
> 
> The error message printed from the connect method is: Object::connect:
> Parentheses expected, signal Thread1::sig in MainWindow.cpp:90.
> 
> 
> 
> http://doc.trolltech.com/4.5/qobject.html#connect
> "Note that the signal and slots parameters must not contain any
> variable names, only the type."
> 
> So remove all your msg variable names. I haven't checked whether there
> is anything else wrong, but you do need to correct this first.
> 
> 
>  Thanks for your reply. I actually found out that it is not possible to do
>  what I wanted. The SIGNAL/SLOT macros just basically makes a string of
>  whatever is in the braces. It does not evaluate a char* ar anything of
>  that sort. I managed a workaround by using the Meta object system to
>  invoke methods I want to invoke. That being said, I would have liked to
>  have support for creating signal/slot connections in the way I specified.

Well you could try just doing what the SIGNAL and SLOT macros do yourself 
since you need it to happen at runtime not at preprocessor time. Looking in 
$QTDIR/src/corelib/global/qobject_defs.h you will see the following:

#ifndef QT_NO_DEBUG
# define QLOCATION "\0"__FILE__":"QTOSTRING(__LINE__)
# define METHOD(a)   qFlagLocation("0"#a QLOCATION)
# define SLOT(a)     qFlagLocation("1"#a QLOCATION)
# define SIGNAL(a)   qFlagLocation("2"#a QLOCATION)
#else
# define METHOD(a)   "0"#a
# define SLOT(a)     "1"#a
# define SIGNAL(a)   "2"#a
#endif

which should be enough for you to construct your own const char* arguments to 
the connect call. It is very unlikely that these codes used by moc will change 
during the lifetime of Qt 4 so it should be safe for now and the foreseeable 
future.

Good luck,

Sean



More information about the Qt-interest-old mailing list