PDA

View Full Version : Strange xml behaviour when opening the file.



cbarmpar
18th September 2008, 23:40
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:

#ifndef KENTRIKO_H
#define KENTRIKO_H
#include <QtGui>
#include <QApplication>
#include <QtCore>
#include "ui_kentriko.h"

class kentriko : public QMainWindow
{
Q_OBJECT

public:
kentriko(QWidget *parent = 0);
~kentriko();
public slots:
void anigma_arxiou();
void diabase();

private:
Ui::kentrikoClass ui;
QXmlStreamReader ena;
};

#endif // KENTRIKO_H


the implementation
kentriko::kentriko(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
connect(ui.anigma,SIGNAL(clicked()),this,SLOT(anig ma_arxiou()));
connect(ui.diabase,SIGNAL(clicked()),this,SLOT(dia base()));
}

kentriko::~kentriko()
{

}
void kentriko::anigma_arxiou(){
QString fileName =
QFileDialog::getOpenFileName(this, tr("anigma arxiou"),
QDir::currentPath(),tr("arxia tipou (*.xml)"));
if (fileName.isEmpty())
return;
QFile file(fileName);
ui.textBrowser->append(fileName);
file.open(QFile::ReadOnly | QFile::Text);
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:



<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.

jacek
19th September 2008, 00:48
...
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.

cbarmpar
19th September 2008, 01:00
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.

jacek
19th September 2008, 01:43
Create "file" on the heap or make it a member variable, so that it doesn't go out of scope.

cbarmpar
19th September 2008, 01:49
Many thanks for your help.

I have just found it as well.

what do you mean change it in the heap?

Regards.

jacek
28th September 2008, 21:44
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.