Название: Как в QComboBox открыть список в виде развернутого дерева?
Отправлено: Jokerochek от Сентября 11, 2009, 08:58
Добрый день. Возникла такая идея. не подскажете, как проще реализовать?
Название: Re: Как в QComboBox открыть список в виде развернутого дерева?
Отправлено: SABROG от Сентября 11, 2009, 09:01
Что ж ты будешь делать... C++ (Qt) #include <QtGui/QApplication> #include "widget.h" #include<QComboBox> #include<QDirModel> #include<QTreeView> #include<QEvent> #include<QMouseEvent> #include<QModelIndex> #include<QDir> class TreeBox : public QComboBox { public: TreeBox(QWidget* parent = 0) : QComboBox(parent), skipNextHide(false) //Widget creation { setView(new QTreeView(this)); view()->viewport()->installEventFilter(this); QComboBox::resize(200,30); } 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(static_cast<QDirModel*>(model())->index(QDir::rootPath())); 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 a(argc, argv); TreeBox combo; //Class object declaration QDirModel model; combo.setModel(&model); //implemention QDir model combo.show(); return a.exec(); }
Название: Re: Как в QComboBox открыть список в виде развернутого дерева?
Отправлено: Jokerochek от Сентября 11, 2009, 09:16
Спасибо=)
|