PyQt QTableView 更新到 PyQt 4.5.1 后不显示图标

发布于 2024-09-24 17:25:04 字数 5123 浏览 0 评论 0原文

我会尽量说得清楚,尽管这在我的脑海里有点混乱。

我有一个 PyQt 应用程序,已经运行了大约一年。更新到 PyQt 4.5.1(从 4.3.3 开始)后,我的图标不再出现在 QTableView 中(此更新与从 2.5.1 到 python 2.6.5 的更新同时进行)。恢复到旧版的 python 和 PyQt,一切都按预期工作。

细分是这样的:

我正在使用模型视图方法。当通过 data() 方法中的 Qt.DecorationRole 请求时,我的模型将返回一个自定义对象 (ColorSwatch),该对象是 QIcon 类的子类。这一直有效(但需要注意的是,由于我不明白的原因,我必须首先将其重新转换为 QVariant)。更新到 PyQt 4.5.1 后,它似乎可以正确运行(即我没有收到任何错误),但图标没有绘制(尽管绘制它的空间是“保留的”,即文本已向右移动,为这个不可见的图标让路)。

以下是我尝试过的一些事情:

我已经验证 ColorSwatch 类仍然有效。同样的类用于将图标绘制到上下文菜单中 - 并且它们显示正确。

我已经验证 data() 方法实际上被调用并且正在返回此 ColorSwatch 对象(重新转换为 QVariant <- 尽管我也没有进行此重新转换进行了测试)。

将蛇血倒在我的键盘上并点燃它。

到目前为止,我还没有任何线索知道我应该做什么。任何提示将不胜感激。谢谢。

是一些(可能)相关的代码(请注意 paramObj.get_icon() 返回一个 ColorSwatch 对象):

#---------------------------------------------------------------------------
def data(self, index, role=QtCore.Qt.DisplayRole):
    """
    Returns the text or formatting for a particular cell, depending on the 
    role supplied.
    """


    blah
    blah
    blah



    elif role == QtCore.Qt.DecorationRole:
        if platform.system()=='Darwin':
            return QtGui.QIcon(paramObj.get_icon())
        else:
            return QtCore.QVariant(paramObj.get_icon())

以下

import os
import tempfile
import sys
import colorsys
import copy
import fnmatch
import time

from PyQt4 import QtGui
from PyQt4 import QtCore


################################################################################
class ColorSwatch(QtGui.QIcon):
    """
    A subclass of QIcon, this class draws a colored paint chip with a border
    The color and size are determined at construction time, and cannot
    be changed later.
    """

    #---------------------------------------------------------------------------
    def __init__(self, r=1, g=1, b=1, br = 0, bg = 0, bb = 0, w=20, h=20):
        """
        Constructor for the ColorSwatch class. Takes the passed arguments and
        creates a square icon filled with the given color and with a border
        color determined by br, bg, bb. All colors should be in floating point
        format.
        """
        QtGui.QIcon.__init__(self)

        #normalize the color
        r8, g8, b8 = self.normalize_color((r, g, b))

        #convert the r, g, b values to 8 bit colors
        r8, g8, b8 = self.fp_to_8b_color((r8, g8, b8))

        #Create the pixmap and painter objects
        paintChip = QtGui.QPixmap(w, h)
        painter = QtGui.QPainter()
        painter.begin(paintChip)

        #fill the swatch
        baseColor = QtGui.QColor(r8, g8, b8)
        painter.fillRect(0, 0, w, h, baseColor)

        #if any of the values were super brights (>1), draw a smaller, white
        #box inset to make sure the user knows
        if r > 1 or g > 1 or b > 1:
            painter.fillRect(5, 5, w-10, h-10, QtGui.QColor(255, 255, 255))

        #if all values are 0, put a faint x through the icon
# # #         brush = QtGui.QBrush()
# # #         brush.setColor(QtGui.QColor(30, 30, 30))
        painter.setPen(QtGui.QColor(200, 200, 200))
        if r ==0 and g == 0 and b == 0:
            painter.drawLine(0, 0, w, h)
            painter.drawLine(w-1, 0, -1, h)
