Model View with Nested Collections and Multiple Views / Widgets



  • Apologies if this is too long a post...

    I'm primarily a .Net developer and have been investigating Qt for a while now. I'm now at the stage of trying to implement the model / view framework in Qt. I think I have a grasp of the basic principles but am unclear of how to hang things together in a more complex UI where widgets need to communicate with each other. Given the following:

    @// 'domain' model classes
    class NestedDomainModel1
    {
    public:
    NestedDomainModel1();

    QString name() const;
    void setName(const QString& newName);

    // other properties

    private:
    QString m_name;
    };

    class NestedDomainModel2
    {
    public:
    NestedDomainModel2();

    QString name() const;
    void setName(const QString& newName);

    // other properties
    };

    class MyDomainModel
    {
    public:
    MyDomainModel();

    void addNestedModel1(const NestedDomainModel1& modelToAdd);
    NestedDomainModel& nestedObjectModel1At(int index);
    int nestedObjectModel1Count() const;

    // repeat for model 2

    private:

    QList<NestedDomainModel1> m_nestedModels1;
    QList<NestedDomainModel2> m_nestedModels2;
    };

    // 'GUI' classes
    class MainWindow : public QMainWindow
    {

    private:
    MyDomainModel* m_model;
    MyTreeViewWidget* m_treeWidget; // -> this sits in a left dock window
    MyInfoDisplayWidget* m_infoWidget; // -> this sits in a right dock window and display details about the item selected in the tree
    };

    class MyDomainModelTreeModel : public QAbstractItemModel
    {
    public:
    explicit MyDomainModelTreeModel(MyDomainModel* model);

    // required overrides for QAbstractItemModel
    private:
    MyDomainModel* m_model;
    };

    class MyTreeViewWidget : public QWidget
    {
    public:
    // Take a pointer to the domain model and create a model for the 'view'.
    // Will create a tree like:
    // Nested Objects 1
    // |- object 001
    // |- object 002
    // |- you get the idea
    // Nested Objects 2
    // |- other object 001
    // |- more of the same
    explicit MyTreeViewWidget(MyDomainModel* model);

    public slots:
    // Used to notify widget when an item is added to the underlying model.
    void nestedModel1Added();
    void nestedModel2Added();

    signals:
    void nestedModel1Selected(NestedDomainModel1& selectedModel);
    void nestedModel2Selected(NestedDomainModel2& selectedModel);

    private slots:
    // connect to tree view event when an item is selected and if all ok, emit one of the selected events
    void onTreeItemSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected);

    private:
    QTreeView* m_treeView;
    MyDomainModelTreeModel* m_treeModel;
    };

    class MyNestedClass1ViewModel : QAbstractItemModel
    {
    public:
    explicit MyNestedClass1ViewModel(NestedDomainModel1* model);

    setModel(NestedDomainModel1* model);

    // required overrides for QAbstractItemModel

    private:
    NestedDomainModel1* m_model
    };

    class MyInfoDisplayWidget : public QWidget
    {
    public:
    explicit MyInfoDisplayWidget(QWidget* parent = 0);

    public slots:
    // this is connected to the 'tree' widget signal in MainWindow
    void setModel(NestedDomainModel1& selectedModel);
    };@

    The basic premise of the UI is something similar in feel to Visual Studio. The tree is similar to the Solution Explorer and the 'info display' is similar to the properties window.

    1. Is this how you use the model / view framework? For those familar with WPF / Silverlight development, is the model / view framework similar to MVVM (at a high level) in that it is the 'model of the view' and wraps / contains the domain model?

    2. Is this how you connect the widgets using the model / view framework (ie. one widget passes a pointer or reference of the model to another)? Or should I be using the SelectionModel? Does that work since the tree model contains different types of objects?

    3. How do you identify the root nodes? For instance, when a MyNestedObject1 is created and needs to be added to tree do I rely on the knowledge that root node is at a model index QModelIndex(0, 0) (ie. row 0 with an invalid parent index)?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.