PDA

View Full Version : How to load a map in QT GUI application using text file



jerkymotion
14th March 2011, 14:14
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

wysota
14th March 2011, 14:21
What have you done so far to solve your problem? What exactly are you having problems with?

jerkymotion
14th March 2011, 15:12
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

for(int i=0;i<??;i++)
{
for(int j=0;j<??;j++)
{
if (map.tile[y][x] != 0)
{
drawImage(brickImage, x * TILE_SIZE, y * TILE_SIZE);
}
}
}

SixDegrees
14th March 2011, 15:19
Why isn't the std::ifstream approach working? Post your code and an explanation of the problem.

jerkymotion
15th March 2011, 08:08
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

void MainWindow::on_pushButton_clicked()
{

QFile file("input.txt");
file.open(QIODevice::ReadOnly);
QTextStream in(&file);
QString line;

while (!in.atEnd())
{
line = in.readAll();


ui->textEdit->setPlainText(line);
}
file.close();

}

wysota
15th March 2011, 10:35
Why don't you check that the file actually gets opened?

jerkymotion
15th March 2011, 11:25
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::only single line is diplayed....please suggest me ...to improve my code


void MainWindow::on_pushButton_clicked()
{
QString filename = QFileDialog::getOpenFileName(this, tr("wew"),"",tr("allfiles(*.txt)"));
QFile file(filename);
if(file.open(QIODevice::ReadOnly | QIODevice::Text));
{
QTextStream in(&file);
QString line;

while (!in.atEnd())
{
line = in.readLine();

ui->textBrowser->setText(line);
//ui->textEdit->setPlainText(line);
}

file.close();
}

}

FelixB
15th March 2011, 11:34
can you explain (in own words!) what happens in this snippet?


while (!in.atEnd())
{
line = in.readLine();
ui->textBrowser->setText(line);
}

jerkymotion
15th March 2011, 11:35
Ok I got it correct!!!
U don't know how to load a map using text file in qt creator

wysota
15th March 2011, 11:41
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.

jerkymotion
15th March 2011, 11:47
that's funny
actually I was asking about how to load a map using text file in qt creator????

pan
15th March 2011, 11:55
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.

FelixB
15th March 2011, 12:58
why do you want to load such a map in Qt Creator?

wysota
15th March 2011, 13:39
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?

jerkymotion
15th March 2011, 15:16
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

wysota
15th March 2011, 15:34
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.