# # #         
# # #         #normalize the color
# # #         r8, g8, b8 = self.normalize_color((r8, g8, b8))

        #now draw the border(s)
        #convert the r, g, b values to 8 bit colors
        r8, g8, b8 = self.fp_to_8b_color((br, bg, bb))

        #draw the border
        painter.setPen(QtGui.QColor(r8, g8, b8))
        painter.drawRect(0,0,w-1,h-1)

        #if any of the values were super brights (>1), draw a border around the
        #inset box as well.
        if r > 1 or g > 1 or b > 1:
            painter.drawRect(5,5,w-11,h-11)

        #done drawing
        painter.end()

        #add it (both to the normal and the selected modes)
        self.addPixmap(paintChip, QtGui.QIcon.Normal)
        self.addPixmap(paintChip, QtGui.QIcon.Selected)


    #---------------------------------------------------------------------------
    def fp_to_8b_color(self, color):
        """
        Convert a floating point color value (passed in the form of a three 
        element tuple) to a regular 8-bit 0-255 value. Returns a 3 item tuple.
        """
        r = max(min(int(color[0]*255),255),0)
        g = max(min(int(color[1]*255),255),0)
        b = max(min(int(color[2]*255),255),0)
        return (r,g,b)


    #---------------------------------------------------------------------------
    def normalize_color(self, color):
        """
        "normalizes" a color value so that if there are any super-whites, it 
        balances all the other floating point values so that we end up with a 
        "real" color.  Negative values will result in undefined behavior.
        Mainly used to make the color chip "look right" when using super whites.
        """
        maxValue = max(color)
        if maxValue > 1:
            return (color[0]/maxValue, color[1]/maxValue, color[2]/maxValue)
        else:
            return color

I'll try to be as clear as possible, though this is all a bit muddled in my head.

I have a PyQt application that has been working for about a year now. After updating to PyQt 4.5.1 (from 4.3.3) none of my icons appear in the QTableView anymore (this update was concurrent with an update to python 2.6.5 from 2.5.1). Reverting to the older python and PyQt, everything works as expected.

The breakdown is this:

