
How to make adequate display of horizontal scroll bar in QTreeWidget with one column
Hi, Habr.
In this article, I decided to leave the solution to the problem of incomprehensible behavior of QTreeWidget - the GUI component of the Qt cross-platform framework . The problem, it seems to me, is relevant, because the question is asked in many forums, but the right solution is not given. However, if I am mistaken, the UFO will inform me of this.
Problem
Qt has a QTreeWidget component designed for displaying information in a tree. An element or a node of a tree can be text with a picture, or another widget.
During the operation of this component, some users have a problem when the text of the element or node does not fit in the width of the widget, and the component does not allow the user to scroll the contents left / right so that he can read the whole text. Instead, an ellipse appears at the end of the text. The problem occurs in cases where the widget is used with one column, or with several (then the problem occurs in the last column).
No property of the QTreeWidget class or its ancestors makes it possible to activate automatic resizing of the column viewing area. So, if you also encountered this misunderstanding, please, under cat.
A simple solution
The solution came to my head when I read the documentation for QTreeWidget. The solution is to use the following method (slot):
void QTreeView::resizeColumnToContents(int column);
As you might guess, the method “adjusts” the size of the column, the number of which is passed as a parameter, to the size of the content. That just the component should do it itself, but it doesn’t.
Application Solution
If the problem arises immediately after creating the widget (that is, the user has not had time to do anything yet, and the text is no longer fit), then in the widget constructor, you can start the cycle using this method to all columns (or only to the last):
//применить ко всем колонкам
for(int i = 0; i < treeWidget->columnCount(); ++i)
treeWidget->resizeColumnToContents(i);
//или применить только к последней
treeWidget->resizeColumnToContents(treeWidget->columnCount() - 1);
If you need to instead (or along with this) change the size of the column after expanding or minimizing the node, then you need to use the following events (signals):
void QTreeView::expanded(const QModelIndex &index); //раскрытие узла
void QTreeView::collapsed(const QModelIndex &index); //сворачивание узла
You must either create slots yourself to respond to these signals and connect them, or generate all this in one click on the form (RMB on the widget on the form, "go to the slot", select the slot).
One way or another, event response slots will only contain a call to a method we already know:
void MainWindow::on_tree_widget_expanded(const QModelIndex &index)
{
treeWidget->resizeColumnToContents(index.column());
}
void MainWindow::on_tree_widget_collapsed(const QModelIndex &index)
{
treeWidget->resizeColumnToContents(index.column());
}
Here index.column () tells the method the number of the column in which the event occurred.
Total
Like this crutchin a straightforward way, you can teach the QTreeWidget component to do what it should be able to do. I hope that the solution is useful to someone. I will be glad to answer questions in the comments. Thanks for attention.