Convert QDomElement to QDomDocument and vs - qt

I've QDomElement object and did the following to convert it to QDomDocument.
// element is the QDomElement object
QString str;
QTextStream stream(&str, QIODevice::WriteOnly);
element.save(stream, 2); // stored the content of QDomElement to stream
QDomDocument doc;
doc.setContent(str.toUtf8()); // converted the QString to QByteArray
But what about the convertion from QDomDocument to QDomElement? By the way, is there any constructive way to convert QDomElement to QDomDocument?

here is the conversion
QDomDocument doc;
doc.setContent( <YOUR XML GOES HERE> );
QDomElement root = doc.documentElement();

Related

convert QByteArray to QString is empty

I use QTcpSocket::readAll() got a QByteArray. However when I used QString::fromUtf8() to convert it to QString, I got a empty QString.
QByteArray ba;
QDataStream in(&ba,QIODevice::ReadWrite);
in << socket->readAll();
QByteArray request = ba;
qDebug() <<"ba:" << ba; // right message
Then:
QString request = QString::fromUtf8(ba); // request is empty
QString request = QString(ba) //also empty
Maybe your bytearray has different text encoding (cyrillic - win1251 or DOS - cp866). For converting bytearray with specific encoding into string use QTextCodec
QByteArray ba("abcd");
QTextCodec *codec = QTextCodec::codecForName("CP1251");
QString str = codec->toUnicode(ba);
Your message is not coded in utf8, maybe GB18030.
In your main function, you must set your codec.
QTextCodec *gb = QTextCodec::codecForName("gb18030");
QTextCodec::setCodecForLocale(gb);
Then you can process the message(I am using Qt4).
QByteArray ba = s->readAll();
QString request = QString::fromLocal8Bit(ba.data(),ba.size());
...
QJsonDocument doc(jobject);
ByteArray arr = doc.toJson();
//Just cast
QString result = static_cast<QString>(doc.toJson());

Transmit QMap by QMimeData

Is any ideas how i can transmit QMap<QString, QString> in Drag and Drop mode by using QMimeData?
Now i convert QMap into QString like this:
"key1:value1;key2:value2;...keyN:valueN" and assigned it to QMimeData::setText().
Then on dropEvent() i rebuild QMap from QString. Is this right way?
Convert QString to QMap
...
QStringList splittedParams = params.split(";");
QMap<QString, QString> *map = new QMap<QString, QString>();
foreach(QString param, splittedParams)
{
if(param.isEmpty()) continue;
QStringList str = param.split(":");
map->insert(str[0], str[1]);
}
...
That's going to fall apart if your strings contain the separators. For a more robust approach use something like
QByteArray ba;
QDataStream ds(&ba, QIODevice::WriteOnly);
map >> ds;
mimeData->setData(QStringLiteral("your/mime/type"), ba);

store exact cv::Mat image in sqlite3 database

is there any way to store exact cv::Mat format data in sqlite3 using Qt.. as i will be using the same cv::Mat format in future..
i tried converting image to unsigned char* and them storing it.. but this didn't worked for me.. any other technique ??
You can serialize cv::Mat to QByteArray (see kde/libkface):
QByteArray mat2ByteArray(const cv::Mat &image)
{
QByteArray byteArray;
QDataStream stream( &byteArray, QIODevice::WriteOnly );
stream << image.type();
stream << image.rows;
stream << image.cols;
const size_t data_size = image.cols * image.rows * image.elemSize();
QByteArray data = QByteArray::fromRawData( (const char*)image.ptr(), data_size );
stream << data;
return byteArray;
}
Then store to DB.
To convert from QByteArray after reading from DB:
cv::Mat byteArray2Mat(const QByteArray & byteArray)
{
QDataStream stream(byteArray);
int matType, rows, cols;
QByteArray data;
stream >> matType;
stream >> rows;
stream >> cols;
stream >> data;
cv::Mat mat( rows, cols, matType, (void*)data.data() );
return mat.clone();
}
It works for me.

