HICON / HBITMAP for QIcon / QPixmap / QImage / Anything with Q in it

I am trying to get 48x48 or 256x256 icons from files on Windows and are facing what seems like a dead end. At the moment I have a HICON descriptor (since PySides QFileIconProvider returns only 32x32 icons) in python, which I would like to show in the pyside window, but functions like QPixmap.fromHICON / HBITMAP are not implemented and also seem to have been removed from source since Qt 4.8 (?). In addition, I try not to save the icon in the file.

So, is there a way to get HICON, or perhaps any other things you can include it in, with any PySide object?

EDIT: I tried to just rewrite the old function from the WinHBITMAP function in python, but that is not great. I'm not sure how I can translate the src string in python, and I don't even know how to change the value of the memory buffer returned by QImage.scanLine ()

for (int y=0; y<h; ++y) { QRgb *dest = (QRgb *) image.scanLine(y); const QRgb *src = (const QRgb *) (data + y * bytes_per_line); for (int x=0; x<w; ++x) { dest[x] = src[x] | mask; } } 

I am currently creating PyCBITMAP from HICON with win32api and retrieving a list of bits.

 for y in range(0, hIcon.height): dest = i.scanLine(y) src = bitmapbits[y*hIcon.widthBytes:(y*hIcon.widthBytes)+hIcon.widthBytes] for x in range(0, hIcon.width): dest[x] = bytes(ctypes.c_uint32(src[x] | 0)) 

As a result of "ValueError: unable to resize memoryview object"

The source for the function can be found here: http://www.qtcentre.org/threads/19188-Converting-from-HBitmap-to-a-QPixmap?p=94747#post94747

+4
source share
2 answers

Fixed!

 def iconToQImage(hIcon): hdc = win32ui.CreateDCFromHandle(win32gui.GetDC(0)) hbmp = win32ui.CreateBitmap() hbmp.CreateCompatibleBitmap(hdc, hIcon.width, hIcon.height) hdc = hdc.CreateCompatibleDC() hdc.SelectObject(hbmp) win32gui.DrawIconEx(hdc.GetHandleOutput(), 0, 0, hIcon.hIcon, hIcon.width, hIcon.height, 0, None, 0x0003) bitmapbits = hbmp.GetBitmapBits(True) image = QtGui.QImage(bitmapbits, hIcon.width, hIcon.height, QtGui.QImage.Format_ARGB32_Premultiplied) return image 
+2
source

It's a little difficult to get this setup, but I read that the Python Imaging Library (PIL) supports raster and ICO files and has downloads for Windows . Assuming you can get the icon file name, you can load it with PIL and then pass the raw data to QImage :

 from PIL import Image from PySide.QtGui import QImage, QImageReader, QLabel, QPixmap, QApplication im = Image.open("my_image.png") data = im.tostring('raw', 'RGBA') app = QApplication([]) image = QImage(data, im.size[0], im.size[1], QImage.Format_ARGB32) pix = QPixmap.fromImage(image) lbl = QLabel() lbl.setPixmap(pix) lbl.show() app.exec_() 

Then work with any QImage operation you need to do from there.

+1
source

All Articles