How can I ensure that a horizontal scrollbar and not an ellipse shows up when resizing a column smaller than its content in a QTreeView ?

To ensure that a scrollbar shows up instead of an ellipse when resizing a column smaller than its content in a QTreeView [], then you need to call

  1. setResizeMode(relevantColumn, QHeaderView::ResizeToContents)

on the header of your tree, so that the section is resized to its optimal size. You also need to call

  1. setStretchLastSection(false)

if your tree only contains one column.

See the documentation:

See the following example for a demonstration:

  1. #include <QtGui>
  3. int main(int argc, char **argv)
  4. {
  5.     QApplication app(argc, argv);
  6.     QTreeWidget tree;
  7.     tree.setColumnCount(1);
  8.     tree.header()->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
  9.     tree.header()->setResizeMode(0, QHeaderView::ResizeToContents);
  10.     tree.header()->setStretchLastSection(false);
  11.     QStringList list;  list << "An item in a QTreeWidget with some text";
  12.     QTreeWidgetItem *item = new QTreeWidgetItem(&tree, list);
  14.     return
  15. }

1 comment

February 17, 2015

Picture of Yash Yash

Lab Rat


  1. header()->setSectionResizeMode(0,QHeaderView::ResizeToContents);
  2. header()->setStretchLastSection(false);

Write a comment

Sorry, you must be logged in to post a comment.