Strange xml behaviour when opening the file.
Hi all,
I am experiencing a strange behaviour when i am trying to read an xml file.
I just want to print a message when the startofdocument is found.
the header file:
Code:
#ifndef KENTRIKO_H
#define KENTRIKO_H
#include <QtGui>
#include <QApplication>
#include <QtCore>
#include "ui_kentriko.h"
{
Q_OBJECT
public:
~kentriko();
public slots:
void anigma_arxiou();
void diabase();
private:
Ui::kentrikoClass ui;
QXmlStreamReader ena;
};
#endif // KENTRIKO_H
the implementation
Code:
kentriko
::kentriko(QWidget *parent
){
ui.setupUi(this);
connect(ui.anigma,SIGNAL(clicked()),this,SLOT(anigma_arxiou()));
connect(ui.diabase,SIGNAL(clicked()),this,SLOT(diabase()));
}
kentriko::~kentriko()
{
}
void kentriko::anigma_arxiou(){
QFileDialog::getOpenFileName(this, tr
("anigma arxiou"),
QDir::currentPath(),tr
("arxia tipou (*.xml)"));
if (fileName.isEmpty())
return;
ui.textBrowser->append(fileName);
ena.setDevice(&file);
}
void kentriko::diabase(){
ena.readNext();
if (ena.isStartDocument()){
ui.textBrowser->append("start of document found");
};
//ena.readNext();
}
:
and the xml that I am trying to read:
Code:
<adbook>
<contact email="kal@goteborg.se" phone="+46(0)31 123 4567" name="Kal" />
<contact email="ada@goteborg.se" phone="+46(0)31 765 1234" name="Ada" />
</adbook>
when the diabase function is called my program crashes.
Any idea what might causing this?
Many thanks in advance.
Re: Strange xml behaviour when opening the file.
Quote:
Originally Posted by
cbarmpar
...
QFile file(fileName);
...
ena.setDevice(&file);
}
You pass a pointer to an object created on a stack to setDevice(). When the function returns, "file" gets destroyed.
Re: Strange xml behaviour when opening the file.
many thanks for the reply
I don't understand what should i change!
The error occurs when i try to execute the readnext() command.
regards.
Re: Strange xml behaviour when opening the file.
Create "file" on the heap or make it a member variable, so that it doesn't go out of scope.
Re: Strange xml behaviour when opening the file.
Many thanks for your help.
I have just found it as well.
what do you mean change it in the heap?
Regards.
Re: Strange xml behaviour when opening the file.
Quote:
Originally Posted by
cbarmpar
what do you mean change it in the heap?
The heap is a data structure that is used to manage all dynamically allocated memory. Everything you create with new operator is created on the heap.