Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. 2D array into graphicview to create Objects from 2D-Array
QtWS25 Last Chance

2D array into graphicview to create Objects from 2D-Array

Scheduled Pinned Locked Moved Unsolved Game Development
algorithmsarraygraphics view
14 Posts 3 Posters 2.5k Views
  • 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.
  • projectbavariaP Offline
    projectbavariaP Offline
    projectbavaria
    wrote on last edited by projectbavaria
    #1

    This code should show the 2D-Array to Graphicview
    name of Graphicview in GUI is "view"
    The Problem is that i don't get the acknockledge to implement a array to a graphicview.
    Because i want to create a game of this 2D-Array and creating objects from them.
    But i can't even display it and i don't know how can i convert my letters (S,X,G => 2D_Array) to GUI with Graphicview ?

    const int xarrSize = 13;
        const int yarrSize = 60;
    
        int x = 0;
        int y = 0;
        char c;
        char grid[xarrSize][yarrSize];
        
        fstream input;
        input.open("C:/map.txt",fstream::in);
        if (!input) {
            cout << "not open";
        }
        while (input >> noskipws >> c) {
           if (c == '\n'){
                x++;
                y = 0;
                grid[x][y] = c;
    
        }
        else {
            grid[x][y] = c;
            y++;
        }
        }
        scene = new QGraphicsScene(this);
       
       
        for (int i = 0; i < xarrSize; ++i) {
           
         for (int j = 0; j < yarrSize; ++j) {
             scene->addItem(grid[i][j]);       
     
    
    //not possible to convert this array to GraphicsItem from a typ of char
             ui->view->setScene(grid);
                    }
         cout << "\n";
                }
    
    1 Reply Last reply
    0
    • projectbavariaP Offline
      projectbavariaP Offline
      projectbavaria
      wrote on last edited by projectbavaria
      #2
      QPixmap outPixmap = QPixmap(xarrSize,yarrSize);
        outPixmap.loadFromData(grid);
      
        // Bild-Datei anzeigen
      
        ui->label->setPixmap(outPixmap);
        ui->label->show();
      

      but doesn't know the loadFromData..
      i will try more

      JonBJ 1 Reply Last reply
      0
      • projectbavariaP projectbavaria
        QPixmap outPixmap = QPixmap(xarrSize,yarrSize);
          outPixmap.loadFromData(grid);
        
          // Bild-Datei anzeigen
        
          ui->label->setPixmap(outPixmap);
          ui->label->show();
        

        but doesn't know the loadFromData..
        i will try more

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @projectbavaria said in 2D array into graphicview to create Objects from 2D-Array:

        outPixmap.loadFromData(grid);
        but doesn't know the loadFromData..

        What type is grid? If it's your char grid[xarrSize][yarrSize] then your call does not correspond to either overload https://doc.qt.io/qt-5/qpixmap.html#loadFromData or https://doc.qt.io/qt-5/qpixmap.html#loadFromData-1.

        projectbavariaP 1 Reply Last reply
        1
        • JonBJ JonB

          @projectbavaria said in 2D array into graphicview to create Objects from 2D-Array:

          outPixmap.loadFromData(grid);
          but doesn't know the loadFromData..

          What type is grid? If it's your char grid[xarrSize][yarrSize] then your call does not correspond to either overload https://doc.qt.io/qt-5/qpixmap.html#loadFromData or https://doc.qt.io/qt-5/qpixmap.html#loadFromData-1.

          projectbavariaP Offline
          projectbavariaP Offline
          projectbavaria
          wrote on last edited by projectbavaria
          #4

          @JonB it is a 2D Array -> array Char [x][y]
          yeah i will look on your link

          1 Reply Last reply
          0
          • projectbavariaP Offline
            projectbavariaP Offline
            projectbavaria
            wrote on last edited by
            #5

            can't go on i will look From Matrix to QImage and QPixmap on forum.. maybe that will help

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              Do you mean you have a two dimensional array of chars that you want to build a grid with and that each of the cell of that grid shall contain the letter at that point of the array ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              projectbavariaP 1 Reply Last reply
              0
              • SGaistS SGaist

                Hi,

                Do you mean you have a two dimensional array of chars that you want to build a grid with and that each of the cell of that grid shall contain the letter at that point of the array ?

                projectbavariaP Offline
                projectbavariaP Offline
                projectbavaria
                wrote on last edited by
                #7
                This post is deleted!
                projectbavariaP 1 Reply Last reply
                0
                • projectbavariaP projectbavaria

                  This post is deleted!

                  projectbavariaP Offline
                  projectbavariaP Offline
                  projectbavaria
                  wrote on last edited by
                  #8
                  This post is deleted!
                  projectbavariaP 1 Reply Last reply
                  0
                  • projectbavariaP projectbavaria

                    This post is deleted!

                    projectbavariaP Offline
                    projectbavariaP Offline
                    projectbavaria
                    wrote on last edited by
                    #9

                    @projectbavaria ![alt text](Unbenannt.jpg image url)

                    1 Reply Last reply
                    0
                    • projectbavariaP Offline
                      projectbavariaP Offline
                      projectbavaria
                      wrote on last edited by
                      #10

                      Unbenannt.jpg ![0_1590505663278_14df9cf0-9d3f-4008-97bc-f55769443621-image.png](Uploading 100%)

                      now i getting a black screen and try to go on

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        From what you posted, there's nothing added to the scene and your view seems to be a local variable so unless it's in your main function body, it will be destroyed before you can even see it.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        projectbavariaP 1 Reply Last reply
                        0
                        • SGaistS SGaist

                          From what you posted, there's nothing added to the scene and your view seems to be a local variable so unless it's in your main function body, it will be destroyed before you can even see it.

                          projectbavariaP Offline
                          projectbavariaP Offline
                          projectbavaria
                          wrote on last edited by projectbavaria
                          #12

                          @SGaist but before i destroying it i have to handle that i want to save my array(inside there are my chars) as a QtPiximap -> after that i can show it in GraphisScene ?

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            Except that you are not using your array to draw anything. You are using a QBitArray which contains nothing to draw on a image that you then use as brush.

                            You should start by just drawing on that QImage properly and set it on a QLabel to see what it would look like. And once you have that working, come back to your QGraphicsView part.

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            projectbavariaP 1 Reply Last reply
                            0
                            • SGaistS SGaist

                              Except that you are not using your array to draw anything. You are using a QBitArray which contains nothing to draw on a image that you then use as brush.

                              You should start by just drawing on that QImage properly and set it on a QLabel to see what it would look like. And once you have that working, come back to your QGraphicsView part.

                              projectbavariaP Offline
                              projectbavariaP Offline
                              projectbavaria
                              wrote on last edited by
                              #14

                              @SGaist okey i will try

                              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