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. QPainter: Colored matrix to paint
QtWS25 Last Chance

QPainter: Colored matrix to paint

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.3k 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.
  • F Offline
    F Offline
    fem_dev
    wrote on 29 Oct 2019, 17:35 last edited by
    #1

    I'm learning about how to paint a widget using QPainter (pixel per pixel) and I already saw the Basic Tools Plugin example in the Qt Creator, but I'm a little bit confuse about how to do this simple task below:

    std::vector< std::vector<QColor> > colored_matrix(3, 3);
    
    // Fill the 'colored_matrix' with QColor data (RGB)
    // Red:
    colored_matrix[0][0] = QColor(256, 0, 0, 0);
    colored_matrix[0][1] = QColor(256, 0, 0, 0);
    colored_matrix[0][2] = QColor(256, 0, 0, 0);
    // Green:
    colored_matrix[1][0] = QColor(0, 256, 0, 0);
    colored_matrix[1][1] = QColor(0, 256, 0, 0);
    colored_matrix[1][2] = QColor(0, 256, 0, 0);
    // Blue:
    colored_matrix[2][0] = QColor(0, 0, 256, 0);
    colored_matrix[2][1] = QColor(0, 0, 256, 0);
    colored_matrix[2][2] = QColor(0, 0, 256, 0);
    
    // Paint this matrix in a UI widget.
    

    a641e78c-0532-4b4e-ad2d-c2752167cfd5-image.png

    How can I get this output colored matrix UI (just the colors, without the cells names)?

    1 Reply Last reply
    0
    • F fem_dev
      29 Oct 2019, 18:24

      @SGaist yes...I understand the generic algorithm, but I don't know how to paint it on UI screen.

      Could you help me?

      F Offline
      F Offline
      fem_dev
      wrote on 29 Oct 2019, 19:48 last edited by
      #4

      @fem_dev here is the solution!

          // Set the image size:
          int sizeX = 400;
          int sizeY = 400;
      
          // Create and init the image:
          QImage image = QImage(sizeX, sizeY, QImage::Format_RGB32);
      
          // Fill the image with colors:
          for (int x = 0; x < sizeX; x++) {
              for (int y = 0; y < sizeY; y++) {
                  image.setPixel(x, y, qRgb(x, y, 200));
              }
          }
      
          // Create a graphics scene:
          QGraphicsScene* graphic = new QGraphicsScene(this);
      
          // Set the image:
          graphic->addPixmap(QPixmap::fromImage(image));
      
          // Display it on the UI:
          ui->graphicsView->setScene(graphic);
      
      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 29 Oct 2019, 17:39 last edited by
        #2

        Hi,

        Looks like you need two for loops that will draw the rectangles and pick the color from your matrix.

        Do you need any more details ?

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

        F 1 Reply Last reply 29 Oct 2019, 18:24
        0
        • S SGaist
          29 Oct 2019, 17:39

          Hi,

          Looks like you need two for loops that will draw the rectangles and pick the color from your matrix.

          Do you need any more details ?

          F Offline
          F Offline
          fem_dev
          wrote on 29 Oct 2019, 18:24 last edited by
          #3

          @SGaist yes...I understand the generic algorithm, but I don't know how to paint it on UI screen.

          Could you help me?

          F 1 Reply Last reply 29 Oct 2019, 19:48
          0
          • F fem_dev
            29 Oct 2019, 18:24

            @SGaist yes...I understand the generic algorithm, but I don't know how to paint it on UI screen.

            Could you help me?

            F Offline
            F Offline
            fem_dev
            wrote on 29 Oct 2019, 19:48 last edited by
            #4

            @fem_dev here is the solution!

                // Set the image size:
                int sizeX = 400;
                int sizeY = 400;
            
                // Create and init the image:
                QImage image = QImage(sizeX, sizeY, QImage::Format_RGB32);
            
                // Fill the image with colors:
                for (int x = 0; x < sizeX; x++) {
                    for (int y = 0; y < sizeY; y++) {
                        image.setPixel(x, y, qRgb(x, y, 200));
                    }
                }
            
                // Create a graphics scene:
                QGraphicsScene* graphic = new QGraphicsScene(this);
            
                // Set the image:
                graphic->addPixmap(QPixmap::fromImage(image));
            
                // Display it on the UI:
                ui->graphicsView->setScene(graphic);
            
            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 29 Oct 2019, 20:31 last edited by
              #5

              You are over-complicating things.

              If you want to use a QGraphicsScene, then use QGraphicsScene::addRect and pass the colour to the brush.

              Otherwise, QImage provides a fill method.

              For QPainter, you have QPainter::drawRect. Just modify the painter brush before paiting the rectangle.

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

              F 1 Reply Last reply 30 Oct 2019, 12:24
              0
              • S SGaist
                29 Oct 2019, 20:31

                You are over-complicating things.

                If you want to use a QGraphicsScene, then use QGraphicsScene::addRect and pass the colour to the brush.

                Otherwise, QImage provides a fill method.

                For QPainter, you have QPainter::drawRect. Just modify the painter brush before paiting the rectangle.

                F Offline
                F Offline
                fem_dev
                wrote on 30 Oct 2019, 12:24 last edited by
                #6

                @SGaist thank you. For my given colored matrix above you are right!

                I called this post above as a "solution" because, in the original first post, I was talking about a oversimplified view of my real problem. It was my fault!
                I tried to simplify the real problem a lot and I ended up changing the focus / resolution target. Sorry about that!

                Well, I want to develop a 2D histogram graph using Qt. So I have to set a color pixel per pixel on screen, like this example below:
                59d89013-b4c7-4f74-a99a-675fe01d40c4-image.png

                Using the source-code in this post above I can set the color values pixel per pixel.

                QUESTION:
                Is there a better/correct way to do this?

                In the nexts steps I need to understand how to:
                a) Create the X and Y axis (with ticks ans labels)
                b) Create the right-side color pallet (with ticks ans labels)
                c) Set a graph title

                Please, if you have any sugestions about how to implement these things, let me know.

                Thank you again!

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  JohanSolo
                  wrote on 30 Oct 2019, 13:12 last edited by
                  #7

                  It's always nice to see ROOT plots ;-)

                  Would QCustomPlot fullfill your needs?

                  `They did not know it was impossible, so they did it.'
                  -- Mark Twain

                  F 1 Reply Last reply 31 Oct 2019, 13:07
                  0
                  • J JohanSolo
                    30 Oct 2019, 13:12

                    It's always nice to see ROOT plots ;-)

                    Would QCustomPlot fullfill your needs?

                    F Offline
                    F Offline
                    fem_dev
                    wrote on 31 Oct 2019, 13:07 last edited by
                    #8

                    @JohanSolo Unfortunately the QCustomPlot doesn't have the 2D histogram plot.

                    I tryed to use ROOT and I saw that there was a ROOT + Qt 3/4 integration in the past.

                    So I downloaded the binary ROOT installer for Windows 10 x64 and installed it.
                    After that, i tryed to run the ROOT installed executable file.
                    It open the Windows Console and give a error about missing #include <new> and another things....and closes the console window...

                    May be there is a another plot library that can plot a 2D histogram and is better to integrate with Qt C++ GUI application.
                    Do you know another library?

                    1 Reply Last reply
                    0

                    2/8

                    29 Oct 2019, 17:39

                    topic:navigator.unread, 6
                    • Login

                    • Login or register to search.
                    2 out of 8
                    • First post
                      2/8
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved