python - HICON/HBITMAP to QIcon/QPixmap/QImage/Anything with a Q in it

标签 python pyside

我试图从 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/

相关文章:

python - python中将edgelist导入igraph的格式

python - 在 QStyledItemDelegate 中使用信号 closeEditor 的正确方法?

python - 如何将多个 QPushButton 添加到 QTableView?

python - 获取链接到 "QPushButton.clicked.connect"信号的函数

python - 我怎样才能找到virtualenv python的路径

javascript - 用 Python 编写的类 CoffeeScript 语言

python - 如果在字符串中找到括号,则删除括号和括号内的数据

python - 如何在 Makefile 中编写内联文件

python - Pyside QTableWidget 不等待单击事件中接受或拒绝对话框

qt - PyQt:keyPressEvent 和 keyReleaseEvent - 按住不反弹