Solved QLabel::setPixmap() doesn't show image!
-
@David406
hi,
maybe you haven't released your pixmap correctly when you're finished with the drawing?You could also try to convert you QPixmap to a QImage, as a step in between
QImage = pixmapForDrawing.toImage(); selectedImageLabel->setPixmap( QPixmap::fromImage(pixmapForDrawing));
to see, if that changes anything.
-
@J.Hilk
Thanks.
Did you mean:drawLine(pixmapForDrawing, me->pos()); QImage imageForDrawing = pixmapForDrawing.toImage(); selectedImageLabel->setPixmap(QPixmap::fromImage(imageForDrawing)); QString filename = "D:\\test.png"; (selectedImageLabel->pixmap())->save(filename);
This didn't work. The saved png file is right, but not showing on the label.
Oh, this is my drawLine method, although I don't think it's relevent:
void ScreenShot::drawLine(QPixmap &pixmap, const QPoint &endPoint) { QPainter painter(&pixmap); painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); painter.drawLine(selectedImageLabel->mapFromGlobal(lastPenPoint), selectedImageLabel->mapFromGlobal(endPoint)); //painter.drawLine(QPoint(0, 0), QPoint(1000, 1000)); lastPenPoint = endPoint; }
-
@David406 said in QLabel::setPixmap() doesn't show image!:
avoiding subclassing QLabel. But here, I met similar problem again!
Are you subclassing QLabel? could you show us the code of that subclass?
-
@VRonin
No, the selectedImageLabel is just an instance of QLabel, not a subclass instance. It's created as a member in the ScreenShot class:QLabel *selectedImageLabel;
-
@David406
ok to make sure, it works at all. Would this show the correct Image?drawLine(pixmapForDrawing, me->pos()); QLabel *lbl = new QLabel(); lbl->setPixmap(pixmapForDrawing); lbl->show();
-
@J.Hilk
This works correctly: A series of windows pops up with lines painted on the original pixmap. -
@David406
Alright, we narrowed the problem down. It's withselectedImageLabel
. My guess, if its a standard QLabel, the pixmap is somewhere else overwriten. -
@J.Hilk
Thanks.
Yes, it's the problem somewhere with selectedImageLabel. But I don't quite understand what you mean by saying overwritten. The saved pixmap of selectedImageLabel is all right. It just doesn't update and show up on the label. -
@David406
Out of the top of my head, I can only think of 3 reasons, why a QLabel wouldn't show a pixmap.- It gets a new Pixmap(overwritten) before the paintevent could draw the old one
- The Lable is actually not visible. Other widget on top or hidden
- A stylesheet forces a different image to be drawn.
-
@J.Hilk
Thanks.
The functionality of my code is:- Select an area of the screen with mouse and set the selected image to label, move and show the label on the selected area dynamicly.
- After selection is done, images can be drawn on the label.
And now, the first part of my code works correctly, here is how it actually works:
In the above picture, the highlighted area is actually a label with selected image as its pixmap. So you can see the label is shown correctly.
However, the second part of my code doesn't work. Namely, pixmap on the label can't be updated (or reset) with its original pixmap modified.
-
Thank you all, finally I found that the problem is in the implementation of paintEvent().