Different tooltips at each header in QTableView
Asked Answered
R

4

5

I can add a single tooltip to all headers using

tableview = QTableView()
tableview.horizontalHeader().setToolTip("headers")

but can I add different tooltips to each header, i.e. I need to access the QWidgets that contains the headers, e.g. (not working):

tableview.horizontalHeader().Item[0].setToolTip("header 0")
Renteria answered 14/6, 2012 at 6:49 Comment(0)
P
6

I'm pretty new to this stuff too, but I think you'll need to subclass QTableView and reimplement the headerData function. Here is a working example. Hopefully you can extract what you need from it:

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_())
Parkinson answered 19/11, 2012 at 19:51 Comment(0)
S
2

Here's what worked for me:

    headerView = self._table.horizontalHeader()
    for i in range(headerView.count()):
        key = headerView.model().headerData(i, QtCore.Qt.Horizontal)
        toolTip = myDictOfToolTips.get(key, None)
        self._table.horizontalHeaderItem(i).setToolTip(toolTip)
Scumble answered 25/1, 2023 at 4:45 Comment(2)
Code without any explanation are rarely helpful. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please edit your question and explain how it answers the specific question being asked.Dudek
Wow! Thanks for the feedback, I'll make sure to make my next contribution as vrebose as possibleScumble
M
0

QTableWidget (which inherits QTableView) has a method horizontalHeaderItem(int) which can be used to get the header items, so you maybe could swich to use that instead of QTableView?

Mayflower answered 14/6, 2012 at 7:43 Comment(1)
As far as I can see, you cannot use QTableWidget with a QAbstractTablemodel. Is there a workaround or another solutionRenteria
B
0

If you use QTableView, you can set tooltip by QStandardItemModel:

QStandardItemModel myModel;
myModel.horizontalHeaderItem(1)->setToolTip("");
Bodine answered 20/8, 2018 at 10:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.