Using QByteArray QIODevice::readAll() from QT5, I was able to make a bytes array from a txt file or an image, used decode after and recreated the file correctly. But, when I tried with a .db file (SQLITE) it didn't work.
I noticed that when you open a .db with a text editor, you will see "SQLite format 3" followed by encoded characters. After making a QByteArray from a .db file, followed by decode() to recreate the file, when I opened it with a text editor, the file only contains the text "SQLite format 3".
Does QByteArray only work with txt file or Image file?
If it does, how can I make a Array of bytes from a .db (SQLITE) file.
Thanks
Update1 (The code belows works):
QFile file("C:/database.db");
if(!file.open(QIODevice::ReadOnly))
qDebug()<<"You are stupid!";
QByteArray byteArray = file.readAll();
QFile file2("C:/database2.db");
file2.open(QIODevice::WriteOnly);
file2.write(byteArray);
file2.close();
file.close();
Update2:
About the decode I mentioned in my initial question, I was using the following:
QString QFile::decodeName(const QByteArray & localFileName)
which make no sense when you read carefully the documentation and was just wrong. :)
You should not open that file with QIODevice::Text flag.
Check this http://doc.qt.io/qt-5/qiodevice.html#OpenModeFlag-enum
Related
I want to read contents from a file, then do some processing, rewrite processed contents into that file.
But after I open file in QIODevice::Truncate mode, then the initial content is empty, but I need initial contents:
QFile update_a_file("some/path/to/file.txt");
update_a_file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate);
QString fileContents(update_a_file.readAll());
qInfo("trial info %s", fileContents.toStdString().c_str());
There is nothing from the output log.
So, how can I read contents, and rewrite(empty initial contents and write new processed contents) into file in one open call? Or are there some other file mode in qt?
You are erasing the contents of the file when you open it with QIODevice::Truncate.
From the docs:
If possible, the device is truncated before it is opened. All earlier contents of the device are lost.
Try taking these steps:
Open the file without Truncate.
Read the content
Close the file
Open the file again, this time with Truncate
Write your modified content.
Close the file.
I am using Qt on Windows and I want to turn this part here:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA..." />
into the native binary encoding of a .png file. I have already isolated the iVBORw0KGgoAAAANSUhEUgAAADIA... part as a QString, my only question is how to convert that into say, a QByteArray that can be written to a file. Specifically, a .png file. The QByteArray::fromBase64 method is what I tried, it doesn't work.
So my code is:
QDataStream stream(&file);
QByteArray qba;
qba.append(sourcestring);
stream << QByteArray::fromBase64(qba);
sourcestring is the source string (with the data:image/png;base64, part removed) and file is the png file I am writing to.
You might do this:
QString s("iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAl...");
QByteArray ba = QByteArray::fromBase64(s.toUtf8());
QImage img = QImage::fromData(ba);
img.save("test.png");
I need an editor of mine to evaluate the JS code in whatever JS file I have open. However, it never does. Although the app output says
QIODevice::read: device not open
Here's my code (mFilename is the variable that holds the open file's filename)
QFile sFile(mFilename);
QTextStream in(&sFile);
text = in.readAll();
sFile.close();
ui->webView->page()->mainFrame()->evaluateJavaScript(text);
You forgot to open the file. To open it you have to use QFile::open method.
If you want to read it just call the file.open like that:
sFile.open(QIODevice::ReadOnly);
Indeed you didn't open the file by calling the QFile constructor.
So, your code would now be:
QFile sFile(mFilename);
QTextStream in(&sFile);
sFile.open(QIODevice::ReadOnly);
text = in.readAll();
sFile.close();
ui->webView->page()->mainFrame()->evaluateJavaScript(text);
I have created a basic UI in Qt with lots of text fields, now i want to use the text or number that the user enters and store it in a file ( preferably a SQL db file, but a text file will do just fine).
after saving the file I should be able to access that info again such that it lays out everything in a tabular format.
See QTextStream class:
QFile data("output.txt");
if (data.open(QFile::WriteOnly | QFile::Truncate)) {
QTextStream out(&data);
out << yourLabel->text();
}
You can read your data in similar way.
I'm trying to compress a text file with QT:
QFile inFile("d:\\build\\Directories\\Debug\\files\\developer.txt");
bool open_file_result = inFile.open(QIODevice::ReadOnly);
QByteArray ba = inFile.readAll();
QFile file("d:\\build\\Directories\\Debug\\files\\developer.gz");
bool open_zip_result = file.open(QIODevice::WriteOnly);
QDataStream out(&file);
out << qCompress(ba);
file.close();
open_file_result and open_zip_result are both true. I can also see the zip file (it also has a size, such as 50KB) but I can't open it. I'm getting the following error:
The archive is either in unknown format or damaged.
What am I doing wrong? If you have a better/another way to compress a text file, please tell me!
There is a difference between the Zip compression algorithm and the Zip container. You're confusing the two.
You need to do some research into the Zip container format that will help you locate and extract a zipped file within a Zip container. Once you've gotten that file, you can apply qUncompress to that file only.
Try looking at the QuaZip, which is a library that has been written for this purpose.