1

I try to set new font size for header and content of a tree widget.

Problem here is: I can not set font size directly for the content because at the moment I set, the content does not appear yet. So I can just set StyleSheet, so when content appears, it follows the StyleSheet. This is my code:

    int modifier = 2;
    /* Set font size for headers */
    QFont f = ui.treeWidget->headerItem()->font(0);
    if (f.pointSize() == 10)
        f.setPointSize(f.pointSize() - 1);
    else
        f.setPointSize(f.pointSize() - modifier);
    ui.treeWidget->headerItem()->setFont(0, f);
    ui.treeWidget->headerItem()->setFont(1, f);
    ui.treeWidget->headerItem()->setFont(2, f);

    /* Set font size for content */
    ui.treeWidget->setStyleSheet("{font-size: " + QString::number(f.pointSize()) + "}");

This is the result, the header font size is changed but the content is not. Where was I wrong, or do you have any other solutions for that? enter image description here

songvan
  • 369
  • 5
  • 21

2 Answers2

5

The following works for me:

ui->treeWidget->setStyleSheet("QTreeWidget { font-size: " + QString::number(f.pointSize()) + "pt; }");

Note my addition of both QTreeWidget and pt;.

acraig5075
  • 10,588
  • 3
  • 31
  • 50
2

Theoretically you can adjust the content in QTreeWidget::item stylesheet subcontrol, but Qt ignored font-size in this case and I don't know why.

Alternatively you can adjust font size of content area in next way:

ui.treeWidget->setFont(f);
ramzes2
  • 773
  • 5
  • 13
  • thanks for your idea. But this way does not work. I tried this way before already. Because the moment we set font, there is nothing as content of tree widget, so this code has no effect. Therefore, I must use setStyleSheet – songvan Jan 12 '17 at 12:52
  • How you added the content? It works fine for newly added content in my case (I use `addTopLevelItem` method after `setFont` call. – ramzes2 Jan 12 '17 at 12:58
  • I also use `addTopLevelItem`. No matter where I put method `setFont` (before or after `addTopLevelItem`), it still does not work. :( – songvan Jan 12 '17 at 13:15