Results 1 to 4 of 4

Thread: Painting png image to widget, nothing displays

  1. #1
    Join Date
    May 2016
    Posts
    3
    Qt products
    Platforms
    Windows

    Default Painting png image to widget, nothing displays

    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:

    Qt Code:
    1. import PySide.QtGui as QtGui
    2. import PySide.QtCore as QtCore
    3.  
    4. class CustomWidget(QtGui.QWidget):
    5. def __init__(self):
    6. super(CustomWidget, self).__init__()
    7.  
    8. self._pixmap = QtGui.QPixmap("C:/.../icons/arrow_down.png")
    9.  
    10. def paintEvent(self, event):
    11. painter = qg.QPainter(self)
    12. painter.drawPixmap(event.rect(), self._pixmap)
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by kainev; 13th May 2016 at 07:35. Reason: updated contents

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Painting png image to widget, nothing displays

    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,
    _

  3. #3
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Painting png image to widget, nothing displays

    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.

  4. #4
    Join Date
    May 2016
    Posts
    3
    Qt products
    Platforms
    Windows

    Default Re: Painting png image to widget, nothing displays

    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

Similar Threads

  1. Painting one Image from another Image with paint brush
    By avanindra in forum Qt Programming
    Replies: 1
    Last Post: 21st July 2012, 13:08
  2. Replies: 1
    Last Post: 7th September 2008, 11:47
  3. Painting an image on QItemDelegates
    By sprek in forum Qt Programming
    Replies: 4
    Last Post: 12th June 2008, 01:00
  4. custom widgets painting image formats
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 12th March 2007, 11:33
  5. Trouble with image painting (QPainter + QImage)
    By krivenok in forum Qt Programming
    Replies: 1
    Last Post: 4th February 2006, 21:25

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.