I am trying to display a table of icons and texts such that each item has text above the icon.
I am currently using QStandardItems with QStandardItemModel and QTableView to display the information, however the text only appears to the right of the icon. I haven't found a way to change where the text displays. I have also tried implementing a QAbstractTableModel and overwriting the data method to return my icon for the Qt.DecorationRole and my text for the Qt.DisplayRole however it also only shows to the right of the icon.
For example:
from PySide.QtGui import *
from PySide.QtCore import *
class CustomTableView(QTableView):
"""Table view of icons and text."""
def __init__(self):
super(CustomTableView, self).__init__()
custom_model = QStandardItemModel()
for v in range(10):
for i in range(10):
new_item = QStandardItem("image.png", str(i))
custom_model.setItem(v, i, new_item)
In this case you must use a delegate:
from PySide import QtCore, QtGui
class StyledItemDelegate(QtGui.QStyledItemDelegate):
def initStyleOption(self, option, index):
super(StyledItemDelegate, self).initStyleOption(option, index)
option.decorationPosition = QtGui.QStyleOptionViewItem.Bottom
option.displayAlignment = QtCore.Qt.AlignHCenter | QtCore.Qt.AlignTop
class CustomTableView(QtGui.QTableView):
"""Table view of icons and text."""
def __init__(self):
super(CustomTableView, self).__init__()
delegate = StyledItemDelegate(self)
self.setItemDelegate(delegate)
self.verticalHeader().setResizeMode(QtGui.QHeaderView.ResizeToContents)
custom_model = QtGui.QStandardItemModel()
for v in range(10):
for i in range(10):
new_item = QtGui.QStandardItem(QtGui.QIcon("image.png"), str(i))
custom_model.setItem(v, i, new_item)
self.setModel(custom_model)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
w = CustomTableView()
w.show()
sys.exit(app.exec_())