PDA

View Full Version : Working with C++ code with QT Libraries, Read .txt File



vitaR
27th March 2014, 05:43
Hi, I'm having a problem with a project.

In this project I have to read some values from a .txt file, and then I have to deploy some images in a window.
I have to count every line (row) of this .txt file and colums, to show a maze with images. But Seems that my function to read this values is not working, so I guess it have to be some kind of compatibility between c++ code and QT code.

There's the code:

1) To read the size of the maze and create it.

void Window::tamMat(){
fstream inFile;
int num;
n=m=0; //attributes of the Window Class
inFile.open("mat.txt",ios::in);
string line;

getline(inFile,line);
stringstream temp(line);
while(temp >> num)
m++;
n++;
while(getline(inFile,line))
n++;
inFile.close();
mat = new int*[n];
for(int i=0;i<n;i++)
mat[i]=new int[m]; //mat is a bidimensional array, I'm creating the size with the values I count from the .txt file
this->show();
}

2)(TESTING ONLY) Try to Show in the frame 'n' numbers of images, with the value that got 'n' in the tamMat() function.

void Window::load(){
container = new QWidget();
QPixmap icon("E:/Data/Pictures/PixelArt/Limon.png");
QPixmap icon2("E:/Data/UNET/EstructuraDeDatos/Qt/NewOne/Image/Background.png");
QGraphicsScene *scene = new QGraphicsScene();
scene->setSceneRect(0.0,0.0,800.0,600.0);
for(int i=0;i<this->n;i++){
QGraphicsPixmapItem *image = new QGraphicsPixmapItem();
image->setPixmap(icon);
scene->addItem(image);
image->setPos(i*icon.width(),0);
}
QTransform transform;
QGraphicsPixmapItem *image2 = qgraphicsitem_cast<QGraphicsPixmapItem*>(scene->itemAt(180,0,transform));
scene->addItem(image2);
QGraphicsView *view = new QGraphicsView();
view->setScene(scene);
view->show();
}

The thing is that seems that the int value 'n', Still zero(0), cause there's no image in this frame. So the for loop, never happen (check load() Function).
I don't know if I can't work with C++ library while I'm working with the QT Ones. Could be this the problem?

Thanks for your time. By the way, I have to work with some functions in c++ code, like fstream library, work with data structures, like piles, some kind of that stuff, any advice you can give me to avoid future errors, would be great, thanks.

ChrisW67
28th March 2014, 02:53
Please edit your post to use
tags. It is hard to read.

The first function has precisely nothing to do with Qt. It's C++, Qt imposes no restrictions on your use of C++.

We cannot tell you if n is correct or not, but it would depend on what the getline() function is (it does not seem to be the fstream or stringstream member function).

The second function creates a graphics scene, containing n pixmap items, and a view (both likely to be memory leaks) . "container" is unused. "icon2" is unused. You ask for the item "image2" at (0,180) only to try to add it the the scene again.