Results 1 to 6 of 6

Thread: Strange xml behaviour when opening the file.

  1. #1
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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:
    Qt Code:
    1. #ifndef KENTRIKO_H
    2. #define KENTRIKO_H
    3. #include <QtGui>
    4. #include <QApplication>
    5. #include <QtCore>
    6. #include "ui_kentriko.h"
    7.  
    8. class kentriko : public QMainWindow
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. kentriko(QWidget *parent = 0);
    14. ~kentriko();
    15. public slots:
    16. void anigma_arxiou();
    17. void diabase();
    18.  
    19. private:
    20. Ui::kentrikoClass ui;
    21. QXmlStreamReader ena;
    22. };
    23.  
    24. #endif // KENTRIKO_H
    To copy to clipboard, switch view to plain text mode 

    the implementation
    Qt Code:
    1. kentriko::kentriko(QWidget *parent)
    2. : QMainWindow(parent)
    3. {
    4. ui.setupUi(this);
    5. connect(ui.anigma,SIGNAL(clicked()),this,SLOT(anigma_arxiou()));
    6. connect(ui.diabase,SIGNAL(clicked()),this,SLOT(diabase()));
    7. }
    8.  
    9. kentriko::~kentriko()
    10. {
    11.  
    12. }
    13. void kentriko::anigma_arxiou(){
    14. QString fileName =
    15. QFileDialog::getOpenFileName(this, tr("anigma arxiou"),
    16. QDir::currentPath(),tr("arxia tipou (*.xml)"));
    17. if (fileName.isEmpty())
    18. return;
    19. QFile file(fileName);
    20. ui.textBrowser->append(fileName);
    21. file.open(QFile::ReadOnly | QFile::Text);
    22. ena.setDevice(&file);
    23. }
    24.  
    25. void kentriko::diabase(){
    26. ena.readNext();
    27. if (ena.isStartDocument()){
    28. ui.textBrowser->append("start of document found");
    29. };
    30. //ena.readNext();
    31.  
    32. }
    To copy to clipboard, switch view to plain text mode 
    :

    and the xml that I am trying to read:

    Qt Code:
    1. <adbook>
    2. <contact email="kal@goteborg.se" phone="+46(0)31 123 4567" name="Kal" />
    3. <contact email="ada@goteborg.se" phone="+46(0)31 765 1234" name="Ada" />
    4. </adbook>
    To copy to clipboard, switch view to plain text mode 

    when the diabase function is called my program crashes.

    Any idea what might causing this?

    Many thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Strange xml behaviour when opening the file.

    Quote Originally Posted by cbarmpar View Post
    ...
    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.

  3. #3
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

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

  5. #5
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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.
    Last edited by cbarmpar; 19th September 2008 at 00:56. Reason: aditional question

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Strange xml behaviour when opening the file.

    Quote Originally Posted by cbarmpar View Post
    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.

  7. The following user says thank you to jacek for this useful post:

    cbarmpar (28th September 2008)

Similar Threads

  1. Re: Opening Project file Issue in Edyuk
    By philwinder in forum Qt-based Software
    Replies: 6
    Last Post: 5th May 2008, 20:49
  2. Set up the Qt4.3.2 with Visual Studio 2005
    By lamoda in forum Installation and Deployment
    Replies: 6
    Last Post: 30th January 2008, 06:51
  3. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  4. Opening swf file in the default browser
    By munna in forum Qt Programming
    Replies: 16
    Last Post: 5th May 2006, 09:33
  5. [Qt 4.1] Strange behaviour with QTableView
    By fane in forum Qt Programming
    Replies: 1
    Last Post: 23rd January 2006, 06:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.