No show of QPixmap
-
Below is snippet of my codes which I expected it to show a specified BMP picture, but it doesn't. Anything I missed? I see that "to create ..." and "Created ..." shown, but see no picture shown. I confirmed the picture existing too.
printf ("to create a QPixmap\n"); pixmap_RoadFault = QPixmap("/exe/bv/image/sdkpic.jpg").scaled(74,45); printf ("Created a QPixmap\n"); palette_RoadFault.setBrush(QPalette::Background, QBrush(pixmap_RoadFault)); palette_RoadFault.setBrush(QPalette::Background, QBrush(pixmap_RoadFault)); ui->label_RoadFault->setGeometry(480,120,80,50); ui->label_RoadFault->setPalette(palette_RoadFault); ui->label_RoadFault->setAutoFillBackground(true)
-
@Stan-Huang It's not clear from your snippet: What does
palette_RoadFault
do?The quickest way to show your QPixmap is to put inside a QLabel and
show()
the QLabel. -
When I see your other (deleted) question here https://forum.qt.io/topic/99218 I would guess /exe/bv/image/sdkpic.jpg does not exist or is not readable. You can check if the QPixmap is empty: http://doc.qt.io/qt-5/qpixmap.html#isNull
-
@Christian-Ehrlicher You're right in saying non-existence of picture to be shown being the cause of issue stated at the deleted one. But for this one, I'm sure it's existent and readable.
-
@Stan-Huang said in No show of QPixmap:
I am still new Qt user.
Then it's best to study simple examples.
Create a new, small Qt Widgets application:
#include <QApplication> #include <QPixmap> #include <QLabel> int main (int argc, char **argv) { QApplication app(argc, argv); QPixmap pixmap_RoadFault("/exe/bv/image/sdkpic.jpg"); QLabel label; label.setPixmap(pixmap_RoadFault); label.show(); return app.exec(); }
By the way, I've restored your other post. Please don't delete posts after you have found a solution.
-
@Stan-Huang said in No show of QPixmap:
I'm sure it's existent and readable.
Although you are 'sure' you should check it with the function I pointed you to ...
-
Hi,
What does
file /exe/bv/image/sdkpic.jpg
return ? -
@Stan-Huang Come on, be a bit more proactive!
Did you check whether your pixmap is null as @Christian-Ehrlicher suggested?
Also, /exe/bv/image/sdkpic.jpg is an absolute path and looks strange - are you REALLY sure it is correct?
Try what @SGaist suggested. -
@SGaist The return of "file /exe/bv/image/sdkpic.jpg" is:
root@mitacimx6:/opt/BVDemo/smartbox# file /exe/bv/image/sdkpic.jpg
/exe/bv/image/sdkpic.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 1108x1478, frames 3 -
@Stan-Huang said in No show of QPixmap:
The return of "file /exe/bv/image/sdkpic.jpg" is:
root@mitacimx6:/opt/BVDemo/smartbox# file /exe/bv/image/sdkpic.jpg
/exe/bv/image/sdkpic.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 1108x1478, frames 3OK, it looks like the file is valid.
Next, check if Qt can understand the file format. What do you get when you call
qDebug() << QImageReader::supportedImageFormats()
? (remember #include <QDebug> and #include <QImageReader>) -
@Christian-Ehrlicher setPixmap(pixmap_RoadFault) is void, so no return.
-
@Stan-Huang said in No show of QPixmap:
setPixmap(pixmap_RoadFault) is void, so no return.
Pleae read my link: http://doc.qt.io/qt-5/qpixmap.html#isNull - no setPixmap() but QPixmap::isNull() ...
-
-
What if you set the pixmap directly on the QLabel rather than using a brush in the palette ?
-
@Stan-Huang
It's not quite clear from the code you posted, but are you sure thatpalette_RoadFault
is not set to something invalid somewhere else in your code, and thatpalette_RoadFault
persists outside the scope of your function?QBrush expects a const reference to a QPixmap, I don't think it will take ownership or make a copy of the QPixmap for drawing.