I created a simple table with QTreeWidget. When a column's text is too long for the column width, it gets truncated with ellipses. Can I show the full text when the user hovers the mouse over the column?
I have tried the following code, but I do not know how to get if the text is truncated or not. Also, the tooltip may not be reliably appearing.
import sys
from PySide6.QtWidgets import QApplication, QTreeWidget, QTreeWidgetItem
data = {
"Dog": "The dog is a domesticated descendant of the wolf.",
"Cat": "Meow!"}
app = QApplication(sys.argv)
tree = QTreeWidget()
tree.setMouseTracking(True)
tree.setColumnCount(2)
tree.setHeaderLabels(["Animal", "Description"])
tree.setColumnWidth(0, 100)
tree.setColumnWidth(1, 100)
items = []
for key, value in data.items():
item = QTreeWidgetItem([key, value])
items.append(item)
tree.insertTopLevelItems(0, items)
def show_tooltip(item, column):
row = tree.indexOfTopLevelItem(item);
print(f"mouse hover over {row}, {column}");
if column == 1:
isTruncated = True #Somehow get if the current column text is truncated.
if isTruncated:
tree.setToolTip("Show the full column text")
else:
tree.setToolTip("")
tree.itemEntered.connect(show_tooltip)
tree.show()
sys.exit(app.exec())