Results 1 to 2 of 2

Thread: Working with C++ code with QT Libraries, Read .txt File

  1. #1
    Join Date
    Mar 2014
    Posts
    27
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Windows

    Default Working with C++ code with QT Libraries, Read .txt File

    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.
    Qt Code:
    1. void Window::tamMat(){
    2. fstream inFile;
    3. int num;
    4. n=m=0; //attributes of the Window Class
    5. inFile.open("mat.txt",ios::in);
    6. string line;
    7.  
    8. getline(inFile,line);
    9. stringstream temp(line);
    10. while(temp >> num)
    11. m++;
    12. n++;
    13. while(getline(inFile,line))
    14. n++;
    15. inFile.close();
    16. mat = new int*[n];
    17. for(int i=0;i<n;i++)
    18. mat[i]=new int[m]; //mat is a bidimensional array, I'm creating the size with the values I count from the .txt file
    19. this->show();
    20. }
    To copy to clipboard, switch view to plain text mode 

    2)(TESTING ONLY) Try to Show in the frame 'n' numbers of images, with the value that got 'n' in the tamMat() function.
    Qt Code:
    1. void Window::load(){
    2. container = new QWidget();
    3. QPixmap icon("E:/Data/Pictures/PixelArt/Limon.png");
    4. QPixmap icon2("E:/Data/UNET/EstructuraDeDatos/Qt/NewOne/Image/Background.png");
    5. scene->setSceneRect(0.0,0.0,800.0,600.0);
    6. for(int i=0;i<this->n;i++){
    7. image->setPixmap(icon);
    8. scene->addItem(image);
    9. image->setPos(i*icon.width(),0);
    10. }
    11. QTransform transform;
    12. QGraphicsPixmapItem *image2 = qgraphicsitem_cast<QGraphicsPixmapItem*>(scene->itemAt(180,0,transform));
    13. scene->addItem(image2);
    14. view->setScene(scene);
    15. view->show();
    16. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by anda_skoa; 30th March 2014 at 11:53. Reason: changed [quote] ot [code] tags

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,533
    Thanks
    13
    Thanked 1,576 Times in 1,505 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Working with C++ code with QT Libraries, Read .txt File

    Please edit your post to use [code] [/code] 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.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

Similar Threads

  1. Release libraries bigger than debug libraries
    By JPNaude in forum Qt Programming
    Replies: 5
    Last Post: 4th April 2013, 08:30
  2. Replies: 7
    Last Post: 24th September 2012, 07:17
  3. Replies: 4
    Last Post: 5th November 2011, 01:38
  4. QT (not-) working with VTK libraries
    By floyd.pepper in forum Newbie
    Replies: 16
    Last Post: 31st August 2009, 12:46
  5. Replies: 2
    Last Post: 8th March 2007, 22:22

Tags for this Thread

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.