[prev in list] [next in list] [prev in thread] [next in thread]
List: pykde
Subject: Re: [PyQt] QStyledItemDelegate option properties?
From: Eric Thomson <thomson.eric () gmail ! com>
Date: 2015-12-13 17:55:51
Message-ID: CAP4_qhnf6ynAmTJkD0dwTksJ8rBAzKTWTY=PzDFWMUMCm1SrBQ () mail ! gmail ! com
[Download RAW message or body]
> In Qt 4.8.6/PyQt 4.10.4, I am reimplementing the `paint` method in a
> `QStyledItemDelegate`, just to help myself understand the default
> mechanics of tree views.
>
> The second input to the `paint` method is `option`, a
> `QStyleOptionViewItem`, which is supposed to be an instance of
> `QStyleOptionViewItemV4`:
>
> http://pyqt.sourceforge.net/Docs/PyQt4/qstyleoptionviewitemv4.html
>
> Such objects are supposed to contain information about the three main
> subelements of an item in a view: the text, icon, and checkstate.
> However, whenever I try to access the text (using option.text) I
> always get an empty string. That is, option seems to be incomplete.
> When I print option.version, it returns 4, so the typing seems right.
> Am I misreading what option.text is supposed to be? The index has the
> data, so I typically just pull it from there when I reimplement paint,
> but I'm wondering what is going on under the hood.
Just to make it more clear, I've attached a SSCCE with a table view
that illustrates the behavior (optionDelegatePyqt.py).
["optionDelegatePyqt.py" (text/x-python)]
import sys
from PyQt4.QtCore import (QT_VERSION_STR, PYQT_VERSION_STR)
from PyQt4.QtGui import (QApplication, QTableView, QStandardItemModel,
QStandardItem, QStyledItemDelegate)
class SimpleTable(QTableView):
def __init__(self, parent = None):
QTableView.__init__(self, parent)
model = QStandardItemModel()
items = [QStandardItem('Sneeze') , QStandardItem('Cough') ]
model.appendColumn(items)
self.setModel(model)
self.setItemDelegate(ShowOptionDelegate())
class ShowOptionDelegate(QStyledItemDelegate):
def __init__(self, parent=None):
QStyledItemDelegate.__init__(self, parent)
def paint(self, painter, option, index):
if index.column()==0 and index.row()==0:
text = index.model().data(index)
print "\n\noption.text: '{0}' (length {1})\nindex text: '{2}' (length {3})".\
format(option.text, len(option.text), text, len(text))
QStyledItemDelegate.paint(self, painter, option, index)
def main():
app = QApplication(sys.argv)
print "Qt version:", QT_VERSION_STR
print "PyQt version: ", PYQT_VERSION_STR
myTable = SimpleTable()
myTable.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
[Attachment #4 (text/plain)]
_______________________________________________
PyQt mailing list PyQt@riverbankcomputing.com
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic