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. How to get Qimage instances correctly implemented in a loop

How to get Qimage instances correctly implemented in a loop

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.2k 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.
  • D Offline
    D Offline
    doubitchou
    wrote on last edited by
    #1

    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);
    }
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      D 1 Reply Last reply
      0
      • mrjjM mrjj

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

        D Offline
        D Offline
        doubitchou
        wrote on last edited by
        #3

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

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

          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.

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

          D 2 Replies Last reply
          1
          • SGaistS SGaist

            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.

            D Offline
            D Offline
            doubitchou
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • SGaistS SGaist

              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.

              D Offline
              D Offline
              doubitchou
              wrote on last edited by
              #6

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

              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