주로 .Net 개발자이며 잠시 동안 Qt를 조사하고 있습니다. 이제는 Qt에서 모델/뷰 프레임 워크를 구현하려는 단계에 있습니다. 기본 원칙을 이해하고 있지만 위젯이 서로 통신해야하는보다 복잡한 UI에서 함께 작업하는 방법이 불분명하다고 생각합니다. 다음을 감안할 때 :중첩 객체 컬렉션이있는 시작 Qt 모델/뷰
// '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);
};
Visual Studio의 기본 기능은 Visual Studio와 비슷합니다. 트리는 솔루션 탐색기와 유사하며 '정보 표시'는 속성 창과 유사합니다.
모델/뷰 프레임 워크를 어떻게 사용합니까? WPF/Silverlight 개발에 익숙한 사람들은 '보기의 모델'이며 도메인 모델을 포함한다는 점에서 모델/뷰 프레임 워크가 MVVM과 유사합니다 (상위 수준).
모델/뷰 프레임 워크를 사용하여 위젯을 연결하는 방법입니다 (즉, 하나의 위젯이 모델의 포인터 또는 참조를 다른 모델로 전달합니다)? 아니면 SelectionModel을 사용해야합니까? 트리 모델에 다른 유형의 객체가 포함되어 있기 때문에이 작업이 가능합니까?
어떻게 루트 노드를 식별합니까? 예를 들어, MyNestedObject1이 생성되어 트리에 추가되어야 할 때, 루트 노드가 모델 인덱스 QModelIndex (0, 0) (즉, 부모 인덱스가 유효하지 않은 행 0)에 있다는 지식에 의존합니까?