PDA

View Full Version : Cut the displayed text in QTreeWidgetItem



Moon
20th March 2013, 13:32
Hi all,

I am trying to avoid a horizontal scrollbar in QTreeWidget by restricting the text of the items to some fixed number of characters (let's say 25). The functionality I need is to restrict the _displayed_ name only, keeping the original name in the case the item text is being edited. Also, the original text can be displayed on mouse hover, but this is simply solvable by QTreeWidgetItem::setToolTip().
Is there any standard way to do something like this?

Thank you.

QStringList sEntryList;
sEntryList << "Some text here which does not need to be displayed fully, making the QTreeWidget horizontally scrollable";
QTreeWidgetItem* const pEntryItem = new QTreeWidgetItem(1, 0, 5, this, sEntryList);
pEntryItem->setFlags(pEntryItem->flags() | Qt::ItemIsDragEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable);
pEntryItem->setToolTip("This text here can be displayed on mouse hover event");

Santosh Reddy
21st March 2013, 09:29
Is there any standard way to do something like this?
No, there is no standard way to do so. You will need to write something for yourself.

Ashkan_s
21st March 2013, 09:40
I am trying to avoid a horizontal scrollbar in QTreeWidget by restricting the text of the items to some fixed number of characters (let's say 25).

You can simply disable horizontal scrollbar. QAbstractScrollArea::setHorizontalScrollBarPolicy( ) (http://qt-project.org/doc/qt-4.8/qabstractscrollarea.html#horizontalScrollBarPolicy-prop)

ChrisW67
21st March 2013, 23:12
Unless my memory is failing, overly long DisplayRole text will be truncated by default if the column is not wide enough. Don't set the view to automatically resize the columns to the content. Then put the desired size on the columns, optionally stretch the a section, and you are done.