Results 1 to 16 of 16

Thread: How to load a map in QT GUI application using text file

  1. #1
    Join Date
    Mar 2011
    Posts
    35
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default How to load a map in QT GUI application using text file

    Hi everybody I wanted load a map in y GUI application
    for example::


    0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0
    1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1
    1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1

    A 0 denotes a draw nothing and 1 denotes a draw rectangle
    Now how to open a .txt file in Qt GUI application....and how to access the every 0 & 1 ..so that I can draw a rectangle whenever the 1 is encountered and draw nothing whenever 0 is encountered ...please help as I have posted this to load simple Map using text files

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: How to load a map in QT GUI application using text file

    What have you done so far to solve your problem? What exactly are you having problems with?
    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 2011
    Posts
    35
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to load a map in QT GUI application using text file

    Actually i have no clue how to read a text file in qt creator ,,,,,,and the C++ method of ifstream is not working???is there any method to read a text file in qt creator...
    and secondly I am going to use for loop to read each element in the text file and I have no Idea where to stop the loop
    Qt Code:
    1. for(int i=0;i<??;i++)
    2. {
    3. for(int j=0;j<??;j++)
    4. {
    5. if (map.tile[y][x] != 0)
    6. {
    7. drawImage(brickImage, x * TILE_SIZE, y * TILE_SIZE);
    8. }
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 15th March 2011 at 10:34. Reason: missing [code] tags

  4. #4
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to load a map in QT GUI application using text file

    Why isn't the std::ifstream approach working? Post your code and an explanation of the problem.

  5. #5
    Join Date
    Mar 2011
    Posts
    35
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to load a map in QT GUI application using text file

    I tried this on my GUI application ....Actually it should load the content of input.txt file in textedit when I clicked the button....but result does not come.ie the the text file is not loaded...and no errors are observed
    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3.  
    4. QFile file("input.txt");
    5. file.open(QIODevice::ReadOnly);
    6. QTextStream in(&file);
    7. QString line;
    8.  
    9. while (!in.atEnd())
    10. {
    11. line = in.readAll();
    12.  
    13.  
    14. ui->textEdit->setPlainText(line);
    15. }
    16. file.close();
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 15th March 2011 at 10:34. Reason: missing [code] tags

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: How to load a map in QT GUI application using text file

    Why don't you check that the file actually gets opened?
    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.


  7. #7
    Join Date
    Mar 2011
    Posts
    35
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to load a map in QT GUI application using text file

    I again tried to display my text file in ...but now in textbrowser...this time my program ran succcessfully and I and i browsed to my text file and it did displayed the content of my textfile but..ERROR:nly single line is diplayed....please suggest me ...to improve my code

    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3. QString filename = QFileDialog::getOpenFileName(this, tr("wew"),"",tr("allfiles(*.txt)"));
    4. QFile file(filename);
    5. if(file.open(QIODevice::ReadOnly | QIODevice::Text));
    6. {
    7. QTextStream in(&file);
    8. QString line;
    9.  
    10. while (!in.atEnd())
    11. {
    12. line = in.readLine();
    13.  
    14. ui->textBrowser->setText(line);
    15. //ui->textEdit->setPlainText(line);
    16. }
    17.  
    18. file.close();
    19. }
    20.  
    21. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 15th March 2011 at 11:40. Reason: missing [code] tags

  8. #8
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to load a map in QT GUI application using text file

    can you explain (in own words!) what happens in this snippet?

    Qt Code:
    1. while (!in.atEnd())
    2. {
    3. line = in.readLine();
    4. ui->textBrowser->setText(line);
    5. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Mar 2011
    Posts
    35
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to load a map in QT GUI application using text file

    Ok I got it correct!!!
    U don't know how to load a map using text file in qt creator
    Last edited by jerkymotion; 15th March 2011 at 11:45.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: How to load a map in QT GUI application using text file

    Quote Originally Posted by jerkymotion View Post
    can anybody please help me to load map in qt creator
    Sure. Launch QtCreator, from the "File" menu choose "Open file or project" and then browse until you see the file containing your map, select it and click on "Open". Your map should be opened in Qt Creator now.
    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.


  11. #11
    Join Date
    Mar 2011
    Posts
    35
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to load a map in QT GUI application using text file

    that's funny
    actually I was asking about how to load a map using text file in qt creator????

  12. #12
    Join Date
    Nov 2010
    Posts
    57
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to load a map in QT GUI application using text file

    wysota might explode soon

    jerkymotion, if you mean qt creator then he is right. If you mean Qt, then look at what felixB asked you and think about what you are doing there! It is the reason you are getting your result.

  13. #13
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to load a map in QT GUI application using text file

    why do you want to load such a map in Qt Creator?

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: How to load a map in QT GUI application using text file

    Quote Originally Posted by jerkymotion View Post
    that's funny
    actually I was asking about how to load a map using text file in qt creator????
    Is this your school project, a daily job project or a free-time project?
    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.


  15. #15
    Join Date
    Mar 2011
    Posts
    35
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to load a map in QT GUI application using text file

    Ok it's my school project
    I wanted load a map for navigation purpose....so that if i clicked in the map it gives the co-ordinate value of that position and I want to send those value to my serial port...that's it
    so I had an idea about loading a map via text file but I am finding it difficult in qt because I am newbie to Qt creator

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: How to load a map in QT GUI application using text file

    Quote Originally Posted by jerkymotion View Post
    Ok it's my school project
    We have a rule on this forum that we can't give full solutions to school projects. We can only help you go in the right direction.

    so I had an idea about loading a map via text file but I am finding it difficult in qt because I am newbie to Qt creator
    Your approach with QFile looked correct but you dropped it for some reason. Maybe try going back to it and expand it. Think about an algorithm for reading the map. If necessary, take a piece of paper and a pencil and "emulate" the process or reading a file.
    Last edited by wysota; 15th March 2011 at 15:38.
    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.


Similar Threads

  1. Replies: 3
    Last Post: 29th April 2010, 15:18
  2. Replies: 7
    Last Post: 7th January 2010, 10:13
  3. How to load a .h or .cpp file in designer?
    By srohit24 in forum Qt Programming
    Replies: 2
    Last Post: 18th February 2009, 13:33
  4. Replies: 1
    Last Post: 3rd September 2008, 14:16

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.