Qt4-preview-feedback Archive, February 2008
QComboBox and QTreeViews
Message 1 in thread
When trying to throw a tree model (e.g. QDirModel) at a QComboBox, it just
displays the top level instead of using a QTreeView dropdown.
Using QComboBox::setView to force a QTreeView on it is tricky because the
event handling closes the dropdown when clicking on a +, and getting an event
filter for this type of thing right is fairly tricky - any chance to get a
QComboBox to handle a QTreeView correctly (or to get a QTreeComboBox or
something like that) in 4.4?
Best regards,
bero
To unsubscribe - send "unsubscribe" in the subject to qt4-preview-feedback-request@xxxxxxxxxxxxx
Message 2 in thread
On Wednesday 13 February 2008 11:08:11 Bernhard Rosenkränzer wrote:
> When trying to throw a tree model (e.g. QDirModel) at a QComboBox, it just
> displays the top level instead of using a QTreeView dropdown.
FWIW I've attached the class I'm currently using to get this functionality -
feel free to use it.
#include "TreeComboBox.moc"
#include "TreeComboBox.moc"
#include <QMouseEvent>
#include <QTreeView>
#include <QHeaderView>
TreeComboBox::TreeComboBox(QWidget *parent):QComboBox(parent),_ignoreHide(false) {
QTreeView *v=new QTreeView(this);
v->header()->hide();
setView(v);
view()->viewport()->installEventFilter(this);
}
bool TreeComboBox::eventFilter(QObject *object, QEvent *event) {
if(event->type() == QEvent::MouseButtonPress && object==view()->viewport()) {
QMouseEvent const *me=static_cast<QMouseEvent*>(event);
if(!view()->visualRect(view()->indexAt(me->pos())).contains(me->pos()))
_ignoreHide=true;
}
return false;
}
void TreeComboBox::showPopup()
{
setRootModelIndex(QModelIndex());
QComboBox::showPopup();
}
void TreeComboBox::hidePopup()
{
if(_ignoreHide)
_ignoreHide=false;
else {
setRootModelIndex(view()->currentIndex().parent());
setCurrentIndex(view()->currentIndex().row());
QComboBox::hidePopup();
}
}
#ifndef _TREECOMBOBOX_H_
#ifndef _TREECOMBOBOX_H_
#define _TREECOMBOBOX_H_ 1
#include <QComboBox>
class TreeComboBox:public QComboBox {
Q_OBJECT
public:
TreeComboBox(QWidget *parent=0);
bool eventFilter(QObject *object, QEvent *event);
void showPopup();
void hidePopup();
protected:
bool _ignoreHide;
};
#endif