How to convert a QJsonObject to QString - qt

I have a QJsonObject data and want to convert to QString. How can I do this? Searched for help in Qt, it only can convert QJsonObject to QVariantMap...
Thanks in advance.

Remembering when I first needed to do this, the documentation can be a bit lacking and assumes you have knowledge of other QJson classes.
To obtain a QString of a QJsonObject, you need to use the QJsonDocument class, like this: -
QJsonObject jsonObj; // assume this has been populated with Json data
QJsonDocument doc(jsonObj);
QString strJson(doc.toJson(QJsonDocument::Compact));

we can do this in one line
QString strFromObj = QJsonDocument(jsonObject).toJson(QJsonDocument::Compact).toStdString().c_str();

When the macro QT_NO_CAST_FROM_ASCII is enabled, you can do something like:
QJsonDocument doc(jsonObject);
QByteArray docByteArray = doc.toJson(QJsonDocument::Compact);
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
Qstring strJson = codec->toUnicode(docByteArray);
Or better, just use QLatin1String(QByteArray&), based on the example of TheDarkKnight:
QJsonDocument doc(jsonObj);
QByteArray docByteArray = doc.toJson(QJsonDocument::Compact);
Qstring strJson = QLatin1String(docByteArray);

Related

Convert TCHAR* to QString

How to convert easiest way in Qt?
int recordSize = 1000;
TCHAR* qRecord = new TCHAR[recordSize];
//here I get data form other function
//here I try to display
qString() << QString::fromWCharArray(qRecord,recordSize);//gives many ????
printf("%s",qRecord); // this work perfectly
I tried with wcstombs, formStdWString nad other but nothing seems to work.
Thanks for any help
QString s= (LPSTR)qRecord;
worked.
thanks
#kajojeq
no, your second answer is not correct. because when the encoding is set to utf16(or even utf8 sometimes) the s variable only save one character.
correct conversion is:
QString str = QString::fromWCharArray(qrecord)

Convert QVector<char*> list into a QStringList

How can I convert a QVector<char*> to a QStringList?
There is no direct way.
QVector<char*> vector;
QStringList list;
foreach (const char * str, vector) {
list << str;
}
I think you can try do it in this way (it works for me if I have a QVector<QString>):
QVector<char*> vector_char;
QStringList myStringList = vector_char.toList();
This code has to work as QStringList has inherited members of QList.
Here is somenthing in the documentation: QVector documentation.
And that is all.
I hope it can help you.

QDateTime to QString with milliseconds in Qt3

Is there a way in Qt3 to convert QDateTime into a QString and back to QDateTime, so that eventually QDateTime will contain information about milliseconds?
Thanks.
Use the toString function. The format parameter determines the format of the result string.
For example the following code will return only the seconds and the miliseconds.
QDateTime t = QDateTime::currentDateTime ();
QString s = t.toString("ss:zzz");
PS. You should consider porting your code to Qt4
QString DataList[100][100] ;
QDateTime AwbDateTime ;
AwbDateTime = QDateTime::fromString(DataList[i][9].left(19),Qt::ISODate) ;
This will work fine in Qt3

quint16 on qbytearray

i need add on firt position of qbytearray a quint16 and after read it: How can i to do it?
I have try this:
quint16 pos = 0;
QFile file(m_pathFile);
if (file.open(QFile::ReadOnly))
{
qDebug() << "el fichero existe";
m_udpSocket->bind(m_port);
QByteArray datagram;
while (!file.atEnd())
{
datagram.begin();
datagram.append(pos++);
datagram = file.read(m_blockSize);
qDebug() << "Sec" << datagram.at(0);
}
}
Thanks you very much
I got add with:
datagram.begin();
datagram.setNum(pos, 10);
datagram.append(file.read(m_blockSize));
but i don't know as read it
Thanks
Ok, first of all, that datagram.begin() is useless since it returns an iterator that you don't assign at all. If you want to insert a number in the first position of a QByteArray you can do something like:
datagram.insert(0, QString::number(pos++));
To read it, the simplest way is to use a QTextStream like this:
QTextStream str(datagram);
quint16 num;
str >> num;
Also, take a look at the docs before posting, because the Qt ones are really simple and helpful if you know how to search (and it's not that difficult, trust me).

QByteArray to integer

As you may have figured out from the title, I'm having problems converting a QByteArray to an integer.
QByteArray buffer = server->read(8192);
QByteArray q_size = buffer.mid(0, 2);
int size = q_size.toInt();
However, size is 0. The buffer doesn't receive any ASCII character and I believe the toInt() function won't work if it's not an ASCII character. The int size should be 37 (0x25), but - as I have said - it's 0.
The q_size is 0x2500 (or the other endianness order - 0x0025).
What's the problem here ? I'm pretty sure q_size holds the data I need.
Something like this should work, using a data stream to read from the buffer:
QDataStream ds(buffer);
short size; // Since the size you're trying to read appears to be 2 bytes
ds >> size;
// You can continue reading more data from the stream here
The toInt method parses a int if the QByteArray contains a string with digits. You want to interpret the raw bits as an integer. I don't think there is a method for that in QByteArray, so you'll have to construct the value yourself from the single bytes. Probably something like this will work:
int size = (static_cast<unsigned int>(q_size[0]) & 0xFF) << 8
+ (static_cast<unsigned int>(q_size[1]) & 0xFF);
(Or the other way around, depending on Endianness)
I haven't tried this myself to see if it works but it looks from the Qt docs like you want a QDataStream. This supports extracting all the basic C++ types and can be created wth a QByteArray as input.
bool ok;
q_size.toHex().toInt(&ok, 16);
works for me
I had great problems in converting serial data (QByteArray) to integer which was meant to be used as the value for a Progress Bar, but solved it in a very simple way:
QByteArray data = serial->readall();
QString data2 = tr(data); //converted the byte array to a string
ui->QProgressBar->setValue(data2.toUInt()); //converted the string to an unmarked integer..
This works for me:
QByteArray array2;
array2.reserve(4);
array2[0] = data[1];
array2[1] = data[2];
array2[2] = data[3];
array2[3] = data[4];
memcpy(&blockSize, array2, sizeof(int));
data is a qbytearray, from index = 1 to 4 are array integer.
Create a QDataStream that operates on your QByteArray. Documentation is here
Try toInt(bool *ok = Q_NULLPTR, int base = 10) const method of QByteArray Class.
QByteArray Documentatio: http://doc.qt.io/qt-5/QByteArray.html

Resources