class Model : public QAbstaractItemModel{public: int x() const; // Other QAbstaractItemModel's mandatory methods implementation, like data(), etc. // ...};
class ModelWidget : public QWidget{public: ModelWidget(Model& model) : model_(model) { treeView_->setModel(&model); model.x(); }private: Model& model_; QTreeView* treeView_;};
class Xable{public: virtual int x() const = 0;};class Model : public QAbstractItemModel, public Xable{public: // Xable's implementation int x() const {...} // Other QAbstaractItemModel's mandatory methods implementation, like data(), etc. // ...};class SortFilterProxyModel : public QSortFilterProxyModel, public Xable{public: // Xable's implementation int x() const { if (!sourceModel()) return 0; // return something suitable or throw an exeption // We assume only Model as possible source model Model* sourceModel = boost::polymorphic_downcast<Model*>(sourceModel()); return sourceModel->x(); } // Other QSortFilterProxyModel's methods // ...};class ModelWidget : public QWidget{public: ModelWidget(QAbstractItemModel& sourceModel) : model_(model) { treeView_->setModel(model); modelAsXable().x(); }private: QAbstractItemModel& model_; QTreeView* treeView_; Xable& modelAsXable() { return dynamic_cast<Xable&>(model_); }};
class ModelWidget : public QWidget{public: ModelWidget(QAbstractItemModel& model) : model_(model) { treeView_->setModel(model); modelAsXable().x(); }private: QAbstractItemModel& model_; QTreeView* treeView_; Xable& modelAsXable() { return dynamic_cast<Xable&>(initialSourceModel(model_)); }};
/// \file/// \brief Initial source model/// \author Akon/// \date 01.07.2009/* ##todo:*/#pragma once#include <QtGui/QAbstractProxyModel>namespace Akon {namespace QtGui {/// Returns initial (first) source model of passed proxy model.inline QAbstractItemModel* initialSourceModel(const QAbstractProxyModel& model){ QAbstractItemModel* result = model.sourceModel(); while (qobject_cast<QAbstractProxyModel*>(result)) result = static_cast<QAbstractProxyModel*>(result)->sourceModel(); return result;}/// If \a model is a proxy model returns initial (first) source model; else returns \a model/// itself as non-const pointer.inline QAbstractItemModel* initialSourceModel(const QAbstractItemModel& model){ return qobject_cast<const QAbstractProxyModel*>(&model) ? initialSourceModel(static_cast<const QAbstractProxyModel&>(model)) : const_cast<QAbstractItemModel*>(&model);}} // QtGui::} // Akon::
template <class BaseT>class SortFilterModelAspect : public <BaseT>{ // All functional of QSortFilterProxyModel is implemented in this class template};
Model model; // source model with x() "non-standard" functionSortFilterModelAspect<Model> proxyModel; // source model with x() "non-standard" function + proxy featuresproxyModel.setFilterRegExp(...);
class ModelWidget : public QWidget{public: ModelWidget(QAbstractItemModel& model, X& x) : model_(model), x_(x) { treeView_->setModel(model); x.x(); }private: QAbstractItemModel& model_; X& x_; QTreeView* treeView_;};