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 can i draw a QImage via QPixmap on my Widged
QtWS25 Last Chance

How can i draw a QImage via QPixmap on my Widged

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

    Hello,

    I want to fill a QImage via setPixel() with three different colors: green, red and blue. After this I want to convert the image into a pixmap: QPixmap :: from image (myImage) and draw the pixmap with my painter myPainter.drawPixmap (pixmap) on the widged.

    Problem: The program runs without errors but nothing has been drawn....

    Here is code:
    @void MainWindow::paintEvent(QPaintEvent *)
    {
    //TmpAttr
    int xSize = _world->getWorldSize().first;
    int ySize = _world->getWorldSize().second;
    QRect playingField(10, 10, xSize, ySize);

    enum type
    {
    fish = 1,
    shark = 2,
    water = 3,
    error = 255
    };
    
    QPainter myPainter(this);
    myPainter.fillRect(playingField, Qt::blue);
    
    QImage testImage(playingField.size(), QImage::Format_Indexed8);
    QVector<QRgb> _colors(256);
    _colors[fish] = Qt::green;
    _colors[shark] = Qt::red;
    _colors[water] = Qt::blue;
    _colors[error] = Qt::yellow;
    testImage.setColorTable(_colors);
    
    //Zeichne Pixel
    for(int x = 0; x < xSize; x++)
    {
        for (int y = 0; y < ySize; y++)
        {
            //std::cout << "Offset: " << counter << " Type: " << _world->getIndividumType(x, y) << std::endl;
            counter ++;
            
            switch (_world->getIndividumType(x, y))
            {
                case 1: //fish
                    testImage.setPixel(x, y, fish);
                    fishCounter++;
                    break;
                case 2: //shark
                    testImage.setPixel(x, y, shark);
                    sharkCouner++;
                    break;
                case 3: //water
                    testImage.setPixel(x, y, water);
                    waterCounter++;
                    break;
                default: //error
                    testImage.setPixel(x, y, error);
                    std::cout << "Error at: " << x << " " << y << std::endl;
                    break;
            }//end switch
        }//end for y
    }//end for x
    
    myPainter.drawPixmap(playingField, QPixmap::fromImage(testImage));
    

    }//end paint@

    Can anyone help me?

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

      Hi and welcome to devnet,

      Not a direct answer to your question but it seems that you are complicating things, why not use a QLabel and set your pixmap on it ? You could update your counters using timerEvent and call update.

      As for your question, what kind of widget is your MainWindow ?

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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MaXgOeKy
        wrote on last edited by
        #3

        Whats the different between setting a pixmap on a Label and setting it on the widget itself? Did u have a excample for the the timerEvent?

        How can i see what kind of widget i have?

        Sorry but im realy new to qt :)

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MaXgOeKy
          wrote on last edited by
          #4

          No idea anyone, why nothing is drawn?

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

            The QLabel handles the painting part for you :)

            @class MainWindow : public QWidget <- or QMainWindow etc...@

            You can see how to use it in "QObject::startTimer":http://qt-project.org/doc/qt-4.8/qobject.html#startTimer

            Also, I just checked the "QImage's documentation":http://qt-project.org/doc/qt-4.8/qimage.html#Format-enum drawing on a Format_Indexed8 image is not supported

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

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              A few remarks:
              I would recommend against doing the actual work generating the QImage in the paintEvent itself. The paintEvent needs to be fast, and your routine is making it slow. I would generate the image in another method and call update() on the widget when a repaint is needed based on the new image.

              You can also directly render a QImage. It depends on your platform if converting to QPixmap first (once, instead of in every call to paintEvent!) has speed benefits.

              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