What is the method to set the text for a QTreeWidget's header?
Asked Answered
R

2

16

I've checked the documentation here and I can't seem to find a method for setting the text of a QTreeWidget's title or header.

Without setting the title QTreeWidget automatically uses the number '1' in my code. An example of what it looks like outputted is below. I'm presuming QTreeWidget has a method for this and I just can't find it.

QTreeWidget example

Revoke answered 30/3, 2012 at 13:23 Comment(0)
B
24

You're looking for setHeaderLabel.

Note that the documentation says it adds a new column, so if your view already has column 0 with text "1", you may instead have to do the following:

if(QTreeWidgetItem* header = treeWidget->headerItem()) {
  header->setText(0, "My Text");
} else {
  treeWidget->setHeaderLabel("My Text");
}
Baryon answered 30/3, 2012 at 13:29 Comment(0)
D
7

Here is an another method to set header texts

QStringList headerLabels;
headerLabels.push_back(tr("text1"));
headerLabels.push_back(tr("text2"));
headerLabels.push_back(tr("text3"));
..
headerLabels.push_back(tr("textN"));

treeWidget->setColumnCount(headerLabels.count());
treeWidget->setHeaderLabels(headerLabels);
Drupelet answered 24/9, 2013 at 7:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.