Write QStream to file in zip with QuaZip

I want to write a QString in a textfile in a ziparchive with QuaZip. I use Qt Creator on WinXP. With my code the text-file in the archive is created but empty.
QDomDocument doc;
/* doc is filled with some XML-data */
zipfile = new QuaZip("test.zip");
zipfile->open(QuaZip::mdCreate);
QuaZipFile file(zipfile);
file.open(QIODevice::WriteOnly, QuaZipNewInfo("foo.xml"));
QTextStream ts ( &file );
ts << doc.toString();
file.close();
zipfile.close();
When I try with a QFile it works as expected:
QDomDocument doc;
/* doc is filled with some XML-data */
QFile file("test.xml");
file.open(QIODevice::WriteOnly);
QTextStream ts ( &file );
ts << doc.toString();
file.close();
I find the right content in test.xml, so the String is there, but somehow the QTextStream doesn't want to work with the QuaZipFile.
When I do it with a QDataStream instead of QTextStream there is an output, but not a correct one.
QDomDocument doc;
/* doc is filled with some XML-data */
zipfile = new QuaZip("test.zip");
zipfile->open(QuaZip::mdCreate);
QuaZipFile file(zipfile);
file.open(QIODevice::WriteOnly, QuaZipNewInfo("foo.xml"));
QDataStream ts ( &file );
ts << doc.toString();
file.close();
zipfile.close();
The foo.xml in the test.zip is filled with some data, but wrong formatted (between each character is an extra 'nul'-character).
How can I write the String in the textfile in the zip-archive?
Thanks,
Paul
You don't need QTextStream or QDataStream to write a QDomDocument to a ZIP file.
You can simply do the following:
QDomDocument doc;
/* doc is filled with some XML-data */
zipfile = new QuaZip("test.zip");
zipfile->open(QuaZip::mdCreate);
QuaZipFile file(zipfile);
file.open(QIODevice::WriteOnly, QuaZipNewInfo("foo.xml"));
// After .toString(), you should specify a text codec to use to encode the
// string data into the (binary) file. Here, I use UTF-8:
file.write(doc.toString().toUtf8());
file.close();
zipfile->close();
In the original first example you must flush the stream:
QDomDocument doc;
/* doc is filled with some XML-data */
zipfile = new QuaZip("test.zip");
zipfile->open(QuaZip::mdCreate);
QuaZipFile file(zipfile);
file.open(QIODevice::WriteOnly, QuaZipNewInfo("foo.xml"));
QTextStream ts ( &file );
ts << doc.toString();
ts.flush();
file.close();
zipfile.close();

Qt QList<QString> serialization for database

I have a QList list. I want to insert it on the database. I didn't find any serializer method after some googling. If there any method / idea to serialize the list data for database?
How about using QStringList instead of QList<QString> -
QStringList numberList_; // instead of QList<QString>, use this
QString myString1 = "Hello";
QString myString2 = "World";
numberList_ << myString1;
numberList_ << myString2;
QByteArray byteArray;
QBuffer buffer(&byteArray);
QDataStream out(&buffer);
out << numberList_;
Probably QList<QString> should also work in place of QStringList. If it doesn't, well, you can convert it pretty easily to QStringList.
QDataStream, QBuffer,
QByteArray and QStringList reference.
Here is another option that is a bit more succinct:
QString serialize(QStringList stringList)
{
QByteArray byteArray;
QDataStream out(&byteArray, QIODevice::WriteOnly);
out << stringList;
return QString(byteArray.toBase64());
}
QStringList deserialize(QString serializedStringList)
{
QStringList result;
QByteArray byteArray = QByteArray::fromBase64(serializedStringList.toUtf8());
QDataStream in(&byteArray, QIODevice::ReadOnly);
in >> result;
return result;
}

Resources