Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. The Output in a QGraphicsScene of a QGraphicsPixmapItem Maze, displays a rotate Maze. (SOLVED)

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

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vitaR
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ;)

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vitaR
        wrote on last edited by
        #3

        [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 :)

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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);
          @

          1 Reply Last reply
          0
          • V Offline
            V Offline
            vitaR
            wrote on last edited by
            #5

            [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?

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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" << ... ;
              @

              1 Reply Last reply
              0
              • V Offline
                V Offline
                vitaR
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved