Results 1 to 16 of 16

Thread: Getting Data from XML File and placing them in lineEdits

  1. #1
    Join Date
    Aug 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Question Getting Data from XML File and placing them in lineEdits

    Ok, heres my code
    Qt Code:
    1. <Item_stats>
    2. <General_Details>
    3. <Price>300</Price>
    4. <Name>Bottle of Wisdom</Name>
    5. <Description> blahblahblah blahblah</Description>
    6. </General_Details>
    7.  
    8. <Auras>
    9. <HPreg>3.6</HPreg>
    10. <Manareg>3.6</Manareg>
    11. </Auras>
    12. </Item_stats>
    To copy to clipboard, switch view to plain text mode 

    that is my basic xml file, i tried using #include QXml and using QdomDocument ... didnt reach a thing.. although i managed to have the file in 'open' state, figured that out using if statement with a file.isOpen...

    Ok, what i want to do with the data is to place them into little Labels/Line edits. for example, i want this to be visible in the gui:

    Item Name: (text here)
    Price: 300...
    if you can give me an example on one i would manage the rest.

    Thanks in advance... XD

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Getting Data from XML File and placing them in lineEdits

    Quote Originally Posted by zellwwf View Post
    didnt reach a thing..
    Could you translate it to English, please? Or at least something more verbose, we've run out of crystal balls.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Getting Data from XML File and placing them in lineEdits

    QXmlSimpleReader or QXmlStreamReader would be good places to start.

  4. #4
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Getting Data from XML File and placing them in lineEdits

    Hi,

    Use QXmlStreamReader. Just some code snippets to get you going :

    Qt Code:
    1. QFile your_text_file = open the text file first
    2. QXmlStreamReader stream( your_text_file );
    3. pXml = &stream;
    4.  
    5. while( !pXML->atEnd() )
    6. {
    7. pXML->readNext();
    8.  
    9. if( pXML->isStartElement() )
    10. {
    11. if( pXML->name()=="Item_stats") // found start of my data
    12. {
    13. while( !pXML->atEnd() )
    14. {
    15. pXML->readNext();
    16.  
    17. if( pXML->isEndElement() )
    18. {
    19. if( pXML->name() == "Item_stats" )
    20. break; // end of my data
    21. }
    22. else if( pXML->isStartElement() ) // entry like 'Auras'
    23. {
    24. // do something with pXML->name()
    25. // and with pXML->readElementText()
    26. // and maybe pXML->attributes().value( "yourattribute")
    27. }
    28. }
    29. }
    30. }
    31. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Aug 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Getting Data from XML File and placing them in lineEdits

    Quote Originally Posted by marcvanriet View Post
    Hi,

    Use QXmlStreamReader. Just some code snippets to get you going :

    Qt Code:
    1. QFile your_text_file = open the text file first
    2. QXmlStreamReader stream( your_text_file );
    3. pXml = &stream;
    4.  
    5. while( !pXML->atEnd() )
    6. {
    7. pXML->readNext();
    8.  
    9. if( pXML->isStartElement() )
    10. {
    11. if( pXML->name()=="Item_stats") // found start of my data
    12. {
    13. while( !pXML->atEnd() )
    14. {
    15. pXML->readNext();
    16.  
    17. if( pXML->isEndElement() )
    18. {
    19. if( pXML->name() == "Item_stats" )
    20. break; // end of my data
    21. }
    22. else if( pXML->isStartElement() ) // entry like 'Auras'
    23. {
    24. // do something with pXML->name()
    25. // and with pXML->readElementText()
    26. // and maybe pXML->attributes().value( "yourattribute")
    27. }
    28. }
    29. }
    30. }
    31. }
    To copy to clipboard, switch view to plain text mode 
    Thank you... thank you ... i will post my results with this tomorrow morning.. i am extremely sleepy, i have been up all day working on this project (not only this part)... With extreme thanks

    upon reading the quote, why do we need a text file? and are we just placing the data from the xml to the text file then reading it?
    thanks

    upon reading the quote, why do we need a text file? and are we just placing the data from the xml to the text file then reading it?
    thanks

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Getting Data from XML File and placing them in lineEdits

    If you have the XML data in a file (free standing or in a resource) then that file is the text file. If you already have the XML data in-memory, i.e. in a QByteArray, then you can use QBuffer in place of QFile as the QIODevice for the stream reader.

  7. #7
    Join Date
    Aug 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Getting Data from XML File and placing them in lineEdits

    thank you again... if i run into trouble again i will inform you

  8. #8
    Join Date
    Aug 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Getting Data from XML File and placing them in lineEdits

    I have a little error that i ran into, pXml, what kind of variable is it
    i get this error "pXml was not declared in this scope"
    The Code is in a header just because i don't like messing around with my main cpp, i try to always to put stuff into headers... i know the code isn't complete, but i wanted to test somethings first

    Qt Code:
    1. //
    2. //#INCLUDES
    3. #ifndef XMLSTUFF_H
    4. #define XMLSTUFF_H
    5. #include <qstring>
    6. #include <QXmlStreamReader>
    7. #include <Qfile>
    8. #include <QMessageBox> //for debugging purposes
    9. //END INCLUDES
    10.  
    11. static void readXml() {
    12. QFile myFile("/xml/champs/ashe_prototype.xml"); //this is the path in my resources
    13. myFile.open(QFile::ReadOnly);
    14. if (myFile.isOpen()) {
    15.  
    16. //MESSAGE BOXES
    17. mbx.setText(" It is Open");
    18. mbx.exec();
    19.  
    20. //STREAMERS XML
    21. QXmlStreamReader stream( &myFile );
    22.  
    23. pXml = &stream;
    24.  
    25. //WHILE LOOPS
    26. while (!pXml->atEnd()){
    27. pXml->readNext();
    28. mbx.setText=pXml.name();
    29. mbx.exec();
    30.  
    31. }
    32.  
    33.  
    34. } else {
    35. mbx.setText(" !It is Open");
    36. mbx.exec();
    37. }
    38. }
    39.  
    40.  
    41.  
    42. #endif // XMLSTUFF_H
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Aug 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Getting Data from XML File and placing them in lineEdits

    sorry to bother you again, but why am i getting pXML is not declared in this scope

  10. #10
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Getting Data from XML File and placing them in lineEdits

    Looks like it should be QXmlStreamReader *, but I have no clue what for (you can just use stream).
    Qt Code:
    1. QFile myFile("/xml/champs/ashe_prototype.xml"); //this is the path in my resources
    To copy to clipboard, switch view to plain text mode 
    If you are trying to access resource file, then it should be more like
    Qt Code:
    1. QFile myFile(":/xml/champs/ashe_prototype.xml"); // with :/ at the beggining
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Aug 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Getting Data from XML File and placing them in lineEdits

    Ok... sweet, i managed to read what i want from the xml.. now, the problem is assigning the data obtain to QObjects/Widgets
    Qt Code:
    1. #include "lolapp.h"
    2. #include "ui_lolapp.h"
    3. #include "xmlstuff.h"
    4. #include <QtXml>
    5.  
    6. //General Variables ***********************************************************************************
    7. static void readXml();
    8. bool inDetails,inSkills,inPassive,inQ,inW,inE,inR,inHealth,inMana,inEnergy,inPhys,inMag,inDef,inExtra;
    9. //******************************************************************************************************
    10.  
    11. LOLApp::LOLApp(QWidget *parent) :
    12. QMainWindow(parent),
    13. ui(new Ui::LOLApp)
    14. {
    15. ui->setupUi(this);
    16.  
    17. //initialize booleans for xml reading//
    18. inDetails=inSkills=inPassive=inQ=inW=inE=inR=inHealth=inMana=inEnergy=inPhys=inMag=inDef=inExtra=false;
    19. //***********************************
    20.  
    21. readXml();
    22. }
    23.  
    24. LOLApp::~LOLApp()
    25. {
    26. delete ui;
    27. }
    28. //FUNCTION DECLARATION
    29. static void readXml() {
    30. QFile myFile(":/xml/champs/ashe_prototype.xml");
    31. QXmlStreamReader stream( &myFile );
    32. myFile.open(QFile::ReadOnly);
    33.  
    34. if (myFile.isOpen()) {
    35. //MESSAGE BOXES**********************************************
    36. mbx.setText(" It is Open XD");
    37. mbx.setStandardButtons(QMessageBox::Abort | QMessageBox::Ok);
    38. mbx.exec();
    39. //WHILE LOOPS***********************************************
    40. while (!stream.atEnd()){
    41. stream.readNext();
    42. if (stream.isStartElement()) {
    43. if (stream.name()=="Details") {
    44. if (stream.name()=="Name"){
    45. //should add a code to add it to the costume label Champ_Name_Label.
    46. //now here, i am supposed to link the name "Akali" to Champ_Name_Label
    47. //which is located in the form (ui) of this cpp... eg; this is called lolapp.ui
    48. //i tried LOLApp-> LOLApp:: LOLApp. nothing seems to work, i tried it alot...
    49. //i just need to know how to put akali, string, into Champ_Name_Label. thanks
    50.  
    51. }
    52. }
    53. }
    54. }
    55.  
    56.  
    57. } else {
    58. mbx.setText(" It is NOT Open");
    59. mbx.exec();
    60. }
    61. }
    To copy to clipboard, switch view to plain text mode 


    Added after 4 minutes:


    Ok... sweet, i managed to read what i want from the xml.. now, the problem is assigning the data obtain to QObjects/Widgets
    Qt Code:
    1. #include "lolapp.h"
    2. #include "ui_lolapp.h"
    3. #include "xmlstuff.h"
    4. #include <QtXml>
    5.  
    6. //General Variables ***********************************************************************************
    7. static void readXml();
    8. bool inDetails,inSkills,inPassive,inQ,inW,inE,inR,inHealth,inMana,inEnergy,inPhys,inMag,inDef,inExtra;
    9. //******************************************************************************************************
    10.  
    11. LOLApp::LOLApp(QWidget *parent) :
    12. QMainWindow(parent),
    13. ui(new Ui::LOLApp)
    14. {
    15. ui->setupUi(this);
    16.  
    17. //initialize booleans for xml reading//
    18. inDetails=inSkills=inPassive=inQ=inW=inE=inR=inHealth=inMana=inEnergy=inPhys=inMag=inDef=inExtra=false;
    19. //***********************************
    20.  
    21. readXml();
    22. }
    23.  
    24. LOLApp::~LOLApp()
    25. {
    26. delete ui;
    27. }
    28. //FUNCTION DECLARATION
    29. static void readXml() {
    30. QFile myFile(":/xml/champs/ashe_prototype.xml");
    31. QXmlStreamReader stream( &myFile );
    32. myFile.open(QFile::ReadOnly);
    33.  
    34. if (myFile.isOpen()) {
    35. //MESSAGE BOXES**********************************************
    36. mbx.setText(" It is Open XD");
    37. mbx.setStandardButtons(QMessageBox::Abort | QMessageBox::Ok);
    38. mbx.exec();
    39. //WHILE LOOPS***********************************************
    40. while (!stream.atEnd()){
    41. stream.readNext();
    42. if (stream.isStartElement()) {
    43. if (stream.name()=="Details") {
    44. if (stream.name()=="Name"){
    45. //should add a code to add it to the costume label Champ_Name_Label.
    46. //now here, i am supposed to link the name "Akali" to Champ_Name_Label
    47. //which is located in the form (ui) of this cpp... eg; this is called lolapp.ui
    48. //i tried LOLApp-> LOLApp:: LOLApp. nothing seems to work, i tried it alot...
    49. //i just need to know how to put akali, string, into Champ_Name_Label. thanks
    50.  
    51. }
    52. }
    53. }
    54. }
    55.  
    56.  
    57. } else {
    58. mbx.setText(" It is NOT Open");
    59. mbx.exec();
    60. }
    61. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by zellwwf; 16th August 2011 at 20:06.

  12. #12
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Getting Data from XML File and placing them in lineEdits

    Make the "readXml" method a (non-static and not const) member of LOLApp class, then you will have access to ui form (via this->ui->something).

  13. #13
    Join Date
    Aug 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Getting Data from XML File and placing them in lineEdits

    i removed static, and i added the following
    Qt Code:
    1. this->ui->champ_name_label.setText("dkdkd");//resulted in "invalid use of this in non-member function
    2. // when using the folloing:
    3. ui->...// reulsted in ui was not declared in this scope
    To copy to clipboard, switch view to plain text mode 

    FULL CODE NOW:
    Qt Code:
    1. #include "lolapp.h"
    2. #include "ui_lolapp.h"
    3. #include "xmlstuff.h"
    4. #include <QtXml>
    5.  
    6. //General Variables ***********************************************************************************
    7. void readXml();
    8. bool inDetails,inSkills,inPassive,inQ,inW,inE,inR,inHealth,inMana,inEnergy,inPhys,inMag,inDef,inExtra;
    9. //******************************************************************************************************
    10.  
    11. LOLApp::LOLApp(QWidget *parent) :
    12. QMainWindow(parent),
    13. ui(new Ui::LOLApp)
    14. {
    15. ui->setupUi(this);
    16.  
    17. //initialize booleans for xml reading//
    18. inDetails=inSkills=inPassive=inQ=inW=inE=inR=inHealth=inMana=inEnergy=inPhys=inMag=inDef=inExtra=false;
    19. //***********************************
    20.  
    21. readXml();
    22. }
    23.  
    24. LOLApp::~LOLApp()
    25. {
    26. delete ui;
    27. }
    28. //FUNCTION DECLARATION
    29. void readXml() {
    30. QFile myFile(":/xml/champs/ashe_prototype.xml");
    31. QXmlStreamReader stream( &myFile );
    32. myFile.open(QFile::ReadOnly);
    33.  
    34. if (myFile.isOpen()) {
    35. //MESSAGE BOXES**********************************************
    36. mbx.setText(" It is Open");
    37. mbx.setStandardButtons(QMessageBox::Abort | QMessageBox::Ok);
    38. mbx.exec();
    39. //WHILE LOOPS***********************************************
    40. while (!stream.atEnd()){
    41. stream.readNext();
    42. if (stream.isStartElement()) {
    43. if (stream.name()=="Details") {
    44. if (stream.name()=="Name"){
    45. //should add a code to add it to the costume label Champ_Name_Label.
    46. ui->champ_name_label.setText("asf");
    47.  
    48. }
    49. }
    50. }
    51. }
    52.  
    53.  
    54. } else {
    55. mbx.setText(" !It is Open");
    56. mbx.exec();
    57. }
    58. }
    To copy to clipboard, switch view to plain text mode 

  14. #14
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Getting Data from XML File and placing them in lineEdits

    i removed static, and ...
    Make this method a non-static member of a class.

  15. #15
    Join Date
    Aug 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Getting Data from XML File and placing them in lineEdits

    i am an idiot, but how do i do that? should i create a class for it? can u provide an example?

  16. #16
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Getting Data from XML File and placing them in lineEdits

    should i create a class for it?
    You already have a class, what I mean is to add a method to it.
    can u provide an example?
    Sure, there is a nice set of examples by Bruce Eckel, read here.

Similar Threads

  1. Replies: 3
    Last Post: 8th June 2011, 06:36
  2. how can i create array of lineedits?
    By BalaQT in forum Qt Programming
    Replies: 1
    Last Post: 20th August 2009, 08:17
  3. problem with LineEdits and keyPressEvents
    By impeteperry in forum Qt Programming
    Replies: 2
    Last Post: 5th November 2007, 22:12
  4. how to read LineEdits by using a loop
    By raghvendramisra in forum Qt Programming
    Replies: 1
    Last Post: 29th August 2007, 07:08
  5. clearing lineEdits
    By Salazaar in forum Newbie
    Replies: 4
    Last Post: 4th June 2007, 21:02

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.