Qt XQuery into a QStringList - qt

I'm trying to use QtXmlQuery to extract some data from XML. I'd like to put this into a QStringList. I try the following:
QByteArray in = "this is where my xml lives";
QBuffer received;
received.setData(in);
received.open(QIODevice::ReadOnly);
QXmlQuery query;
query.bindVariable("data", &received);
query.setQuery(NAMESPACE //contains definition of the t namespace
"doc($data)//t:service/t:serviceID/text()");
QBuffer outb;
outb.open(QIODevice::ReadWrite);
QXmlSerializer s(query, &outb);
query.evaluateTo(&s);
qDebug() << "buffer" << outb.data(); //This works perfectly!
QStringList * queryResult = new QStringList();
query.evaluateTo(queryResult);
qDebug() << "string list" << *queryResult; //This gives me no output!
As you can see, everything works fine when I send the output into a QBuffer via a QXmlSerializer... but I get nothing when I try to use a QStringList.

Try to use string() instead of text(), it should work.

Related

QMetaType::convert failed

I'm trying to use QMetaType::convert to convert a QJsonValue to another dynamic type. At first, I tested the following code with the dynamic type setting to QString, the conversion was failed.
QJsonValue value("test");
QString string;
if (!QMetaType::convert(&value, QMetaType::QJsonValue, &string, QMetaType::QString))
{
qDebug() << "failed";
}
Then, I found this static method to check whether the meta system has a registered conversion between two meta types.
qDebug() << QMetaType::hasRegisteredConverterFunction(QMetaType::QJsonValue, QMetaType::QString);
unfortunately, the result was false. Maybe QJsonValue is so complex that the conversion from QJsonValue to QString is not supported. Finally, I tried this, and the result was still false:
qDebug() << QMetaType::hasRegisteredConverterFunction(QMetaType::Int, QMetaType::Int);
It's odd, seems to be, Qt dose not implement the converter functions between basic meta types. And, users can't use QMetaType::registerConverter to register converter function between two basic meta types.
I still can't believe that Qt dosen't implement conversions between basic meta types, is there any initializtion or .pro setting I missed?
I guess you have the QMetaType system and the QVariant class to encapsulate Qt data types on the one hand, and QJsonValue to encapsulate a value in JSON on the other hand.
QMetaType::convert can deal with QVariant data only. What you can do is to extract the QVariant from your QJsonValue and then use the QMetaType system to convert your data you know being a QString.
// Your code
QJsonValue value("test");
QString string ;
if (!QMetaType::convert(&value, QMetaType::QJsonValue, &string, QMetaType::QString))
{
qDebug() << "failed";
}
// Extract the QVariant data
QVariant variant = value.toVariant() ;
// Two way to test conversion between QVariant and a type
qDebug() << "canConvert template:" << variant.canConvert<QString>() << endl
<< "canConvert parameter:" << variant.canConvert( QMetaType::QString ) ;
if( variant.canConvert<QString>() )
{
// Convert using QVariant methods
qDebug() << "QVariant toString:" << variant.toString() ;
// Convert Using QJsonValue methods
qDebug() << "QJsonValue toString:" << value.toString() ; // It's just a string representation of the data, not the actual data
}
outputs :
failed
canConvert template: true
canConvert parameter: true
QVariant toString: "test"
QJsonValue toString: "test"
PS: the QJsonValue::Type : String is different from the QVariant::Type : String (QMetaType::QString) so there is no relationship between them.

How to correctly manage a QProcess with one only argument

I'd like to monitorize how RAM memory I used for may Qt application; so, I thought about something to put inside the code.
I tried the following:
QProcess p;
p.start("ps -A");
p.waitForFinished();
QByteArray RamMem =p.readAllStandardOutput();
p.close();
quint16 pidcounter = 0;
QString pidString(RamMem);
QStringList RamMemSplit = pidString.split('\n');
quint16 RamMemSplitcounter = RamMemSplit.count();
while(pidcounter< RamMemSplitcounter)
{
if (RamMemSplit[pidcounter].contains(MyApp))
{
splitsplit = RamMemSplit[pidcounter].split(" ");
qDebug() << "Process:"<< splitsplit[10]<< "pid:"<< splitsplit[0];
}
pidcounter++;
}
In this way, I save name of running process associated to its pid;
Now, I'd like to apply another process ("pmax -x mypid), so I can obtain the RAM amount of my app:
How can I perform this? I read a QProcess requires a QStringList argument; in my case, I have only one parameter to use as argument and it's no so clear how to correctly set tyhe QProcess. The following is my idea, connected to the previous part:
QStringList listprova(splitsplit[0]);
QProcess pr;
pr.start("pmap -x", listprova);
pr.waitForFinished();
QByteArray pmapResult = pr.readAllStandardOutput();
pr.close();
QString pmapString(pmapResult);
QStringList pmapSplit = pmapString.split('\n');
quint8 pmapCounter = pmapSplit.count();
qDebug() << pmapSplit[pmapCounter]; // last line of "pmap -x [pid]" console command
You have 2 arguments, -x and the pid.
QStringList arguments;
arguments << "-x" << splitsplit[0];
QProcess pr;
pr.start("pmap", arguments);

