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 [doc.qt.nokia.com], 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:

http://qt.nokia.com/doc/qheaderview.html#ResizeMode-enum
http://qt.nokia.com/doc/qheaderview.html#stretchLastSection-prop

See the following example for a demonstration:

  1. #include <QtGui>
  2.  
  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);
  13.     tree.show();
  14.     return
  15. }

No comments

Write a comment

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