我试图从 Windows 中的文件中获取 48x48 或 256x256 的图标,但遇到了一个看似死胡同的问题。目前我在 python 中有一个 HICON 句柄(因为 PySides QFileIconProvider 只返回 32x32 图标),我想在 pyside 窗口中显示,但是没有实现像 QPixmap.fromHICON/HBITMAP 这样的函数,而且似乎也已从源代码中删除从 Qt 4.8(?) 开始。另外,我试图避免将图标保存到文件中。
那么,有没有什么方法可以将 HICON 或任何其他你可以将其转化为任何类型的 PySide 对象的东西?
编辑: 我一直在尝试简单地用 Python 中的 WinHBITMAP 函数重写旧函数,但效果并不理想。我不确定我应该如何将 src 行转换为 python,我也不知道如何更改 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;
}
}
目前我使用 win32api 从 HICON 创建一个 PyCBITMAP 并检索位列表。
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))
这导致“ValueError:无法修改 memoryview 对象的大小”
可在此处找到该函数的源代码:http://www.qtcentre.org/threads/19188-Converting-from-HBitmap-to-a-QPixmap?p=94747#post94747
最佳答案
已修复!
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
关于python - HICON/HBITMAP to QIcon/QPixmap/QImage/Anything with a Q in it,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15306866/