Trying to show many images using QGraphics, Need Help.



  • 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.





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



  • [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.



  • 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.


Log in to reply
 

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