QTreeView in QTreeWidgetItem
Unsolved
General and Desktop
-
Hi, guys!
I have QTreeWidget with QTreeWidgetItems.
QTreeWidgetItem should contain QTreeView.It work, but i need to resize QTreeView automaticaly by content.
tree->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents); // do not workvoid MainWindow::addTreeView() { QFileSystemModel *model = new QFileSystemModel(); model->setRootPath(QDir::currentPath()); QTreeView *tree = new QTreeView(); tree->setModel(model); tree->setRootIndex(model->index(QDir::currentPath())); // ?? tree->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents); // not work tree->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); QTreeWidgetItem *item = new QTreeWidgetItem(ui->treeWidget); ui->treeWidget->setItemWidget(item, 0, tree); // ?? ui->treeWidget->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents); ui->treeWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); } MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); addTreeView(); addTreeView(); }
How it can be implemented?
-
@Craetor said in QTreeView in QTreeWidgetItem:
QTreeWidgetItem should contain QTreeView.
I don't understand this: do you mean an item in your tree should contain another tree?!
-
@qwasder85,
how it can be solved? -
I fixed this issue.
- QTreeView return correct sizeHint for AdjustToContents.
- I implemented QStyledItemDelegate for sending sizeHint to QTreeWidgetItem from the QTreeView and force call QTreeWidget updateGeometries.
- Child class of QTreeView call SetData(0, Qt::SizeHintRole, sizeHint()) for QTreeWidgetItem on expanded and collapsed
it work fine, may be it very strange way, but i don't known another)
QSize CSResizeDelegate::sizeHint(const QStyleOptionViewItem&, const QModelIndex& index) const { CSResizableTreeWidgetItem *item = static_cast<CSResizableTreeWidgetItem*>(index.internalPointer()); if(item->treeWidget()) ((CSResizableTreeWidget*)item->treeWidget())->updateGeometriesPublic(); // force call QTreeWidget::updateGeometries return item->getTree()->sizeHint(); // return QTreeView::sizeHint }