[Qt 5.6][QPixmap] 在 QLabel 中设置 QPixmap 时获取 "black picture with colored pixels"

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

(使用 Visual Studio Community 2015,Qt 5.6.0)

出于培训目的,我正在尝试使用信号和槽事件通过 QLabel 显示 RGB QPixmap。 (显示某种颜色预览)

为此,我为每个值(r、g 和 b)添加了三个滑块。例如,当我更新红色滑块时,它应该生成一个带有颜色值的新 QPixmap,然后将其放入 QLabel,如下所示:

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

效果不是很好,因为我得到的是黑色 QLabel,只有很少的彩色像素,例如 this。我真的不知道为什么会显示这个。

有人可以和我一起解决吗?

嗯,@peppe 是对的。我忘了用颜色填充 QPixmap。 :)

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

感谢您的回答!