To make a long story short I have a program that uses the QDomDocument class to create an xml file and then uses the save() function to save it to a text stream object. So basically its
QDomDocument somedoc;
//create the xml file, elements, etc.
QFile io(fileName);
QTextStream out(&io);
doc.save(out,4);
io.close();
I want to be able to show the progress of the save using the QProgressDialog class, but I'm having a hard time figuring it out. Is there a way I can incrementally check to see if the file is through processing and just update the progress? Any suggestions? Thanks.
Firstly, I thought that we can find answer in Qt source code, but it was not so simple, so I found easier solution, just use toString() method and write it as usual file. For example:
QStringList all = doc.toString(4).split('\n');//4 is intent
int numFiles = all.size();
QProgressDialog *progress = new QProgressDialog("Copying files...", "Abort Copy", 0, numFiles, this);
progress->setWindowModality(Qt::WindowModal);
QFile file("path");
file.open(QIODevice::WriteOnly);
progress->show();
QTextStream stream(&file);
for (int i = 0; i < numFiles; i++) {
progress->setValue(i);
if (progress->wasCanceled())
break;
stream << all.at(i) << '\n';
QThread::sleep(1);//remove these lines in release, it is just example to show the process
QCoreApplication::processEvents();
}
progress->setValue(numFiles);
file.close();
If you want to look at the source code of QDomDocument::save(), you can find it in
qt-everywhere-opensource-src-5.4.1.zip\qt-everywhere-opensource-src-5.4.1\qtbase\src\xml\dom
or just on the GitHub.
Related
I am a newbie, I am creating a XML file in which I need to give CRC value to cross check on server. For this I need QDomDocument data as text. For this I am first creating XML file with fake CRC value. Then I open it with QFile and read all data. Then I split data and calculate data CRC. Now I have to rewrite whole file again. I know it is the worst idea ever to write same file twice but as I am a newbie, I don't know how to do it in better style. Here is my code:-
QDomElement docElem = doc.documentElement();
QFile xmlfile(filename);
if(!xmlfile.open(QIODevice::ReadWrite | QIODevice::Text))
{
qDebug("Can not open file device.");
}
xmlfile.resize(0);
QXmlStreamWriter xw;
xw.setDevice(&xmlfile); //set file to XML writer
xw.setAutoFormatting(true);
xw.setAutoFormattingIndent(4);
xw.writeStartDocument();
xw.writeStartElement(fileID); //fileID as the start element
if(docElem.hasAttributes())
{
xw.writeAttribute("xmlns:xs",docElem.attribute("xmlns:xs"));
xw.writeAttribute("xmlns",docElem.attribute("xmlns"));
}
xw.writeTextElement("Frame_Start_ID","STX");
xw.writeTextElement("Frame_Length","1234");
xw.writeTextElement("Source_Device_Id","CDIS_PIS ");
xw.writeTextElement("Destination_Device_Id","DDNS-SERVER ");
xw.writeTextElement("Frame_Code","I");
xw.writeStartElement("Frame_Data");
//inside frame data
xw.writeTextElement("File_Number","1");
xw.writeTextElement("File_Name","Event");
for(int j=0;j<logFields.count();j++)
{
xw.writeTextElement(logFields.at(j),logData.at(j));
}
xw.writeEndElement();
xw.writeTextElement("CRC","14405904");
xw.writeTextElement("Frame_End_Id","ETX");
xw.writeEndDocument();
xmlfile.flush();
xmlfile.close();
QFile xmlfyle(filename);
xmlfyle.open(QIODevice::ReadWrite | QIODevice::Text);
QString content = (QString)xmlfyle.readAll();
QStringList list1 = content.split("<CRC>");
qDebug() << "Split value = " << list1.at(0);
QByteArray crc_new = crc_o.crc_generate_modbus((unsigned char*)list1.at(0).data(),list1.at(0).size());
xmlfyle.resize(0);
QXmlStreamWriter xw_new;
xw_new.setDevice(&xmlfyle); //set file to XML writer
xw_new.setAutoFormatting(true);
xw_new.setAutoFormattingIndent(4);
xw_new.writeStartDocument();
xw_new.writeStartElement(fileID); //fileID as the start element
if(docElem.hasAttributes())
{
xw_new.writeAttribute("xmlns:xs",docElem.attribute("xmlns:xs"));
xw_new.writeAttribute("xmlns",docElem.attribute("xmlns"));
}
xw_new.writeTextElement("Frame_Start_ID","STX");
xw_new.writeTextElement("Frame_Length","1234");
xw_new.writeTextElement("Source_Device_Id","CDIS_PIS ");
xw_new.writeTextElement("Destination_Device_Id","DDNS-SERVER ");
xw_new.writeTextElement("Frame_Code","I");
xw_new.writeStartElement("Frame_Data");
xw_new.writeTextElement("File_Number","1");
xw_new.writeTextElement("File_Name","Event");
for(int j=0;j<logFields.count();j++)
{
xw_new.writeTextElement(logFields.at(j),logData.at(j));
}
xw_new.writeEndElement();
char tab[10];
sprintf(tab,"%d",crc_new.data());
xw_new.writeTextElement("CRC",QString::fromUtf8(tab));
xw_new.writeTextElement("Frame_End_Id","ETX");
xw_new.writeEndDocument();
xmlfyle.flush();
xmlfyle.close();
can anyone suggest me what could be a better way to do this.Thanks
One version of QXmlStreamWriter constructor accepts a QByteArray and writes into the array instead of an output file.
QXmlStreamWriter Class
So what you can do is; using QXmlStreamWriter, prepare data for your XML in a QByteArray, do whatever you need to do with the CRC inside this data; and when everything is done, write this QByteArray to the output file.
I have a QPainterPath that is being drawn to my QGraphicsScene and I am storing the points of the path as they are drawn into a QList.
My question is how do I now save those points out to an xml ( I assume this would work best ) as they are drawn? My goal is when the app closes, I read that xml, and the path is immediately re-drawn into the scene.
Here is the method I have setup for the writing, which I would call everytime I write a new point to the path.
void writePathToFile(QList pathPoints){
QXmlStreamWriter xml;
QString filename = "../XML/path.xml";
QFile file(filename);
if (!file.open(QFile::WriteOnly | QFile::Text))
qDebug() << "Error saving XML file.";
xml.setDevice(&file);
xml.setAutoFormatting(true);
xml.writeStartDocument();
xml.writeStartElement("path");
// --> no clue what to dump here: xml.writeAttribute("points", ? );
xml.writeEndElement();
xml.writeEndDocument();
}
Or maybe this isn't the best way to go about this?
I think I can handle the reading and re-drawing of the path, but this first part is tricking me up.
You may use binary file:
QPainterPath path;
// do sth
{
QFile file("file.dat");
file.open(QIODevice::WriteOnly);
QDataStream out(&file); // we will serialize the data into the file
out << path; // serialize a path, fortunately there is apriopriate functionality
}
Deserialization is similar:
QPainterPath path;
{
QFile file("file.dat");
file.open(QIODevice::ReadOnly);
QDataStream in(&file); // we will deserialize the data from the file
in >> path;
}
//do sth
I have tried to figure this out over the weekend, but to no avail. I cant seem to find an example using QFileSystemWatcher::Files() directly so i thought i would ask.
I have a program that :
lets the user select a 'source' folder.
press a button to start watching that source folder for new files
there is a signal emitted using 'directoryChanged()' where i try to update the count every time a file is added or removed.
I will profess that my implementation of QfileSystemWatcher is probably not correct. but this code is working and does trigger the signal/slot. but the count is always zero...
from mainwindow.cpp...
the signal:
//connect push buttons
QObject::connect(ui->startButton, SIGNAL(clicked()),
this, SLOT(startButtonClicked()));
//link qfilesystemwatcher with signals and slots
QObject::connect(&hotfolder, SIGNAL(directoryChanged(QString)), this, SLOT(hotfolderChanged()));
the slots:
void MainWindow::startButtonClicked(){
//start the file system watcher using the 'source folder button'
//first, get the resulting text from the source folder button
QString sourceText = ui->sourceBtnLineEdit->text();
ui->statusbar->showMessage(sourceText);
//convert the text from source button to a standard string.
string filePath = sourceText.toStdString();
cout << filePath << endl;
//call method to add source path to qfilesystemwatcher
startWatching(sourceText);
}
void MainWindow::hotfolderChanged(){
int fileCount = filesWatched();
ui->statusbar->showMessage(QString::number(fileCount));
}
from magickWatcher.h
#ifndef MAGICKWATCHER_H
#define MAGICKWATCHER_H
#include <QFileSystemWatcher>
#include <mainwindow.h>
//create the qFileSystemWatcher
QFileSystemWatcher hotfolder;
//add folder to qfilesystemwatcher
//starts watching of folder path
int startWatching( QString folder){
hotfolder.addPath(folder);
cout << "hotfolder created!" << endl;
return 0;
}
//get file list of folder being watched
int filesWatched(){
QStringList watchedList = hotfolder.files();
//report out each line of file list
for (int i = 0; i < watchedList.size(); ++i){
cout << watchedList.at(i).toStdString() << endl;
cout << "is this looping?!!" << endl;
}
return watchedList.count();
}
#endif // MAGICKWATCHER_H
How can i use QFileSystemWatcher to get the file count of the watched folder? I know about QDir and its options but want to specifically know how to use QFileSystemWatcher.
I am still wrapping my head around c++ in general so thank you for any advice or tips as well. I think maybe my problem is how i am implementing QFileSystemWatcher.
Some relevant links i have used:
QFileSystemWatcher working only in main()
http://doc.qt.io/qt-5/qfilesystemwatcher.html#files
First let's have a closer look at docs (bold format is mine):
QFileSystemWatcher examines each path added to it. Files that have been added to the QFileSystemWatcher can be accessed using the files() function, and directories using the directories() function.
So, files() only returns a list of files which you have already added to the watcher using addPath() method, NOT a list of files implicitly being watched by adding a directory.
You can get information about files in the watched directory e.g. by using QDir::entryInfoList with filters applicable in your case. At least QDir::Files and possibly QDir::NoDotAndDotDot would make sense.
//get file list of folder being watched
int filesWatched() {
QString folder = "/path/to/hotfolder/";
QDir monitoredFolder(folder);
QFileInfoList watchedList =
monitoredFolder.entryInfoList(QDir::NoDotAndDotDot | QDir::Files);
QListIterator<QFileInfo> iterator(watchedList);
while (iterator.hasNext())
{
QFileInfo file_info = iterator.next();
qDebug() << "File path:" << file_info.absoluteFilePath();
}
return watchedList.count();
}
The data of my file.txt is as below:
Student_ID=0001
Student_Name=joseph
Student_GradeLevel=2
How do I get the value, let say I want to get the Student_ID using Qt.
Thanks.
Take a look at this function, it can be used to find any value you want in your input file, where all lines are in the format you've posted above (key=value). If the key is not found, it returns an empty QString() object.
QString findValueInFile(QString key, QString filename) {
QFile file(filename);
if(file.open(QIODevice::ReadOnly)) {
QTextStream txtStr(&file);
QStringList fileContent = txtStr.readAll().split('\n');
for(auto &&line : fileContent) {
if(line.contains(key)) return line.split(QChar('='))[1];
}
file.close();
}
return QString(); // not found
}
Now you call it somewhere, e.g.:
qDebug() << findValueInFile("Student_ID", "file.txt");
qDebug() << findValueInFile("Student_Name", "file.txt");
This function can be easily modified if you replace your = sign with other delimiter e.g. => or sth else. However for key=value format there is a special QSettings class (mentioned by sebastian) that can allow you to read those values even easier:
QSettings file("file.txt", QSettings::IniFormat);
qDebug() << file.value("Student_Name").toString(); // et voila!
You can probably also use QSettings, as they are able to read ini files.
There are some caveats though regarding backslashes which might be important to you (though they aren't for the example you posted): http://doc.qt.io/qt-4.8/qsettings.html#Format-enum
QSettings iniFile("myfile.txt", QSettings::IniFormat);
// now get the values by their key
auto studentId = iniFile.value("Student_ID").toString().toInt();
I'm more of a PyQt user, so: apologies if I got some C++ specifics wrong...
Recently I was working on a gui application & I wanted to save the data of QTableView in a .csv or .txt file. I Used the guidance received during this question which made me think if the reverse is also possible; i.e. if the QTableView can be populated from a .csv or .txt file. Once again I would prefer staying with a model based design such as QTableView instead of item based QTableWidget.
Any code-snippet or tutorial-documentation would be really helpful.
Consider a test.csv file (it could be generated by any text editor):
And the textstream behind is (if generated by programming):
1,2,3,\n4,5,6,\n7,8,9,\n10,11,12,\n13,14,15,
If opened in Microsoft Office Excel, it might looked like:
To read this .csv file to the model of your QTableView:
QStandardItemModel *model = new QStandardItemModel;
QFile file("test.csv");
if (file.open(QIODevice::ReadOnly)) {
int lineindex = 0; // file line counter
QTextStream in(&file); // read to text stream
while (!in.atEnd()) {
// read one line from textstream(separated by "\n")
QString fileLine = in.readLine();
// parse the read line into separate pieces(tokens) with "," as the delimiter
QStringList lineToken = fileLine.split(",", QString::SkipEmptyParts);
// load parsed data to model accordingly
for (int j = 0; j < lineToken.size(); j++) {
QString value = lineToken.at(j);
QStandardItem *item = new QStandardItem(value);
model->setItem(lineindex, j, item);
}
lineindex++;
}
file.close();
}
(You could manipulate the code to meet you table format)
[Result]