[PyQt] QIcon and KIcon not totally interchangeable in Python?

Andreas Pakulat apaku at gmx.de
Sat Dec 24 15:06:16 GMT 2011


Hi,

from the C++ API's for Qt and KDE I'm using to being able to pass in a
KIcon instance whenever an API wants a QIcon, since a KIcon is a QIcon
and the compiler will add an implicit conversion operator.

I now discovered that this does apparently not work with PyQt4/PyKDE4.
Note I'm using sip API v2 (i.e. no QVariant). In my particular case I'm
returning a KIcon from the data() function of a table-model and that
works just fine. But the tableview does not render the icon (or the
delegate rather I guess).

Explicitly creating a QIcon from the KIcon works around this, but I'm
wondering wether I'm missing something or this is indeed intended
behaviour.

Attached is a small example exposing the behaviour, as it is the entry
in the table is empty, if one switches the return-lines in the data
function it works.

Andreas

-------------- next part --------------
from PyKDE4 import kdeui, kdecore
from PyQt4 import QtCore, QtGui
import sys

class TableModel(QtCore.QAbstractTableModel):
    def __init__(self, parent=None):
        QtCore.QAbstractTableModel.__init__(self,parent)

    def rowCount(self, parent=QtCore.QModelIndex()):
        return 1
    def columnCount(self, parent=QtCore.QModelIndex()):
        return 1

    def data(self, idx, role):
        if role == QtCore.Qt.DecorationRole:
            if idx.row() == 0 and idx.column() == 0:
                # Switch the two lines and it starts to work
                #return QtGui.QIcon(kdeui.KIcon("media-playback-start"))
                return kdeui.KIcon("media-playback-start")
        return None

global app
appName = "dummy"
programName = kdecore.ki18n("Dummy")
version = "0.1.0"
aboutData = kdecore.KAboutData(appName, "", programName, version)

kdecore.KCmdLineArgs.init(sys.argv, aboutData)

app = kdeui.KApplication()
win = QtGui.QTableView()
model = TableModel()
win.setModel(model)
win.show()
app.exec_()



More information about the PyQt mailing list