PDA

View Full Version : HeightForWidth always -1 QListViw



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.

wysota
8th May 2011, 22:44
Not to spoil your fun but the proper way of doing what you are trying to obtain is implementing a custom view, possibly based on QTreeView/QTreeWidget. With your current solution you'll spend 90% of the time working around "problems" such as this one.

migel
9th May 2011, 09:12
I knew it, it gotta be a way to do it simply, but could not find any example and I come up that. This is QTreeWidget but the child is QListView with widgets and what U mean bu custom view, you mean custom view for a childs ? to display it next to each other instead of line by line ? Please can U give me any example how can I do it ?? its just drive me creazy I spent 3 days to figure the proper way to it.

Thanks You

wysota
9th May 2011, 13:06
what U mean bu custom view, you mean custom view for a childs ?
I mean a subclass of QAbstractItemView or one of its subclasses to implement the view as a whole and not to compose it of multiple widgets.


its just drive me creazy I spent 3 days to figure the proper way to it.
You are drifting the wrong way.

migel
9th May 2011, 15:42
how do I do it ?

wysota
9th May 2011, 15:54
First you think what you want to achieve and then you implement the virtual methods from QAbstractItemView to realize your goal. There is no easy receipe for what you want to do. It's not that much work though. Once I imlemented something close to what you want now.

migel
9th May 2011, 16:48
confused the above !

How the hell I implement QAbstractItemView to ui->treeView ???????????????? examples, examples, examples please I know its possible but no idea how to do it, at least please share with me how to start .


Once I imlemented something close to what you want now.

so Have you implemented what I want ?? can you share the code ?

wysota
9th May 2011, 17:08
How the hell I implement QAbstractItemView to ui->treeView ????????????????
QTreeView inherits QAbstractItemView. Basically the most important method when implementing a view is QAbstractItemView::visualRect() and also its counterpart -- QAbstractItemView::indexAt(). Start with those.


examples, examples, examples please I know its possible but no idea how to do it, at least please share with me how to start .
I can't show you any magic 5 lines of code that do anything practical. You can have a look at the chart example to have something to start with.


so Have you implemented what I want ??
Probably not exactly what you want but something very similar.

can you share the code ?
No, sorry...

migel
9th May 2011, 17:45
well, thanks but you are not helping

wysota
9th May 2011, 19:31
well, thanks but you are not helping

My hourly rate is about 80 euro. You can hire me to do your job for you. I'm certainly not going to spend a couple of hours of my free time to give you a COTS product just like that as an "example". You have been told what you have to do. If you don't feel up to the task of doing it yourself then hire someone to do it for you or find a different job/hobby.