The Output in a QGraphicsScene of a QGraphicsPixmapItem Maze, displays a rotate Maze. (SOLVED)



  • Hi, I'm having an issue, where I have to show a maze in a window.. Maybe some if you remember me.

    I finally figure it out all this things in QT, but now once I load the Maze from a .txt File, and then try to add the respective images of each int value (int 2D Array) to the QGraphicsScene, seems that something is wrong.
    PROBLEM:
    The maze that appear in the Window, is rotated (90 degrees plus inverted).

    The code have to looks like this:
    @/*
    Number One (1) = Wall
    Other Numbers (0,2,3,5,6,7,8,9) = Space
    1 1 0 1 1 1 1 1 1 1
    1 1 0 1 0 5 1 1 0 1
    1 1 6 1 7 1 1 1 0 1
    1 1 0 1 0 1 0 0 0 1
    1 0 9 0 0 1 0 1 0 1
    1 8 1 1 0 1 6 1 0 1
    1 0 1 1 0 1 0 1 5 1
    1 0 1 1 0 1 0 1 0 1
    1 0 0 1 0 0 0 1 7 1
    1 1 1 1 1 1 1 1 0 1
    */@

    Now the ones that shows up looks like this:
    "ROTATED MAZE":http://imgur.com/CeZnbwi

    You can compare that the .txt given, isn't the same as the image, and have to be the same. I'm showing you guys the codes, the function that count the size of the maze, the ones that load the values of the maze, and the one that show everything.

    Counting the size of the Maze.
    @void Window::tamMat(){
    fstream inFile;
    int num;
    n=m=0;
    inFile.open("Path_Here",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];
    

    }@
    Loading the values of the Maze (int)
    @void Window::loadM(){
    fstream inFile;
    inFile.open("PATH/mat.txt",ios::in);
    for(int i=0;i<this->n;i++)
    for(int j=0;j<this->m;j++)
    inFile >> mat[i][j];
    inFile.close();
    }
    @

    Show Window

    @void Window::load(){
    container = new QWidget();
    QPixmap icon2("PATH/Background.png");
    QGraphicsScene *scene = new QGraphicsScene();
    scene->setSceneRect(0.0,0.0,800.0,600.0);
    QGraphicsPixmapItem *image3 = new QGraphicsPixmapItem();
    image3->setPixmap(icon2);
    scene->addItem(image3);

    for(int i=0;i<this->n;i++){
        for(int j=0;j<this->m;j++){
            QPixmap *icon;
            if(this->mat[i][j]==1)
                icon = new QPixmap("PATH/Wall.png");
            else
                icon = new QPixmap("PATH/Space.png");
    
            QGraphicsPixmapItem *image = new QGraphicsPixmapItem(*icon);
            scene->addItem(image);
            image->setPos(i*icon->width()+200,j*icon->height()+100);
            icon->~QPixmap();
        }
    }
    QGraphicsView *view = new QGraphicsView();
    view->setScene(scene);
    view->show();
    

    }
    @
    Thanks for reading, I know I have efficiency problems, but I'm still learning QT.


  • Moderators

    You mixed up indices for rows and columns.
    When you read file you have i for rows and j for columns. When you place pixmaps you use i for columns and j for rows.
    Change
    @ image->setPos(iicon->width()+200,jicon->height()+100);@
    to
    @ image->setPos(jicon->width()+200,iicon->height()+100);@
    and it should work.

    That's why you shouldn't name your variables i,j,a,b,c... Use meaningful names like "row" and "column". A little more typing, a whole lot less debugging ;)



  • [quote author="Chris Kawa" date="1396261511"]You mixed up indices.....[/quote]

    Thanks! it works, maybe your right about the name of the variables, plus... I'm not a native English speaker, so my problem is about "width and height" words, I always confuse them haha and didn't figure out this could be my problem. Thanks anyway :)


  • Moderators

    Btw. Your code has greater problems than these indices.

    @
    QPixmap *icon;
    if(this->mat[i][j]==1)
    icon = new QPixmap("PATH/Wall.png");
    else
    icon = new QPixmap("PATH/Space.png");

    QGraphicsPixmapItem *image = new QGraphicsPixmapItem(*icon);
    ...
    icon->~QPixmap();
    @
    You're leaking memory like crazy ;) You should (almost) never call the destructor yourself! This doesn't deallocate memory. it just runs destructor code on it. Instead call delete. it will implicitly call the destructor and deallocate memory. Also since you only briefly use the pixmap there's no need for dynamic allocation at all.

    This will be simpler and won't leak:
    @
    QPixmap icon((this->mat[i][j]==1) ? "PATH/Wall.png" : "PATH/Space.png");
    auto image = new QGraphicsPixmapItem(icon);
    @



  • [quote author="Chris Kawa" date="1396278462"]Btw. Your code has greater problems...[/quote]

    Thanks but I can't do that, I mean using:
    QPixmap icon((this->mat[i][j]==1) ? "PATH/Wall.png" : "PATH/Space.png");

    Cause I have more images than that. I have cat, mouse, pizza, vine, bread, rum. So there are other images and not only two.
    But I'm gonna delete the destructor sentence, thanks for the help I'll appreciate it. Got a suggestion? I mean there's like 7 Images I have to use, should I'll use if/else if/else or use a Switch?


  • Moderators

    No need for branching (if, switch etc.). Processors don't like it ;) I would suggest something along these lines:

    @
    QStringList paths { "PATH/Wall.png", "PATH/Space.png", ... }

    ...

    QPixmap icon(paths[mat[i][j]]);
    @
    If you're not using C++11 yet (you should, it's great) then change first line to
    @
    QStringList paths;
    paths << "PATH/Wall.png" << "PATH/Space.png" << ... ;
    @



  • Perfect, sounds a good way, thanks my friend :) really appreciate it!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.