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. Trying to show many images using QGraphics, Need Help.
Forum Updated to NodeBB v4.3 + New Features

Trying to show many images using QGraphics, Need Help.

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 1.5k 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, All this days I tried to show a maze in a window, but all my options, were bad, even using QGridLayout.

    Someone told me to use this libraries QGraphicsView/Scene/..., cause may work better than my other way.
    But I still not figure out how to do this guys, can I get some help here?

    I have an int maze:
    @/*
    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*/@

    I have to show a maze, where each number represent an image, I tried to load two images at least, but seems that I have to make two variables of QGraphicsPixmapItem, just to show two images, now How I'm suppose to load a maze? I'm trying to figure out how to do this, but don't get any clue yet. Sorry, I'm starting with QT and don' t know much as some other people around here.
    The thing that I'm trying is not working cause, every time I reload the image of the QGraphicsPixmapItem, and try to move to the next position, the same image change and move without adding a new one, maybe cause there is only one item of QGraphicsPixmapItem on the Scene, I tried using a 2D Array, and adding each item to the Scene but didn't work.
    I appreciate any help, Thanks for reading.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      butterface
      wrote on last edited by
      #2

      Have you seen this example http://qt-project.org/doc/qt-4.8/demos-chip.html

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Asperamanca
        wrote on last edited by
        #3

        Posting some of your code might help us to better understand your problem.

        I would try the following (if I understand your problem correctly):

        Load all your pixmaps in a QList, so that your index refers to the correct pixmap:
        @
        // class member
        QList<QPixmap> m_PixmapList;

        // function to load the pixmaps
        void loadPixmaps
        {
        m_PixmapList.append(QPixmap("FilenameForPixmapWithIndex_0");
        m_PixmapList.append(QPixmap("FilenameForPixmapWithIndex_1");
        m_PixmapList.append(QPixmap("FilenameForPixmapWithIndex_2");
        // and so on
        }@

        Write a method that creates a new QGraphicsPixmapItem, adds it to the scene, and sets the correct pixmap:

        @QGraphicsPixmapItem* createAndInsertPixmap(int pixmapIndex)
        {
        QGraphicsPixmapItem* pItem = new QGraphicsPixmapItem();
        pItem->setPixmap(m_PixmapList.at(pixmapIndex);

        // Assume I have access to the QGraphicsScene with class member m_pScene
        m_pScene->addItem(pItem);
        }@

        Create the maze using two loops:

        @void createMaze()
        {
        const int mazeWidth; //TODO: Determine the width of the maze
        const int mazeHeight; //TODO: Determine the height of the maze
        const int pixmapWidth = 32; // Assumption, change as needed
        const int pixmapHeight = 32; // Assumption, change as needed
        for(int x=0; x < mazeWidth; ++x)
        {
        for (int y=0; y < mazeHeight; ++y)
        {
        const int pixmapIndex; // TODO: Determine pixmap index at maze position x/y
        QGraphicsPixmapItem* pItem = createAndInsertPixmap(pixmapIndex);
        // Position the new item
        pItem.setPos(xpixmapWidth, ypixmapHeight);
        }
        }
        }@

        (Code not tested and incomplete, but it should illustrate a way to do it)

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

          [quote author="Asperamanca" date="1394701057"]Posting some of....[/quote]

          Thanks for answer, you helped me a lot, gonna try it, I was trying to not use a QList, do you know an alternative to it? My project says I cant use that. If not I guess I'm gonna lose some points but whatever. I just wanna finish my project, I'm so tired.. Thanks for the reply.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Asperamanca
            wrote on last edited by
            #5

            Any container should do. I just used QList because I'm used to them. You can use std containers, or even a plain fixed-length array.

            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