TreeView as view for ComboBox
Hello,
is this supported?
Code:
int main(int argc, char **argv)
{
view->setModel(model);
combo->setView(view);
combo->setModel(model);
combo->show();
app.exec();
}
If so - what do I make wrong?
(The view isn't rendered correctly - only 10px height, selecting a file doesn't work)
thanks,
niko
1 Attachment(s)
Re: TreeView as view for ComboBox
Here's something you can start with. ;)
1 Attachment(s)
Re: TreeView as view for ComboBox
I'm getting the same result as before; see attached screenshot.
Does it work better for you?
(Qt version 4.3.3)
Re: TreeView as view for ComboBox
What if you change in showPopup():
to
Code:
setRootModelIndex
(static_cast<QDirModel
*>
(model
())->index
(QDir::rootPath()));
?
Re: TreeView as view for ComboBox
great! that works now!!
Why did you add the eventFilter and the skipNextHide? I disabled it and it still worked for me.
thanks,
niko
Re: TreeView as view for ComboBox
Quote:
Originally Posted by
niko
Why did you add the eventFilter and the skipNextHide? I disabled it and it still worked for me.
Otherwise you can't expand/collapse branches without the popup view getting hid...
Re: TreeView as view for ComboBox
aah, i see :D
many thanks!!
Re: TreeView as view for ComboBox
I have to bring this topic up again, because the solution is not really working for me.
The problem is that QComboBox calculates the height of the popup by counting the root-items.
Jpn suggested using another root, so that there are more items. But in my case this doesn't work - as it can happen that there is only one item.
My modified testcase:
Code:
#include <QtGui>
{
public:
{
setView(v);
v->header()->hide();
v->viewport()->installEventFilter(this);
}
{
if (event
->type
() == QEvent::MouseButtonPress && object
== view
()->viewport
()) {
QMouseEvent* mouseEvent
= static_cast<QMouseEvent
*>
(event
);
QModelIndex index
= view
()->indexAt
(mouseEvent
->pos
());
if (!view()->visualRect(index).contains(mouseEvent->pos()))
skipNextHide = true;
}
return false;
}
virtual void showPopup()
{
}
virtual void hidePopup()
{
setRootModelIndex(view()->currentIndex().parent());
setCurrentIndex(view()->currentIndex().row());
if (skipNextHide)
skipNextHide = false;
else
}
private:
bool skipNextHide;
};
int main(int argc, char* argv[])
{
TreeComboBox combo;
combo.setModel(&model);
combo.show();
app.exec();
}
Re: TreeView as view for ComboBox
I can't think of any better solution than faking the amount of top level items:
Code:
{
public:
int rowCount
(const QModelIndex
& index
= QModelIndex()) const {
return !index.isValid() ? qMax(10, count) : count; // at least 10 when top-level
}
};
This will, however, give false branches whilst expanding the tree. I guess one could get rid of them with a hackish reimplementation of QTreeView::drawBranches().
1 Attachment(s)
Re: TreeView as view for ComboBox
That dind't help - because for those non-existing items the TreeView returned a height of 0 in the visualRect function.
My solution was to re-implement visualRect for the TreeView - and modify the height of the last top-level row.
see attached source.
However I still have a problem:
steps to reproduce (with attached application):
* open popup
* select 1.2
* open popup again
* close branch
* Test 1 gets the new root?!
Re: TreeView as view for ComboBox
Setting minimum size helped me avoiding the height problem, although it's not perfect:
Code:
comboBox->view()->setMinimumHeight(comboBox->count()*10);
Regards,
Oscar
Re: TreeView as view for ComboBox
Quote:
Originally Posted by
niko
However I still have a problem:
steps to reproduce (with attached application):
* open popup
* select 1.2
* open popup again
* close branch
* Test 1 gets the new root?!
Does anyone know how to solve this?
removing lines
setRootModelIndex(view()->currentIndex().parent());
setCurrentIndex(view()->currentIndex().row());
will solve this problem partially.
If ComboBox is non-editable it work correct, but if it is editable bug comes back.