×
Namespaces

Variants
Actions
Revision as of 06:04, 17 September 2009 by valderind4 (Talk | contribs)

Implementing QTreeView in QComboBox using Qt- Part 2

From Nokia Developer Wiki
Jump to: navigation, search
{{{width}}}
Article Metadata
Tested with
Devices(s): S60 Emulator
Compatibility
Platform(s): S60 3rd Edition, FP1, FP2
S60 5th Edition
Symbian
Article
Keywords: QComboBox,QTreeView,QDirModel
Created: (01 Aug 2009)
Last edited: valderind4 (17 Sep 2009)

Contents

Introduction

This code demonstrate the QTreeView implemented in QComboBox.Here you can expand the tree view and also select the file you want,no restriction of QComboBox closing on one click,you can select any items until your confirm with that.

Name of classed used:

QTreeView-Provides a default model/view implementation of a tree view.

QDirModel-Provides a data model for the local file system.

Preconditions

Source Code

#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();
}

Screenshot

Treecombo.JPG

Related Links

Implementing QTreeView in QComboBox-I

176 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×