python tableview绑定字典_python-QTableView中每个标题的不同工具提示

我对这些东西也很陌生,但是我认为您需要继承QTableView并重新实现headerData函数.这是一个工作示例.希望您可以从中提取所需内容:

from PyQt4 import QtGui, QtCore

import sys

class PaletteListModel(QtCore.QAbstractListModel):

def __init__(self, colors = [], parent = None):

QtCore.QAbstractListModel.__init__(self,parent)

self.__colors = colors

# required method for Model class

def rowCount(self, parent):

return len(self.__colors)

# optional method for Model class

def headerData(self, section, orientation, role):

if role == QtCore.Qt.DisplayRole:

if orientation == QtCore.Qt.Horizontal:

return QtCore.QString("Palette")

else:

return QtCore.QString("Color %1").arg(section)

if role == QtCore.Qt.ToolTipRole:

if orientation == QtCore.Qt.Horizontal:

return QtCore.QString("Horizontal Header %s Tooltip" % str(section))

else:

return QtCore.QString("Vertical Header %s Tooltip" % str(section))

# required method for Model class

def data(self, index, role):

# index contains a QIndexClass object. The object has the following

# methods: row(), column(), parent()

row = index.row()

value = self.__colors[row]

# keep the existing value in the edit box

if role == QtCore.Qt.EditRole:

return self.__colors[row].name()

# add a tooltip

if role == QtCore.Qt.ToolTipRole:

return "Hex code: " + value.name()

if role == QtCore.Qt.DecorationRole:

pixmap = QtGui.QPixmap(26,26)

pixmap.fill(value)

icon = QtGui.QIcon(pixmap)

return icon

if role == QtCore.Qt.DisplayRole:

return value.name()

def setData(self, index, value, role = QtCore.Qt.EditRole):

row = index.row()

if role == QtCore.Qt.EditRole:

color = QtGui.QColor(value)

if color.isValid():

self.__colors[row] = color

self.dataChanged.emit(index, index)

return True

return False

# implment flags() method

def flags(self, index):

return QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable

if __name__ == '__main__':

app = QtGui.QApplication(sys.argv)

app.setStyle("plastique")

data = QtCore.QStringList()

data << "one" << "two" << "three" << "four" << "five"

tableView = QtGui.QTableView()

tableView.show()

red = QtGui.QColor(255,0,0)

green = QtGui.QColor(0,255,0)

blue = QtGui.QColor(0,0,255)

model = PaletteListModel([red, green, blue])

tableView.setModel(model)

sys.exit(app.exec_())

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值