QComboboox with treeview and custom event handler
-
this is what I have so far
@#ifndef ATPTREECOMBOBOX_H
#define ATPTREECOMBOBOX_H#include "atpTreeComboBoxView.h"
#include <QApplication>
#include <QtGui>
#include <QTreeView>
#include <QHeaderView>
#include <QComboBox>class atpTreeComboBox : public QComboBox {
public:
atpTreeComboBox(QWidget* parent = 0) : QComboBox(parent), skipNextHide(false) {
//atpTreeComboBoxView *atpTreeView = new atpTreeComboBoxView(this);
setView(atpTreeView);
view()->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() {
setMaxVisibleItems(10);
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;
};#endif // ATPTREECOMBOBOX_H
@but I wish to be able to add the view not in the class because when I add a model to it it will not hide the columns
so i have a QStandardItemModel with 3 columns and i wish to display only one.
on the qtreeview is displaying corecly
but when i set the same view to my atpCombobox it will not display corecly and also it won't take in account the event handler.I wish to be able to send the view as an argument and to keep event filter and show and hide popup working
even if i set a new view (so without set it up in the class - the class to receive the view as an argument)
@
QStandardItemModel *m = new QStandardItemModel(this);
m->setColumnCount(3);
m->setHeaderData(0, Qt::Horizontal,"Name");
m->setHeaderData(1, Qt::Horizontal,"ID");
m->setHeaderData(2, Qt::Horizontal,"Parent");
QtreeView comboView = new QTreeView(this);mycombo = new atpTreeComboBox(this, comboView);
mycombo->setModel(m);
mycombo->setView(comboView );
mycombo->setGeometry(90,100,330,22);
mycombo->show();
mycombo->setMaxVisibleItems(10);
@any help will apreciate a lot