Vika
Vika

Reputation: 153

QTreeWidget - width of column with widget

I have QTreeWidget with two columns. The first column is the text, and the second is QPushButton. I can not specify the size of buttons and the size of the second column. When you try to set the size of the column of content, the second column disappears. How to change the width of the second column?

tree_widget_->setColumnCount(2);
tree_widget_->header()->resizeSection(1, 10);
tree_widget_->header()->setStretchLastSection(false);
tree_widget_->header()->setResizeMode(0,QHeaderView::Stretch);
tree_widget_->topLevelItem(4)->addChild( wiop = new QTreeWidgetItem(QStringList() << QString( "Расстояние: %1 км" ).arg( range ) ) );
tree_widget_->setItemWidget(tree_widget_->topLevelItem(4)->child(0),1,range_plot_button_ = new QPushButton("График",tree_widget_));
range_plot_button_->resize(10,10);

Upvotes: 2

Views: 11821

Answers (2)

Vika
Vika

Reputation: 153

I try setColumnWidth, but it is not work. Later I try tree_widget_->header()->setResizeMode(1,QHeaderView::ResizeToContents); and it is work!

Upvotes: 3

John Paine
John Paine

Reputation: 63

tree_widget_->setColumnWidth(1, 10) should do what you wanted. When you assign QPushButton to QTreeWidget as a top level item the QTreeWidget object usualy assigned as a parent of this button, so all geometry of that button is handled by it's parent. Take a look at docs.

If you want your push button to have it's own geometry with layouts (within column) you can make blank widget, set it to TreeWidget as an item and make your push button to be a child of that blank widget with all conveniences of layouts in Qt.

Upvotes: 5

Related Questions