How to get Qimage instances correctly implemented in a loop



  • hi,

    I've implemented a QButtonGroup in the form of a 8x8 Matrix which lets draw some patterns each time you click on a button. The final goal is : when you have done drawing the pattern you want, you ask to save it to a ListWidget to be able to see all your patterns and to reload back to the main matrix any save pattern by a click.

    Like this

    some precisions :

    • I use the internal QVector<QVector<QRgb>> SequenceVect; to save it on a file
    • CurrentGUIMatrix->GetButtonColor(BtnID) let ask the QButtongroup GUIMatrix what is the color of the current bouton BtnID

    The code below does a part of what is described here, however it reproduces always the same image as I don't see how to implement it a different way in the loop from PushGUIPattern_ToSequence. To be honest, I just begin using QPaint and drawing API with Qt, so I'm sure there is a better way to do that.

    void MyClass::MatrixSnapshot(QImage &imageTest, uint BtnID, QRgb BtnCol )
    {
        uint GridWith  = imageTest.width() ;
        uint GridHeight = imageTest.height();
    
        imageTest.fill(Qt::gray);
    
        QPainter p;
        p.begin(&imageTest);
    
        //    Draw the grid
           p.setPen(QPen(QColor(Qt::black)));
           QVector<QLine> GridLines ;
           uint step= GridWith/MatxCols ;
    
               // Vertical Lines
           for (uint ind=0; ind < MatxCols ; ind++)
               GridLines.append( QLine( step+(ind*step),0,step+(ind*step),GridWith ));
    
        // Horizontal Lines
           for (uint ind=0; ind < MatxRows ; ind++)
               GridLines.append( QLine( 0, step +(ind*step),GridHeight,step + (ind*step) ));
    
            p.drawLines(GridLines);
    
                   // now let's copy the current Pattern into the grid
    
            p.setBrush(QBrush(QColor( BtnCol )));
            uint Current_Row= BtnID/8 ;
            uint Current_Col = BtnID -(8*Current_Row);
            p.drawRect( QRect(Current_Col*step, Current_Row*step, step-1,step-1)) ; 
        p.end();
    }
    
    // Goal : when you are happy with a pattern, you need to "take a picture" of it and
    // save it into a sequence list.
    // Parse the GUImatrix QButtonGroup and retrieve the color for each of the button in a 2D Vector 
    
    void MyClass::PushGUIPattern_ToSequence()
    {
        QImage* imageTest;(40,40, QImage::Format_RGB32);
    
        for ( uint BtnID = 0; BtnID < MatxRows*MatxCols; BtnID++ )
        {
            QVector<QRgb> inner_vector;
            QRgb BtnColor= CurrentGUIMatrix->GetButtonColor(BtnID)   ;
            inner_vector.push_back(  BtnColor  );
            MatrixSnapshot(imageTest,BtnID, BtnColor ) ;
            SequenceVect.push_back(inner_vector);
        }
                // Create a snapshot to GUI
        QListWidgetItem *SnapshotItem = new QListWidgetItem("Pattern1", SequenceList);
        SnapshotItem->setData(Qt::DecorationRole, QPixmap::fromImage(imageTest));
        SequenceList->insertItem(1, SnapshotItem);
    }
    

  • Qt Champions 2016

    Hi
    Im not sure about
    " however it reproduces always the same image"

    Like it saves the same regardless of what "cells" you color ?

    Also this line looks funky to me ? copy & paste error?
    ( seems to miss the new keyword)

    QImage* imageTest;(40,40, QImage::Format_RGB32);



  • @mrjj

    Exact, on the picture whatever the colors are , the image will always stay grey, which is the default value for each button.

    For the 2nd question : you are right again. Whereas I know that the new should be present in the loop, I don't see how this can be done as the call for the Snapshot requires the address for the p.begin(&imagetest).

    Overall, I think my code could be really improved but I have improvised by testing with Qpaint examples.


  • Lifetime Qt Champion

    Hi,

    One of the first thing you do in MatrixSnapshot is to fill your picture with grey so in the end the only square that will be visible if changed is the last one.



  • @SGaist said in How to get Qimage instances correctly implemented in a loop:

    Hi,

    One of the first thing you do in MatrixSnapshot is to fill your picture with grey so in the end the only square that will be visible if changed is the last one.

    HI Sgaist.

    so in the end the only square that will be visible if changed is the last one.
    

    what is the proper way ? if not fill it at all there are strange colors in the image as it's not initialized.



  • @SGaist

    this is confusing. I've removed the fill and it works nicely.
    I was so sure that the new Qimage was missing in the loop.

    May i ask you advice about this implementation : how would you proceed to improve it ?


Log in to reply
 

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