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();
Related
So I wrote a Qt quick application that takes user inputs and stores them in a json file. I now want to add a feature that lets me recall the data in my file and display it in a text field within my application. I can get the text in the C++ portion of my application, Im just not sure how to display it in my user interface. Here is the code to get the text from my json file.
void Jsonfile:: display(){
//1. Open the QFile and write it to a byteArray and close the file
QFile file;
file.setFileName("data.json");
if(!file.open(QIODevice::ReadOnly)){
qDebug() << "file couldn't be opened/found";
return;
}
QByteArray byteArray;
byteArray = file.readAll();
file.close();
//2. Format the content of the byteArray as QJsonDocument
//and check on parse Errors
QJsonParseError parseError;
QJsonDocument jsonDoc;
jsonDoc = QJsonDocument::fromJson(byteArray, &parseError);
if(parseError.error != QJsonParseError::NoError){
qWarning() << "Parse error at " << parseError.offset << ":" << parseError.errorString();
return;
}
QTextStream textStream(stdout);
textStream << jsonDoc.toJson(QJsonDocument::Indented);
}
I am trying to read data from Multiple files in Qt.
This how I am doing it:
void MainWindow::on_pushButton_3_clicked()
{
QString path = "C:/MyDevelopment/readfiles";
QDir dir(path);
QStringList filters;
filters << "*.txt";
foreach ( QString fileName, dir.entryList(filters, QDir::Files) )
{
QFile readFile(fileName);
if(!readFile.open(QIODevice::ReadOnly | QIODevice::Text ) )
{
qDebug("Failed to read file.....");
//return ;
}
QTextStream in(&fileName);
while (!in.atEnd())
{
QString line = in.readLine();
qDebug() << line;
}
}
it is always going in failed to open. what i am doing wrong here??
in mentioned directory all files are .txt files.
what should be the contents in the area of signal and slots of the push button in the Qt, so that after clicking the push button only the text file will open.
void MainWindow::on_pushButton_clicked()
{
....
}
You can use whatever you want in order to open file, like FILE, fstream, QFile ecc. You simply call a class method, but inside that function you can put everything.
You're using Qt, so you can check QFile class of QT.
It can be done like:
void MainWindow::on_pushButton_clicked()
{
QFile file( filename );
if( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
QMessageBox::warning(this, tr("Error opening!"), tr("Could not open the file"));
QTextStream stream( &file );
while( !stream.atEnd() )
{
QString lineText;
lineText = stream.readLine(); //Read a line of text
QStringList tokens= lineText.split(" ",QString::SkipEmptyParts); //Take tokens from the line
}
file.close();
}
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();
I have written a piece of code that should save doubles in an csv file. Here it is:
QString fileName = QFileDialog::getSaveFileName(this,tr("Save Logger Data"), "",tr("LoggerData(*.csv);;All Files (*)"));
if (fileName.isEmpty())
{
return;
}
else
{
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly))
{
QMessageBox::information(this, tr("Unable to open file"), file.errorString());
return;
}
QDataStream out(&file);
out << data1 << "/t" << data2 << "/n";
}
Here, data1 and data2 are doubles. When I open the savefile I only see weird characters (I asume they are hexadecimal values??). How can I change my code so it saves doubles instead of hex?
QDataStream is not the right class for this. For text output use QTextStream instead.