setPixmap() doesn't show image!
-
Hi all, it's me again!
I was trying to use setPixmap() method to paint an image to a label,
which is called "ScreenLabel" derived from QLabel, but met a problem. Here is my code:void ScreenLabel::paintImageToLabel(const QImage &image, QPoint topLeftPosition) { selectedImage = image; QPixmap imageToPix = QPixmap::fromImage(image); this->resize(imageToPix.size()); //this->setFixedSize(imageToPix.size()); this->setPixmap(imageToPix); QString filename = "D:\\testhh.png"; this->pixmap()->save(filename); this->move(topLeftPosition); this->show(); }
And I called this method in my main widget class "ScreenShot" to paint selected image of the background into that label:
void ScreenShot::setSelectedImageToLabel(QPoint topLeftPoint, int w, int h) { QImage selectedImage = background.copy(topLeftPoint.x(), topLeftPoint.y(), w, h); imageLabel->paintImageToLabel(selectedImage, topLeftPoint); }
where "background" is a QImage type storing image information.
But this method didn't work, all I got is a white area without nothing painted,
namely the copied image was not set to the label pixmap.So I tried to debug by saving the label's pixmap to my local disk using
this->pixmap()->save(filename);
as in the above code. And this turned out to be right, there's nothing wrong
with the saved pixmap of this label, namely the selected area of the background image.
Here is the saved pixmap "testhh.png":Here are the declarations of my main widget "ScreenShot" and label "ScreenLabel":
class ScreenLabel : public QLabel { Q_OBJECT public: ScreenLabel(QWidget *); ... ... private: ... ... signals: ... ... public slots: ... ... } class ScreenShot : public QWidget { Q_OBJECT public: ScreenShot(QWidget *parent = 0); ... ... ... ... }
The ScreenLabel instance is created in the ScreenShot constructor as follows:
imageLabel = new ScreenLabel(this);
where the ScreenLabel constructor is defined as:
ScreenLabel::ScreenLabel(QWidget */*parent*/) : /* QLabel(parent), */ isPressed(false), ... ... { ... ... ... ... }
Here I commented out the QLabel(parent) initialization, this way I can get the above resulting pictures. However if I leave it there without commented out, I wouldn't even be able to get the white area (the first picture), namely nothing will be shown.
So what is the problem here, any ideas?
-
Locked continued here: https://forum.qt.io/topic/78128/qlabel-setpixmap-doesn-t-show-image