migel
8th May 2011, 17:33
I created TreeWidget
- node
- child
- node
-child
I implemented each child with QListView to display multiple widgets under -child. The point is that QListView height does not expend together with its content. I figured finally why, its because the layout of QListView returns -1 of the hasHeightForWith function, so the height of the QListView has always the scroll bar. How the heck resize the height base on width and the list content.
I done it in the past resizing the height but on QLabel's not sure what's wrong with QListView.
Please help me out. Thanks
ui->treeWidget->setVerticalScrollMode(QListView::ScrollPerPixel);
for(int i = 0; i <= 2; i++ ) {
QTreeWidgetItem *rootItem = new QTreeWidgetItem();
rootItem->setText(0, "Folder Name");
ui->treeWidget->setColumnCount(1);
ui->treeWidget->insertTopLevelItem(0, rootItem);
QStandardItemModel *filesViewModel = new QStandardItemModel;
QListView *filesView = new QListView;
QHBoxLayout *layout = new QHBoxLayout;
QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
//sizePolicy.setHeightForWidth(filesView->sizePolicy().hasHeightForWidth());
sizePolicy.setHeightForWidth(true);
qDebug() << sizePolicy.hasHeightForWidth();
filesView->setLayoutMode(QListView::SinglePass);
filesView->setLayout(layout);
filesView->setModel(filesViewModel);
filesView->ensurePolished();
filesView->setViewMode(QListView::IconMode);
filesView->setFlow(QListView::LeftToRight);
filesView->setResizeMode(QListView::Adjust);
//filesView->setSizePolicy(QSizePolicy::Preferred,QSizePolicy:: Expanding);
filesView->setSizePolicy(sizePolicy);
filesView->setMovement(QListView::Free);
filesView->setWrapping(true);
//filesView->setMinimumHeight(100);
filesView->setWordWrap(true);
qDebug() << layout->hasHeightForWidth();
qDebug() << layout->heightForWidth(200);
// this should return true
qDebug() << layout->hasHeightForWidth();
for(int j = 0; j <= 20; j++ ) {
QStandardItem *fileItem = new QStandardItem;
FileWidget *fileWidget = new FileWidget;
fileWidget->ui->label->setText(QString::number(j));
fileItem->setSizeHint(fileWidget->size());
filesViewModel->appendRow(fileItem);
filesView->setIndexWidget(fileItem->index(),fileWidget);
filesView->resize(filesView->contentsRect().width(),
filesView->heightForWidth(filesView->contentsRect().width()));
fileItem->setSizeHint(fileWidget->size());
}
QTreeWidgetItem *childItem = new QTreeWidgetItem();
childItem->setText(0, "FILES VIEW HERE");
rootItem->insertChild(0, childItem);
ui->treeWidget->setItemWidget(childItem, 0, filesView);
filesView->resize(filesView->contentsRect().width(),
filesView->heightForWidth(filesView->contentsRect().width()));
childItem->setSizeHint(0,filesView->size());
}
On the screenshot you can see the scrolls on the QListView in the CHILDs
Numbers in the List represents individual QWidgets laid out inline.
- node
- child
- node
-child
I implemented each child with QListView to display multiple widgets under -child. The point is that QListView height does not expend together with its content. I figured finally why, its because the layout of QListView returns -1 of the hasHeightForWith function, so the height of the QListView has always the scroll bar. How the heck resize the height base on width and the list content.
I done it in the past resizing the height but on QLabel's not sure what's wrong with QListView.
Please help me out. Thanks
ui->treeWidget->setVerticalScrollMode(QListView::ScrollPerPixel);
for(int i = 0; i <= 2; i++ ) {
QTreeWidgetItem *rootItem = new QTreeWidgetItem();
rootItem->setText(0, "Folder Name");
ui->treeWidget->setColumnCount(1);
ui->treeWidget->insertTopLevelItem(0, rootItem);
QStandardItemModel *filesViewModel = new QStandardItemModel;
QListView *filesView = new QListView;
QHBoxLayout *layout = new QHBoxLayout;
QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
//sizePolicy.setHeightForWidth(filesView->sizePolicy().hasHeightForWidth());
sizePolicy.setHeightForWidth(true);
qDebug() << sizePolicy.hasHeightForWidth();
filesView->setLayoutMode(QListView::SinglePass);
filesView->setLayout(layout);
filesView->setModel(filesViewModel);
filesView->ensurePolished();
filesView->setViewMode(QListView::IconMode);
filesView->setFlow(QListView::LeftToRight);
filesView->setResizeMode(QListView::Adjust);
//filesView->setSizePolicy(QSizePolicy::Preferred,QSizePolicy:: Expanding);
filesView->setSizePolicy(sizePolicy);
filesView->setMovement(QListView::Free);
filesView->setWrapping(true);
//filesView->setMinimumHeight(100);
filesView->setWordWrap(true);
qDebug() << layout->hasHeightForWidth();
qDebug() << layout->heightForWidth(200);
// this should return true
qDebug() << layout->hasHeightForWidth();
for(int j = 0; j <= 20; j++ ) {
QStandardItem *fileItem = new QStandardItem;
FileWidget *fileWidget = new FileWidget;
fileWidget->ui->label->setText(QString::number(j));
fileItem->setSizeHint(fileWidget->size());
filesViewModel->appendRow(fileItem);
filesView->setIndexWidget(fileItem->index(),fileWidget);
filesView->resize(filesView->contentsRect().width(),
filesView->heightForWidth(filesView->contentsRect().width()));
fileItem->setSizeHint(fileWidget->size());
}
QTreeWidgetItem *childItem = new QTreeWidgetItem();
childItem->setText(0, "FILES VIEW HERE");
rootItem->insertChild(0, childItem);
ui->treeWidget->setItemWidget(childItem, 0, filesView);
filesView->resize(filesView->contentsRect().width(),
filesView->heightForWidth(filesView->contentsRect().width()));
childItem->setSizeHint(0,filesView->size());
}
On the screenshot you can see the scrolls on the QListView in the CHILDs
Numbers in the List represents individual QWidgets laid out inline.