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 correct solution is not given. However, if I am mistaken, the UFO will inform me about it.
In Qt, there is a QTreeWidget component designed for tree-like display of information. An element or a tree node can be text with a picture, or another widget.
During operation of this component, some users have a problem when the text of an element or node does not fit the width of the widget, and the component does not allow the user to scroll left / right to read the entire text. Instead, a three-dot appears at the end of the text. The problem occurs in cases of using the widget with one column, or several (then the problem occurs in the last column).
No property of the QTreeWidget class or its ancestors makes it possible to activate the automatic resizing of the column's viewport. So, if you are also faced with this misunderstanding, I ask for cat.
The solution came down to my little head when I read the QTreeWidget documentation . The solution is to use the following method (slot):
void QTreeView::resizeColumnToContents(int column);
As you can guess, the method "fits" the size of the column, the number of which is passed as a parameter, to the size of the contents. That just the component should do itself, but does not do.
If the problem occurs immediately after creating the widget (that is, the user hasn’t done anything yet, but the text no longer fits), then in the widget's constructor, you can start a loop using this method on all columns (or only on the last):
// for(int i = 0; i < treeWidget->columnCount(); ++i) treeWidget->resizeColumnToContents(i); // treeWidget->resizeColumnToContents(treeWidget->columnCount() - 1);
If it is necessary instead (or with it) to change the size of the column after opening or folding the node, then the following events (signals) should be used:
void QTreeView::expanded(const QModelIndex &index); // void QTreeView::collapsed(const QModelIndex &index); //
You must either create your own slots to respond to these signals and connect them, or generate it all in one click on the form (right click on the form widget, go to slot, select the slot).
One way or another, event response slots will contain only a call to a method already familiar to us:
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.
Here it is a crutch in a straightforward way, you can teach a QTreeWidget component to do what it should and can do. I hope that the decision will be useful to someone. I will be glad to answer questions in the comments. Thanks for attention.
Source: https://habr.com/ru/post/350014/
All Articles