Problems with Open-Source Downloads read and

How can i draw a QImage via QPixmap on my Widged

  • 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 *)
    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;
    //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);
                case 2: //shark
                    testImage.setPixel(x, y, shark);
                case 3: //water
                    testImage.setPixel(x, y, water);
                default: //error
                    testImage.setPixel(x, y, error);
                    std::cout << "Error at: " << x << " " << y << std::endl;
            }//end switch
        }//end for y
    }//end for x
    myPainter.drawPixmap(playingField, QPixmap::fromImage(testImage));

    }//end paint@

    Can anyone help me?

  • Lifetime Qt Champion

    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 ?

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

  • No idea anyone, why nothing is drawn?

  • Lifetime Qt Champion

    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":

    Also, I just checked the "QImage's documentation": drawing on a Format_Indexed8 image is not supported

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

Log in to reply