Search code examples
qtcolorspixelqpixmapqlabel

[Qt 5.6][QPixmap] Getting "black picture with colored pixels" in QLabel when setting a QPixmap in it


(Using Visual Studio Community 2015, Qt 5.6.0)

For training purposes, I'm trying to display a RGB QPixmap through a QLabel using signals and slots events. (to display a sort of color preview)

For this, I added three sliders for each value (r, g and b). When I update for example the red slider, it is supposed to generate a new QPixmap with the color values then put it into a QLabel, like this :

void                Application::updateColorLabel(int value) {
    int             r, g, b;
    QPixmap         pixmap;
    QColor          color;

    this->ui.label_minValueR->setNum(value);
    pixmap = QPixmap(this->ui.label_color_preview->size());
    r = this->ui.label_minValueR->text().toInt();
    g = this->ui.label_minValueG->text().toInt();
    b = this->ui.label_minValueB->text().toInt();
    color = QColor(r, g, b);
    this->ui.label_color_preview->setPixmap(pixmap);
}

It doesn't work very well, since I get a black QLabel with few colored pixels, like this. I don't really know why it displays this.

Can someone figure it out with me, please ?


Solution

  • Well, @peppe was right. I forgot to fill the QPixmap with the color. :)

    void                Application::updateColorLabel(int value) {
        int             r, g, b;
        QPixmap         pixmap;
        QColor          color;
    
        this->ui.label_minValueR->setNum(value);
        r = this->ui.label_minValueR->text().toInt();
        g = this->ui.label_minValueG->text().toInt();
        b = this->ui.label_minValueB->text().toInt();
        color = QColor(r, g, b);
        pixmap = QPixmap(this->ui.label_color_preview->size());
        pixmap.fill(color);
        this->ui.label_color_preview->setPixmap(pixmap);
    }
    

    Thank you for your answer !