Hi,

Here's another example that I wrote quite recently when testing and
debugging QtScript + QtXml. As you can see the script bindings created by
QtScriptGenerator are not perfect there probably might be even more gotchas
to encounter along the way. I might also throw this to the Tundra
repository in the near future as well. Sorry for the missing indentation
(GMail seems to strip it away).

// Loads the plugin.xml from Tundra/bin twice, first by using both plain
QFile, and the second time using QFile and QXmlInputSource.
function QtXmlTest()
{
var file = new QFile("plugins.xml");
var errorMsg;
var errorLine;
var errorColumn;
var domDocFromFile = new QDomDocument();

// The following line should work to my knowledge, but for some
(non-apparent) reason it doens't:
// var fileOk =
domDocFromFile["setContent(QIODevice,bool,QString,int,int)"](file, true,
errorMsg, errorLine, errorColumn);
// I'm suspecting it has something to do with the fact that QDomDocument
doesn't inherit QObject (I have encountered some mysterious
// behavior with e.g. QTreeWidgetItem also), and the fact that the actual
C++ function implementation uses default argument values:
// bool setContent(QIODevice *dev, bool namespaceProcessing, QString *
errorMsg = 0, int * errorLine = 0, int * errorColumn = 0);
// My guess is that that passing C-type pointers (or references for that
matter) which are used for output is not simply
// supported/understood by QtScript(Generator).
// Probably QtScriptGenerator doesn't generate function "signatures" for
all cases (the function at hand would yield four different possible
signatures).
// Hence the function call must be done with none of the "optional"
arguments provided.
// So, the following works without problems (let's try "bool
setContent(QIODevice * dev)"):
var fileOk = domDocFromFile.setContent(file);
if (fileOk)
{
// Should work ok!
// Simply dump the whole file contents to stdout.
print("File contents:");
print(domDocFromFile.toString());
print("");
}

file.close(); // Remember to close the file handle.

// So, the setContent function seems to be working by omitting the default
arguments from the signatures
// (http://developer.qt.nokia.com/doc/qt-4.7/qdomdocument.html)
alltogether, and the following signatures/overloads should work:
// bool setContent(const QByteArray & data, bool namespaceProcessing)
// bool setContent(const QString & text, bool namespaceProcessing)
// bool setContent(QIODevice * dev, bool namespaceProcessing)
// bool setContent(QXmlInputSource * source, bool namespaceProcessing)
// bool setContent(const QString & text)
// bool setContent(const QByteArray & buffer)
// bool setContent(QIODevice * dev) - which we usesd previously
// bool setContent(QXmlInputSource * source, QXmlReader * reader)

var file2 = new QFile("plugins.xml");
var source = new QXmlInputSource(file2);
var domDocFromInputSource = new QDomDocument();

// Utilize the Qt XML/DOM API and print the names of the C++ plugins
defined int the XML file individually.
// Let's try "bool setContent(QXmlInputSource * source, bool
namespaceProcessing)".
var sourceOk = domDocFromInputSource.setContent(source, true);
if (sourceOk)
{
// Should work ok!
var pluginElems = domDocFromInputSource.elementsByTagName("plugin");
print(pluginElems.length() + " C++ plugins found. The plugins are:");
for(var i = 0; i < pluginElems.length(); ++i)
print("* " + pluginElems.item(i).toElement().attribute("path"));
}

file2.close();
}
}

-- 
http://groups.google.com/group/realxtend
http://www.realxtend.org

Reply via email to