PDA

View Full Version : Painting png image to widget, nothing displays



kainev
13th May 2016, 06:29
Hi Everyone!

I've just picked up Qt as I do a lot of work in Autodesk Maya, I'm using the 'PySide' python implementation though answers with the PyQt4 implementation or C++ is fine as well, I'll just try and translate :P

So I've got an icon, which is a 64x64 pixel PNG image, and I've got a custom widget that subclasses QWidget. In the paintEvent, I want to paint this image to the widget.

From looking up examples I thought the bare minimum I needed to do to make it display was:


import PySide.QtGui as QtGui
import PySide.QtCore as QtCore

class CustomWidget(QtGui.QWidget):
def __init__(self):
super(CustomWidget, self).__init__()

self._pixmap = QtGui.QPixmap("C:/.../icons/arrow_down.png")

def paintEvent(self, event):
painter = qg.QPainter(self)
painter.drawPixmap(event.rect(), self._pixmap)

This widget is just being added to a layout however no image is displayed. Everything else I've tried (setting a custom rect object etc) hasn't worked and I have no idea what to do.. any help is greatly appreciated.

Kinds Regards,
Kaine

EDIT:

I just saved my image as a jpeg, and with that same code above, it displays.. however I need to be able to make use of the PNGs alpha channel. My icon is a small arrow, and I want to display JUST the arrow, not any colour around it.

anda_skoa
13th May 2016, 09:25
Have you checked if the loaded pixmap is null?
Maybe your Qt is missing the image format plugin for PNG and thus fails to load the png file.

Check also the list returned by QImageReader::supportedFormats().

Cheers,
_

yeye_olive
13th May 2016, 09:26
Apparently your PNG file cannot be loaded in _pixmap. Are there any error messages showing up on the output stream or error stream when you run your program?

You can determine if the image was successfully loaded in the QPixmap by calling _pixmap.isNull() after construction. Alternatively, instead of constructing the QPixmap and loading the image in a single step, you can first construct an empty QPixmap (presumably with _pixmap = QtGui.QPixmap(), I am unsure about PySide's syntax), then load the file with _pixmap.load("C:/.../icons/arrow_down.png"), which returns a boolean value that indicates if the load was successful.

If your PNG file seems well formed but cannot be loaded, then you could post it and let others see if QPixmap manages to load it on their systems.

kainev
14th May 2016, 02:28
Thanks for the help!

I've managed to get it to display by deleting the PNG image and recreating it, something must have been wrong with the actual file I guess.. Cheers for the tip about isNull(). I was just checking the QPixmap object itself, not it's contents. I'm having some difficulties finding a solid set of documentation as there seems to be a few sets for each implementation and not much on PySide, though I've found that you can for the most part just replace 'PyQt4' with 'PySide' and everything works as normal. The only exceptions seem to be a few (very few) minor syntactical differences. Other than that I'm absolutely loving Qt.

Cheers,
Kaine