[PyQt] setItemDelegateForColumn() doesn't work? (so setItemDelegateForRow() does)
Zhaohao Liang
liangvls at gmail.com
Mon Jan 4 05:52:33 GMT 2010
Hi
I'm using PyQt 4.6.2 for Python 2.6. It seems that the
setItemDelegateForColumn() of QTableWidget doesn't work.
I have checked it with below code:
print ui.tableWidget.itemDelegateForColumn(0)
ui.tableWidget.setItemDelegateForColumn(0, MyItemDelegate())
print ui.tableWidget.itemDelegateForColumn(0)
It will print two "None" as result.
ps: This will happen for setItemDelegateForRow()
Please help to check it. Thank you!
#---------------
#Here is the sample code:
import sys
from PyQt4 import QtCore, QtGui
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.tableWidget = QtGui.QTableWidget(Dialog)
self.tableWidget.setGeometry(QtCore.QRect(60, 40, 256, 192))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(2)
self.tableWidget.setRowCount(1)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(0, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog",
"Dialog", None, QtGui.QApplication.UnicodeUTF8))
self.tableWidget.verticalHeaderItem(0).setText(QtGui.QApplication.translate("Dialog",
"row0", None, QtGui.QApplication.UnicodeUTF8))
self.tableWidget.horizontalHeaderItem(0).setText(QtGui.QApplication.translate("Dialog",
"column0", None, QtGui.QApplication.UnicodeUTF8))
self.tableWidget.horizontalHeaderItem(1).setText(QtGui.QApplication.translate("Dialog",
"column1", None, QtGui.QApplication.UnicodeUTF8))
class MyItemDelegate(QtGui.QItemDelegate):
def __init__(self, parent = None):
QtGui.QItemDelegate.__init__(self, parent)
def setEditorData(self, editor, index):
print editor
print index
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(window)
print ui.tableWidget.columnCount()
print ui.tableWidget.itemDelegateForColumn(0)
ui.tableWidget.setItemDelegateForColumn(0, MyItemDelegate())
print ui.tableWidget.itemDelegateForColumn(0)
print ui.tableWidget.rowCount()
print ui.tableWidget.itemDelegateForRow(0)
ui.tableWidget.setItemDelegateForRow(0, MyItemDelegate())
print ui.tableWidget.itemDelegateForRow(0)
window.show()
sys.exit(app.exec_())
--
Thanks
Zhaohao Liang
More information about the PyQt
mailing list