I am using the model-view methodology. My model, when requested via a Qt.DecorationRole in the data() method, will return a custom object (ColorSwatch) that is a subclass of the QIcon class. This has always worked (with the caveat that I, for reasons I don't understand, have to recast it as a QVariant first). After updating to PyQt 4.5.1 it appears to run correctly (i.e. I am not getting any errors), but the icon does not draw (though the space where it would be drawn is "reserved" i.e. the text has been shifted to the right to make way for this invisible icon).

Here are some things that I have tried:

I have verified that the ColorSwatch class does still function. This same class is used to draw icons into a contextual menu - and they appear correctly.

I have verified that the data() method is actually getting called and is returning this ColorSwatch object (recast into a QVariant <- though I have tested without this recasting as well).

Pouring snake blood onto my keyboard and lighting it afire.

Nothing so far has given me any clue as to what I should do. Any hints would be greatly appreciated. Thanks.

Here is some of the (potentially) relevant code (note that paramObj.get_icon() returns a ColorSwatch object):

#---------------------------------------------------------------------------
def data(self, index, role=QtCore.Qt.DisplayRole):
    """
    Returns the text or formatting for a particular cell, depending on the 
    role supplied.
    """


    blah
    blah
    blah



    elif role == QtCore.Qt.DecorationRole:
        if platform.system()=='Darwin':
            return QtGui.QIcon(paramObj.get_icon())
        else:
            return QtCore.QVariant(paramObj.get_icon())

and

import os
import tempfile
import sys
import colorsys
import copy
import fnmatch
import time

from PyQt4 import QtGui
from PyQt4 import QtCore


################################################################################
class ColorSwatch(QtGui.QIcon):
    """
    A subclass of QIcon, this class draws a colored paint chip with a border
    The color and size are determined at construction time, and cannot
    be changed later.
    """

    #---------------------------------------------------------------------------
    def __init__(self, r=1, g=1, b=1, br = 0, bg = 0, bb = 0, w=20, h=20):
        """
        Constructor for the ColorSwatch class. Takes the passed arguments and
        creates a square icon filled with the given color and with a border
        color determined by br, bg, bb. All colors should be in floating point
        format.
        """
        QtGui.QIcon.__init__(self)

        #normalize the color
        r8, g8, b8 = self.normalize_color((r, g, b))

        #convert the r, g, b values to 8 bit colors
        r8, g8, b8 = self.fp_to_8b_color((r8, g8, b8))

        #Create the pixmap and painter objects
        paintChip = QtGui.QPixmap(w, h)
        painter = QtGui.QPainter()
        painter.begin(paintChip)

        #fill the swatch
        baseColor = QtGui.QColor(r8, g8, b8)
        painter.fillRect(0, 0, w, h, baseColor)

        #if any of the values were super brights (>1), draw a smaller, white
        #box inset to make sure the user knows
        if r > 1 or g > 1 or b > 1:
            painter.fillRect(5, 5, w-10, h-10, QtGui.QColor(255, 255, 255))

        #if all values are 0, put a faint x through the icon
# # #         brush = QtGui.QBrush()
# # #         brush.setColor(QtGui.QColor(30, 30, 30))
        painter.setPen(QtGui.QColor(200, 200, 200))
        if r ==0 and g == 0 and b == 0:
            painter.drawLine(0, 0, w, h)
            painter.drawLine(w-1, 0, -1, h)
# # #         
# # #         #normalize the color
# # #         r8, g8, b8 = self.normalize_color((r8, g8, b8))

        #now draw the border(s)
        #convert the r, g, b values to 8 bit colors
        r8, g8, b8 = self.fp_to_8b_color((br, bg, bb))

        #draw the border
        painter.setPen(QtGui.QColor(r8, g8, b8))
        painter.drawRect(0,0,w-1,h-1)

        #if any of the values were super brights (>1), draw a border around the
        #inset box as well.
        if r > 1 or g > 1 or b > 1:
            painter.drawRect(5,5,w-11,h-11)

        #done drawing
        painter.end()

        #add it (both to the normal and the selected modes)
        self.addPixmap(paintChip, QtGui.QIcon.Normal)
        self.addPixmap(paintChip, QtGui.QIcon.Selected)


    #---------------------------------------------------------------------------
    def fp_to_8b_color(self, color):
        """
        Convert a floating point color value (passed in the form of a three 
        element tuple) to a regular 8-bit 0-255 value. Returns a 3 item tuple.
        """
        r = max(min(int(color[0]*255),255),0)
        g = max(min(int(color[1]*255),255),0)
        b = max(min(int(color[2]*255),255),0)
        return (r,g,b)


    #---------------------------------------------------------------------------
    def normalize_color(self, color):
        """
        "normalizes" a color value so that if there are any super-whites, it 
        balances all the other floating point values so that we end up with a 
        "real" color.  Negative values will result in undefined behavior.
        Mainly used to make the color chip "look right" when using super whites.
        """
        maxValue = max(color)
        if maxValue > 1:
            return (color[0]/maxValue, color[1]/maxValue, color[2]/maxValue)
        else:
            return color

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晚风撩人 2024-10-01 17:25:04

伊沃在上面回答了我的问题。

实际有效的代码是:

#---------------------------------------------------------------------------
def data(self, index, role=QtCore.Qt.DisplayRole):
    """
    Returns the text or formatting for a particular cell, depending on the 
    role supplied.
    """


    blah
    blah
    blah



    elif role == QtCore.Qt.DecorationRole:
        if platform.system()=='Darwin':
            return QtGui.QIcon(paramObj.get_icon())
        else:
            return QtCore.QVariant(QtGui.QIcon(paramObj.get_icon()))
            #Note that it is first cast as a QIcon before
            #being cast as a QVariant.

再次感谢伊沃。

Ivo answered my question above.

the actual code that works is:

#---------------------------------------------------------------------------
def data(self, index, role=QtCore.Qt.DisplayRole):
    """
    Returns the text or formatting for a particular cell, depending on the 
    role supplied.
    """


    blah
    blah
    blah



    elif role == QtCore.Qt.DecorationRole:
        if platform.system()=='Darwin':
            return QtGui.QIcon(paramObj.get_icon())
        else:
            return QtCore.QVariant(QtGui.QIcon(paramObj.get_icon()))
            #Note that it is first cast as a QIcon before
            #being cast as a QVariant.

Thanks again Ivo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文