Qt QVariant toList not working

I have a Qt (4.7) program that takes a QByteArray and should break it into a list of QVariants, after using a parser to transform it into a QVariant. The problems seem to arise when I try to use the toList() function. I have something similar to this:
QVariant var = //whatever the value passed in is...
std::cout << "Data = " << var.toString().toStdString() << std::endl;
QList<QVariant> varlist = var.toList();
std::cout << "List Size = " << varlist.size() << std::endl;
which would return this:
Data = variant1 variant2 variant3
Size = 0
where the size should clearly be 3. Does anyone have an idea what I may be doing wrong? thanks!
The documentation of toList() says:
Returns the variant as a QVariantList if the variant has userType() QMetaType::QVariantList or QMetaType::QStringList; otherwise returns an empty list.
My guess is, your variant's userType() is neither of those two.
You probably need to construct your variant differently, e.g.
QVariantList list;
list << variant1 << variant2 << variant3;
QVariant var = list;
So, I have no idea why, but when I put the command I specified above into a separate function, ie QList<QVariant> myClass::ToList(QVariant v){return v.toList();}, and then call varlist = myClass::ToList(v), it works. Still doesn't the original way, but this way it's fine. Guess I'll just chalk it up to one of the quirks of Qt...

How to parse dynamic response using QtSoap?

I have currently the response of type:
<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><StartBuisnessResponse xmlns=\"http://test.com/kerosene/mytest/\"><StartBuisnessResult><Commodity><_price>45</_price></Commodity><Commodity><_price>36</_price></Commodity></StartBuisnessResult></StartBuisnessResponse></soap:Body></soap:Envelope>
Here, the node is dynamic. In such a case, I am not able to find a way to parse the response SOAP XML using QtSoap.
This is the Code which works for fetching the first commodity:
QString str("<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><StartBuisnessResponse xmlns=\"http://cg.nic.in/kerosene/finotest/\"><StartBuisnessResult><Commodity><_price>45</_price></Commodity><Commodity><_price>36</_price></Commodity></StartBuisnessResult></StartBuisnessResponse></soap:Body></soap:Envelope>");
QByteArray *arr = new QByteArray();
arr->append(str);
QtSoapMessage *testMsg = new QtSoapMessage();
testMsg->setContent(*arr);
const QtSoapType &testCont = testMsg->returnValue();
const QtSoapType &price = testCont["Commodity"];
qDebug() << "The value of the _price here is " << price["_price"].value().toString();
But how do I traverse through the subsequent nodes in this case? Any idea?
If you follow the example shown on Qt Solutions QtSoap that they have for Google, you should be on your way with it.
http://doc.qt.digia.com/solutions/4/qtsoap/index.html
http://doc.qt.digia.com/solutions/4/qtsoap/google-example.html
An alternative if you don't want to try that is to use the QXmlStreamReader:
http://qt-project.org/doc/qt-4.8/qxmlstreamreader.html#details
Here is some quick code to get out the _price information from this:
// add "QT += xml" to your .pro
#include <QXmlStreamReader>
#include <QDebug>
QXmlStreamReader xml(str);
while (!xml.atEnd())
{
if (xml.readNextStartElement())
qDebug() << qPrintable(xml.name().toString());
if(xml.name().toString() == "_price")
{
qDebug() << "\t" << xml.readElementText().toInt();
}
}
You also have a number of other alternatives available, too. See Qt XML Processing.
Hope that helps.

Not able to Seralize QHash to DataStream

while leaning about QHash and serializing QHash to DataStream I got an error with the following code.
typedef QHash <quint32,QString> hashtype1;
typedef QHash <QLocale::Language,hashtype1> hashtype;
hashtype1 hash;
hash.insert(1, "Key1");
hash.insert(2, "Key2");
hashtype hash1;
hash1.insert(QLocale::English, hash);
hash1.insert(QLocale::French, hash);
QByteArray ba;
QByteArray ba1;
QDataStream ds(&ba, QIODevice::ReadWrite);
QDataStream ds1(&ba1, QIODevice::ReadWrite);
ds << hash;
ds1 << hash1;
qDebug() << ds.device()->readAll();
ds.device()->reset();
ds1.device()->reset();
hashtype1 hashcopy;
ds >> hashcopy;
hashtype hash1copy;
ds1 >> hash1copy;
The last statement is giving an error saying
/usr/include/qt4/QtCore/qdatastream.h:362: error: no match for ‘operator>>’ in ‘in >> k’
I am not able to correct this..
Am I doing somthing wrong??
How can I correct this?
The problem is that there are no QDataStream operators for QLocale::Language. When streaming out this works because it gets automatically converted to an integer type. It can't do this for streaming in. So you will either need to change your QHash to use a different template parameter for the key or write streaming operators for QLocale::Language (which should be trivial, you just need to cast it to/from int).

Resources