[Qt-interest] Connect objects from XML description

Eirik Ulvik eiriku at simsurgery.com
Mon Dec 14 10:30:26 CET 2009


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>

Here Thread1 and Thread2 are QThreads. I first instantiate these objects 
and then do a connection based on the xml tags. I thought that this 
should be easy as the SIGNAL and SLOT macros take const char * as 
arguments, but I have not been able to get this working.
 The code I use to read the xml is like this:

void MainWindow::on_actionOpen_triggered()
{
  QMap<QString, QObject*> obj_map;
  QFile f("Test.xml");
  f.open(QFile::ReadWrite);
  QString xml = f.readAll();
  QDomDocument doc;
  doc.setContent(xml);
  QDomNodeList objs = doc.elementsByTagName(QString("obj"));
  for(int i = 0; i < objs.size(); i++) {
    QDomElement e = objs.item(i).toElement();
    QString cl = e.attribute(QString("class"));
    QString id = e.attribute(QString("id"));
    if(cl == "Thread2") {
      Thread2 *t2 = new Thread2();
      obj_map[id] = t2;
    }
    else if(cl == "Thread1") {
      Thread1 *t1 = new Thread1();
      obj_map[id] = t1;
    }
  }

  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.

My question is if anyone has tried this before or if I am missing 
something. I have checked the char * and the seem to be correct.

Any suggestions would be welcome.

Regards,
Eirik



More information about the Qt-interest-old mailing list