I am trying to learn how to use JSON and the Qt JSON classes. For example I wnat to create a simple QJsonDocument, save it to a file, load it into a different QJsonDocument and compare results.
I managed to create a QJsonDocument. However there is no simple command in the QJsonDocument interface to save it to a file. The same goes for loading the document from a file.
#include <QJsonObject>
#include <QJsonDocument>
#include <QVariant>
int main()
{
QVariantMap map;
map.insert("integer", 1);
map.insert("double", 2.34);
map.insert("bool", QVariant(true));
map.insert("string", "word");
QJsonObject object = QJsonObject::fromVariantMap(map);
QJsonDocument document;
document.setObject(object);
// ?? save document to file
// ?? load file to document
return 0;
}
This answer shows how to load the document by
reading to a QFile
converting QFile to a QString
converting the QString to a QByteArray
constructing the QJsonDocument from the QByteArray
Is there a more straightforward way to do this?
Personally, I think that code [that you linked to] looks a bit messy. Warning: head compiled code follows.
QJsonDocument loadJson(QString fileName) {
QFile jsonFile(fileName);
jsonFile.open(QFile::ReadOnly);
return QJsonDocument().fromJson(jsonFile.readAll());
}
void saveJson(QJsonDocument document, QString fileName) {
QFile jsonFile(fileName);
jsonFile.open(QFile::WriteOnly);
jsonFile.write(document.toJson());
}
This may not be perfect: it assumes QFile instead of QIODevice, but if you're dealing with only local files maybe it won't matter. You can then use these functions instead of repeating the Json load/save code everytime you need to load/save Json.
No need for converting to string and back. With QSettings and QVariant classes you can easily do that. Create QVariant object from QJsonDocument and save it with QSettings. Look at functions QJsonDocument::fromVariant and QJsonDocument::toVariant. Combine them with QSettings class and specifically void QSettings::setValue ( const QString & key, const QVariant & value ) method, that works well with QVariant and that's it.
Also QSettings class has this constructor QSettings::QSettings ( const QString & fileName, Format format, QObject * parent = 0 )
that would allow you to set path to the file - fileName variable
Related
I'm a complete beginner to QT5, I searched YouTube for any QSettings tutorials and only found 2 of them, both in Spanish.
I'm trying to store simple text from a textEdit and then load it on save/load button click. So far I have not been able to accomplish this. Here's my code so far, no errors, it just doesn't work.
Widget.cpp
void Widget::saveText(QString key, QString text)
{
QSettings settings("App", "BillReminder");
settings.beginGroup("Text");
settings.setValue(key + "t", text);
settings.endGroup();
}
QString Widget::loadText(QString key)
{
QSettings settings("App", "BillReminder");
settings.beginGroup("Text");
settings.value(key + "t", text).toString();
settings.endGroup();
return QString(text);
}
void Widget::on_saveButton_clicked()
{
saveText("textEdit", text);
}
void Widget::on_loadButton_clicked()
{
QString text1 = loadText(text);
ui->textEdit->setText(text1);
}
widget.h - class Widget : public QWidget
private:
Ui::Widget *ui;
QString text;
void saveText(QString key, QString text);
QString loadText(QString key);
void SetText(QString key);
The problem is in your loadText() method. QSettings::value() is a function that returns a value retrieved from the QSettings storage. The second parameter is only a default value, that would be returned in case your settings storage doesn't contain the requested key.
QString Widget::loadText(QString key)
{
QSettings settings("App", "BillReminder");
settings.beginGroup("Text");
QString theValue = settings.value(key + "t", text).toString();
settings.endGroup();
return theValue;
}
This code example contains many issues.
don't shadow variable names (e.g. "text" is method argument and member variable); use e.g. underscore to indicate member variables
above answer about reading values as return value solve one issue as well
on_saveButton is using as a settings key a "textEdit" string but on_loadButton is used wrong "text" member variable string as key -> you want use the same string to load stored variable i.e. you are reading something else just now.
you are saving member variable "text" that is not initialised in your example i.e. it may be empty; and later you are setting UI text edit with stored settings (empty string in your example?)
Please go through QSettings Qt documentation for working example.
I have a QMap object and I would like to convert it to JSON. I am confused how I would accomplish this.
I read QT documentation saying that I can use QDataStream to convert QMap to JSON, but QDataStream seems to convert files: http://doc.qt.io/qt-4.8/datastreamformat.html
// c++
QMap<QString, int> myMap;
It would be easiest to convert the map to QVariantMap which can automatically be converted to a JSON document:
QMap<QString, int> myMap;
QVariantMap vmap;
QMapIterator<QString, int> i(myMap);
while (i.hasNext()) {
i.next();
vmap.insert(i.key(), i.value());
}
QJsonDocument json = QJsonDocument::fromVariant(vmap);
The same thing can be used to create a QJsonObject if you want, via the QJsonObject::fromVariant() static method. Although for QJsonObject you can skip the conversion to variant map step and simply populate the object manually as you iterate the map:
QMap<QString, int> myMap;
QJsonObject json;
QMapIterator<QString, int> i(myMap);
while (i.hasNext()) {
i.next();
json.insert(i.key(), i.value());
}
If you are using Qt 5.5 or higher you could use QJsonDocument::fromVariant, your map could be converted easily to a QVariantMap. If not, try QJson
For your purpose, you are looking for QMAP serialization, see this link: Serialization Qt. Try to set up the constructor with a QByteArray, something like this:
QByteArray serializeMap(const QMap<QString, int>& map) {
QByteArray buffer;
QDataStream stream(&buffer, QIODevice::WriteOnly);
out << map;
return out;
}
That's, will serialize your map in a QByteArray wich could be easily converted to a QString or std::string.
What is the best way to store multiple QFile in a collection? Would be best to use QList, array, or vectors?
I want to access the element of the collection as a QFile and not QFile*.
I'm trying to read a directory in Qt and store all the files in a collection. Then print all the file names to the screen.
How I'm reading the directory:
QDirIterator it(p_dir, QStringList() << "*.wav", QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext())
{
// Put file into collection
}
Currently I'm using QList to store the QFiles:
QList<QFile*> list;
I read that the best way to get file information is to use QFileInfo type. How could I set the QFileInfo file to be the element from my list?
I want to do something like:
QFileInfo info;
QListIterator<QFile*> i(list);
while(i.hasNext())
{
info.setFile(i);
}
But the above gives me:
error: no matching function for call to 'QFileInfo::setFile(QListIterator&)'
fi.setFile(i);
Keep in mind QFile inherits QObject, and as such is not copyable or movable. It is common practice to store pointers to such objects in containers rather than the objects themselves. As for which container to use, it doesn't really matter much, but QVector will be the most efficient.
QListIterator is a Java style iterator. So:
QListIterator::QListIterator(const QList<T> & list)
Constructs an iterator for traversing list. The iterator is set to be
at the front of the list (before the first item).
The iterator doesn't point to anything, if you want to get the actual object, use i.next() to give you a QFile* which you then will have to dereference, since setFile() takes in a QFile &, not a QFile *.
QFile is not copyable. In C++98, there's generally no way of storing it in a container, unless the container supports in-place construction. I don't know offhand of any such C++98 containers, although writing one wouldn't be all that hard.
In C++11, you can use any container that doesn't need to copy elements and supports emplacement, e.g. std::list:
// https://github.com/KubaO/stackoverflown/tree/master/questions/qfile-list-36391586
#include <QtCore>
#include <list>
void populate(std::list<QFile> & files) {
QDir dir(QCoreApplication::applicationDirPath());
for (auto fileName : dir.entryList(QDir::Files)) {
qDebug() << "adding file" << fileName;
files.emplace_back(fileName);
}
}
void iterate(std::list<QFile> & files) {
for (auto & file : files)
if (file.open(QIODevice::ReadOnly)) {
qDebug() << "successfully opened" << file.fileName();
}
}
int main(int argc, char ** argv) {
QCoreApplication app(argc, argv);
std::list<QFile> files;
populate(files);
iterate(files);
}
On my system, when run from Qt Creator, the output is:
adding file "main.o"
adding file "Makefile"
adding file "qfile-list-36391586"
successfully opened "main.o"
successfully opened "Makefile"
successfully opened "qfile-list-36391586"
Since QFile is a proper class implementing RAII of system resources, the files will get automatically closed by files::~list, before main returns.
I'm currently manipulating QFileInfo based on certain criterian/logics..but if I just filter all the complicated logic, it would be something like
QDir testDir("/home/mypath/to/dir");
QFileInfoList files = testDir.entryInfoList(); // #1. here you may use filters as I'm filtering based on a specific file extension
QFileInfoList myfiles;
for(int index = 0; index < files.size(); ++index)
{
const QFileInfo& info = files.at(index);
qDebug() << info.absoluteFilePath() << endl;
// #2. I've some sort of filters I used in #1 above that I compares here by utilizing info here, something like
//if (info.baseName().contains("anyPattern");
myfiles.push_back(info);
}
// Now you can view whatever you stored
for (int vw_idx = 0; vw_idx < myfiles.size(); ++vw_idx)
{
const QFileInfo& info = files.at(index);
qDebug() << info.absoluteFilePath() << endl;
}
I use QFileDialog to open a file and use a QDatastream to read it.
QString fileName = QFileDialog::getOpenFileName(this, tr("open file"),
" ",
tr("Text (*.c);;Bin(*.bin)"));
QFile f(fileName);
QDataStream readstream(&f);
f.open(QIODevice::ReadOnly);
But after that ,I have to use a function written in C so I have a problem about how to get the parameters.
My C function is:
Ymodem_Transmit (uint8_t *buf, const uint8_t* sendFileName, uint32_t sizeFile)
1.uint8_t *buf is a pointer to the start address of the file ,but I dont know how to get it from QDataStream. Maybe I can read them into a buffer but my file is a little big so I don't want to use a big buffer.
2.const uint8_t* sendFileName is a string and how to get that?
Even thoughQFileDialog::getOpenFileName return me a Qstring filename but I think it includes the file's path,not exactly the filename.What I want is a real filename , I believe some class-function can do this but I can't find it.
3.uint32_t sizeFile Can I get it by using qint64 QFile::size() ? If I was right , how can I transfer a qint64 into uint32_t? Will that work if I do so:
qint64 filesize=QFile::size();
(uint32_t)filesize;
I highly recommend you not to use your C-function - QFile itself provides a really nice interface for accessing data in your file system. If you want to get your filename excluding a path use QFIleInfo class and fileName()member. And yes, you can get the file size this way, but be carefull - if your file size is rather big (bigger than can hold uint32_t) your value will be overflown.
How Can I deserialize the output QVariant to std::string without using QT.
by reqs, My program could not include a Qt.
QVariant.toString().toStdString();
Example.
file.ini (write with QSetting)
..
ID="\x1\0\0\0\xd0\x8c\xd9\xec\xfb*"
profile_program /* Pseudo Code */
int main ()
{
void* IDQt =getIDFromIniFile("file.ini");
std::string myId = convertID(IDQt);
process(myID);
}
Look in the sources, probably src/corelib/kernel/qvariant.cpp for QDataStream& operator<<(QDataStream&, const QVariant&). That way you'll know what gets written during serialization.
Once you do, you'll see that the operator<< calls QVariant::save(QDataStream&). What's written is as follows:
quint32 QVariant::type()
quint8 QVariant::isNull()
if type()==UserType
QString MetaType::typeName(userType())
end
if the variant is not valid
QString empty
else
-- MetaType::save(...)
end
You need to drill down into QString and QMetaType to figure out how those are serialized.