English 中文(简体)
带有一栏图像的可读写
原标题:QTableView with a column of images

我有一份直言,以电网形式显示数据库的某些信息。 其中一个领域是形象的一条途径,我愿在我的桌上展示这些形象。

我与一名代表一起尝试过一些事情,但我不敢与他们混为一谈,我无法采取任何行动。 我也以以下作用尝试了:

    if index.column() == 4:
        if role == QtCore.Qt.DecorationRole:
            label = QtGui.QLabel()
            path = "path/to/my/picture.jpg"
            image = QtGui.QImage(str(path)) 
            pixmap = QtGui.QPixmap.fromImage(image)
            label.setPixmap(pixmap)
            return label

这部法典受到我在另一个论坛发现的、本应发挥作用的东西的启发。 然而,对我来说,它没有做任何事情,只是延缓了我守则的执行。

它为什么不发挥作用? 如果你有一位代表的话,我也想这样做!

感谢大家的关注

RESOLVED: I got it working with a custom delegate. Here is my code if someone s interested :

class ImageDelegate(QtGui.QStyledItemDelegate):

    def __init__(self, parent):
        QtGui.QStyledItemDelegate.__init__(self, parent)

    def paint(self, painter, option, index):        

        painter.fillRect(option.rect, QtGui.QColor(191,222,185))

        # path = "path	omyimage.jpg"
        path = "araignee_de_mer.jpg"

        image = QtGui.QImage(str(path))
        pixmap = QtGui.QPixmap.fromImage(image)
        pixmap.scaled(50, 40, QtCore.Qt.KeepAspectRatio)
        painter.drawPixmap(option.rect, pixmap) 
问题回答

感谢您的解决办法和守则实例,Johanna。 这是一个巨大的帮助。

此外,如果其他人需要,我就这样说:

(1) 安排贵方言一栏(我用我方言的话说的话语)的图像代表:

self.setItemDelegateForColumn(1, yourImageDelegate(parent))

2) 在您的ImageDelegate类别中,你可能想超负荷体积,以达到你的形象:

def sizeHint(self, option, index) :
    return QSize(160, 90) # whatever your dimensions are

Swoot!

i 试验时,即使用图(选项.rect.x(),选项.rect.y(),pixmap)来绘制icon,无。 比额表。

class ImageDelegate(QtGui.QStyledItemDelegate):
    def __init__(self, parent=None):
        QtGui.QStyledItemDelegate.__init__(self, parent)
        #self.icon =icon
    def paint(self, painter, option, index):
        #painter.fillRect(option.rect, QtGui.QColor(191,222,185))
        # path = "path	omyimage.jpg"
        path = "icon1.png"
        image = QtGui.QImage(str(path))
        pixmap = QtGui.QPixmap.fromImage(image)
        #pixmap.scaled(16, 16, QtCore.Qt.KeepAspectRatio)
        # when i test ,just use option.rect.x(), option.rect.y(), no need scaled 
        painter.drawPixmap(option.rect.x(), option.rect.y(),  pixmap)

我不认为,你需要把《和平图》归纳为“Label”或“形象”。 只是回到“和平计划”。

if index.column() == 4:
    if role == QtCore.Qt.DecorationRole:            
        path = "path/to/my/picture.jpg"
        pixmap = QtGui.QPixmap(path) 
        return pixmap




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签