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:
#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();
}
#include <QtGui>
class TreeComboBox : public QComboBox
{
public:
TreeComboBox(QWidget* parent = 0) : QComboBox(parent), skipNextHide(false)
{
QTreeView* v = new QTreeView(this);
setView(v);
v->header()->hide();
v->viewport()->installEventFilter(this);
}
bool eventFilter(QObject* object, QEvent* event)
{
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()
{
setRootModelIndex(QModelIndex());
QComboBox::showPopup();
}
virtual void hidePopup()
{
setRootModelIndex(view()->currentIndex().parent());
setCurrentIndex(view()->currentIndex().row());
if (skipNextHide)
skipNextHide = false;
else
QComboBox::hidePopup();
}
private:
bool skipNextHide;
};
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
TreeComboBox combo;
QStandardItemModel model;
model.appendRow(new QStandardItem("Test1"));
model.item(0)->appendRow(new QStandardItem("Test1.1"));
model.item(0)->appendRow(new QStandardItem("Test1.2"));
model.item(0)->appendRow(new QStandardItem("Test1.3"));
combo.setModel(&model);
combo.show();
app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks