c++ - 如何在共享模型的多个 Qt Widget 之间进行通信
问题描述
我在主窗口中有一个集合小部件,它们都使用模型上的单个数据,我附上了下面的结构图。目前只有树小部件可以访问模型并且还具有树视图。与其他小部件的通信是通过信号和插槽完成的。在树中进行新选择时,屏幕上的每个其他小部件都会更改以表示新选择的树节点。这可行,但是通过一个小部件向所有其他小部件发送信号非常麻烦。
我想将树使用的模型传递给所有其他小部件,并使其脱离模型在对其进行任何更改时发送的信号。因此,可以通过模型本身完成通信,而不是让所有类相互发送信号。
我已经创建了一个模型并将其传递给树和主窗口类的构造函数中的其他小部件,但我不确定我应该如何从这一点开始,因为我对 MVC 相对不熟悉。树类是此时唯一具有视图的类,我是否还需要将视图添加到其他类,以便能够访问选择某个字段时发出的信号,或者模型是否足够? .
我很感激任何意见,谢谢。
解决方案
选择不是模型的属性,而是视图的属性。多个视图可以附加到单个模型,每个视图显示不同的“项目”。因此,没有开箱即用的方法来表示模型本身的选择变化。
有一个专门的QItemSelectionModel
类,可以准确地处理您需要的内容:跟踪跨多个视图的单个模型的选择。
您可以将数据模型包装在选择模型中,并以此为基础进行交流,例如:
MyMainWindow::MyMainWindow() {
myModel = new MyModel;
treeView = new MyTreeView(myModel);
QItemSelectionModel *selectionModel = new QItemSelectionModel(myModel);
treeView->setSelectionModel(selectionModel);
detailWidget = new MyDetailWidget(this, selectionModel);
}
////////////
MyDetailWidget::MyDetailWidget(QWidget *parent, QItemSelectionModel *selectionModel) {
// React to changed selection
connect(selectionModel, &QItemSelectionModel::currentChanged, this,
[=](auto current, auto /*previous*/) {
updateView(current);
}
);
// React to change in selected item
connect(selectionModel->model(), &QAbstractItemModel::dataChanged, this,
[=](auto topLeft, auto bottomRight, auto /*roles*/) {
QItemSelection sel(topLeft, bottomRight);
if (sel.contains(selectionModel->currentIndex()) {
updateView(selectionModel->currentIndex());
}
}
);
}
void MyDetailWidget::updateView(const QModelIndex &idx) {
// Assume some label on this widget
m_name->setText(idx.data(Qt::DisplayRole).toString());
}
推荐阅读
- python-3.x - 连续函数乘以离散函数时如何使用scipy积分
- python - For Loop 有 2 个变量,但只想枚举其中 1 个
- python - 以特定格式打印键值对
- java - JVM 原生方法栈架构
- sql-server - 迁移到新的 SQL Server 后,在 MS Access 中更新链接表的最佳方法是什么?
- node.js - 如何定义 Typescript 接口,其中一个属性是变量属性名称处的数组值?
- latex - 在 Anki 中使用 LaTeX 时的小字体
- java - 同一租户中多个用户的天蓝色身份验证导致问题。当我以第二个用户身份登录时,第一个用户的数据持续存在
- ios - 如何决定在 iOS 13 上为 userNotificationCenter:openSettingsFor: 打开通知设置的窗口?
- javascript - 如果通过标记设置,为什么内部组件会完全呈现?这是一个错误吗?