Python QTableView中每个标题处的不同工具提示
我可以使用以下命令向所有标题添加单个工具提示Python QTableView中每个标题处的不同工具提示,python,pyqt,tooltip,qtableview,qheaderview,Python,Pyqt,Tooltip,Qtableview,Qheaderview,我可以使用以下命令向所有标题添加单个工具提示 tableview = QTableView() tableview.horizontalHeader().setToolTip("headers") 但是我是否可以向每个标题添加不同的工具提示,即我需要访问包含标题的QWidgets,例如(不工作): QTableWidget(它继承了QTableView)有一个方法horizontalHeaderItem(int),可以用来获取标题项,所以你可以切换到使用它来代替QTableView?我对这东西
tableview = QTableView()
tableview.horizontalHeader().setToolTip("headers")
但是我是否可以向每个标题添加不同的工具提示,即我需要访问包含标题的QWidgets,例如(不工作):
QTableWidget
(它继承了QTableView
)有一个方法horizontalHeaderItem(int)
,可以用来获取标题项,所以你可以切换到使用它来代替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_())
从PyQt4导入QtGui,QtCore
导入系统
类PaletteListModel(QtCore.QAbstractListModel):
定义初始化(self,colors=[],parent=None):
QtCore.QAbstractListModel.\uuuuu init\uuuuuu(self,parent)
自身颜色=颜色
#模型类的必需方法
def行数(自身、父级):
返回透镜(自身颜色)
#模型类的可选方法
def headerData(自身、部门、方向、角色):
如果角色==QtCore.Qt.DisplayRole:
如果方向==QtCore.Qt.Horizontal:
返回QtCore.QString(“调色板”)
其他:
返回QtCore.QString(“颜色%1”).arg(节)
如果角色==QtCore.Qt.ToolTipRole:
如果方向==QtCore.Qt.Horizontal:
返回QtCore.QString(“水平标题%s工具提示”%str(节))
其他:
返回QtCore.QString(“垂直标题%s工具提示”%str(节))
#模型类所需方法
def数据(自身、索引、角色):
#索引包含一个QIndexClass对象。该对象具有以下特性
#方法:行(),列(),父对象()
行=索引。行()
值=自身颜色[行]
#将现有值保留在编辑框中
如果角色==QtCore.Qt.EditRole:
返回self.\u颜色[row].name()
#添加工具提示
如果角色==QtCore.Qt.ToolTipRole:
返回“十六进制代码:”+value.name()
如果角色==QtCore.Qt.decoration角色:
pixmap=QtGui.QPixmap(26,26)
pixmap.fill(值)
icon=QtGui.QIcon(pixmap)
返回图标
如果角色==QtCore.Qt.DisplayRole:
返回值。名称()
def setData(self、index、value、role=QtCore.Qt.EditRole):
行=索引。行()
如果角色==QtCore.Qt.EditRole:
颜色=QtGui.QColor(值)
如果color.isValid():
自身颜色[行]=颜色
self.dataChanged.emit(索引,索引)
返回真值
返回错误
#implement flags()方法
def标志(自、索引):
返回QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
app=QtGui.QApplication(sys.argv)
附件:设置样式(“塑料材质”)
data=QtCore.QStringList()
数据如果使用QTableView,可以通过QStandardItemModel设置工具提示:
QStandardItemModel myModel;
myModel.horizontalHeaderItem(1)->setToolTip("");
就我所见,您不能将QTableWidget与QAbstractTablemodel一起使用。是否有解决方法或其他解决方案
QStandardItemModel myModel;
myModel.horizontalHeaderItem(1)->setToolTip("");