Beispiel #1
0
def table_test():
	"""
	Messing around with QTableWidgets

	This successfully creates a table of DateTimes and allows you to edit them.
	"""

	from PyQt4.QtGui import QTableWidget, QTableWidgetItem, QItemEditorCreatorBase, QDateTimeEdit, QItemEditorFactory, QStyledItemDelegate
	from PyQt4.QtCore import QVariant, QDateTime, QObject

	class DateTimeEditorCreator(QItemEditorCreatorBase):
		"""
		See gui/itemviews/qitemeditorfactory.cpp for implementations of
		createEditor() and valuePropertyName()
		"""
		def __init__(self):
			QItemEditorCreatorBase.__init__(self)

		def createWidget(self, parent):
			wid = QDateTimeEdit(parent)
			wid.setCalendarPopup(True)
			wid.setFrame(False)
			return wid

		def valuePropertyName(self):
			return "dateTime"

	def die():
		raise MyError("Oops")

	class MyError(Exception):
		def __init__(self, value):
			self.value = value
		def __str__(self):
			return repr(self.value)

	tableWidget = QTableWidget(12, 3)
	tableWidget.setItemDelegate(QStyledItemDelegate())
	tableWidget.itemDelegate().setItemEditorFactory(QItemEditorFactory())
	tableWidget.itemDelegate().itemEditorFactory().registerEditor(QVariant.DateTime, DateTimeEditorCreator())
	for row in range(10):
		for col in range(3):
			date = QDateTime.currentDateTime()
			string = str(row) + ',' + str(col)
			item = QTableWidgetItem()
			item.setData(Qt.DisplayRole, QVariant(date))
			tableWidget.setItem(row, col, item)
	tableWidget.show()
	sys.exit(app.exec_())