问题描述
我正在尝试使用模型-视图-控制器方案在 Qt 中实现一个应用程序。我有一些模型类(数据库)、一个应用程序控制器和一堆 GUI 类。所有 GUI 类都知道应用程序控制器,并且可以调用其公共插槽向它发送应存储在模型中的数据。应用程序控制器获取数据并调用适当的数据库来存储它。数据库类检查数据并向应用控制器返回一个布尔值以判断数据是否正常。
现在,一个 GUI 元素准备了一些用户数据并希望将其发送到应用控制器。我试图弄清楚如何让 GUI 类知道应用程序控制器使用模型验证传入数据并接受数据。我的应用程序控制器不应该关心也不知道哪些 GUI 元素调用其公共 API 来向模型提交数据。但是,应用控制器如何向 GUI 元素传达响应以告诉它一切正常,因此 GUI 元素可以自行关闭?
我的想法是让 GUI 元素创建一个随机数令牌,将它与数据一起作为一种“回调地址”发送到应用程序控制器。只要我的 GUI 元素收到带有正确标记的 OK 信号,它就可以对此做出反应。 我不知道。我所有的解决方案都感觉很奇怪,而不是 Qt 中应该有的样子。
编辑 1:用于说明的伪代码示例:
void Appcontroller::Slot(userdata)
{
bool okay = database->save(userdata)
};
void Appcontroller::CreateMasterWindow()
{
this->masterwindow_ = new MasterWindow(this);
// I am propagating the knowledge of the app controller to the
// MasterWindow and the latter can propagate that info further
// down,that's fine (everybody can know the responsible
// controller,but I don't think the controller should care about
// all his bastard children)
};
void MasterWindow::CreateSubWindow()
{
this->subwindow_ = new SubWindow(appcontroller_);
// the masterwindow creates a random subwindow that the controller
// should't have to care about
};
void SubWindow::SendUserData(userdata)
{
QObject::connect(this,&SubWindow::SendToControllerSignal,appcontroller_,&Appcontroller::Slot);
emit SendToControllerSignal(userdata);
// how to get a callback (targeted to me/this) from this,// if the appcontroller does not know me? what is de way?
};
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)