Cah*_*tcü 5 c++ qt signals-slots
我有qt信号槽系统的问题.
首先,我创建了一个名为System in Singleton模式的类,因此我可以访问我想要的实例.系统有一个信号SelectionChanged.
我有一个列表小部件,我将它的itemSelectionChanged信号连接到我的自定义插槽,称为onSelectionChanged.在onSelectionChanged插槽中,我发出System的SelectionChanged信号.还没有问题.
在我的软件设计中,许多GUI小部件或自定义类可以使用一系列对象,而System的SelectionChanged信号可以由列表小部件以外的小部件发出.
所以我在列表小部件中创建一个名为OnSystemSelectionChanged的插槽,然后将其连接到System的SelectionChanged信号.OnSystemSelectionChangedSlot是这样的.
void MyListWidget::OnSystemSelectionChanged(QObject *sender)
{
if (sender == this) return;
// Then I want to get a list of selected objects and set them as selection of this widget like this:
this->SetSelection(System::Instance()->GetSelectedObjects());
}
Run Code Online (Sandbox Code Playgroud)
但问题是当我开始设置列表小部件的所选项时,它将发出itemSelectionChanged信号并且将调用我的onSelectionChanged插槽.然后插槽将发出System的SelectionChanged信号,然后也将调用OnSystemSelectionChanged.它将通过sender参数停止,但是没有方法可以立即设置列表小部件的所选项目.
我怎么能解决这个问题呢.
我希望我能很好地解释我的问题.提前致谢.
编辑:更正拼写和语法错误.
Rei*_*ica 10
在Qt中有几种方法可以解决这个问题.
对一个基础模型使用多个视图.这会自动处理对多个视图控件的更改传播,您无需执行任何额外操作.您可以使用QDataWidgetMapper将"普通旧"小部件链接到模型中的数据元素.我会说这应该是首选的做事方式.拥有所有UI的基础模型无论如何都是朝着良好软件设计方向迈出的一步.
在数据模型之间传播更改时,请同时实现a DisplayRole和EditRole.视图将使用其中一个角色(例如,EditRole)来名义上修改模型,而您可以使用其他角色(例如,DisplayRole)以编程方式修改模型.您可以dataChanged在自己的插槽中处理来自模型的信号,正确处理角色,并setData使用其他角色调用其他模型.这可以防止循环.
对于非QAbstractItemViews的控件,实现两个信号:一个在任何更改时发出,另一个仅在基于键盘/鼠标输入的更改时发出.这是暴露的接口QAbstractButton,例如:toggled(bool)信号是前者,clicked()后者是后者.然后,您只能连接到基于输入的信号.
您自己的代码必须将编程更改传播到所有相互关联的控件,因为从代码中更改一个控件不会修改其他控件.这应该不是问题,因为设计良好的代码应该从其余代码封装UI控件的实现细节.因此,对话框/窗口类将以不与显示特定属性的控件数相关联的方式公开其属性.
使用禁止信号发射的标志(Bartosz的答案).
在更改期间中断信号/插槽连接(Bartosz的答案).
使用QObject::blockSignals().
我能想到两种可能的解决方案:
void MyListWidget::OnSystemSelectionChanged(QObject *sender)
{
if (sender == this || inhibitSelectionChanged)
return;
this->inhibitSelectionChanged = true;
this->SetSelection(System::Instance()->GetSelectedObjects());
this->inhibitSelectionChanged = false;
}
Run Code Online (Sandbox Code Playgroud)
void MyListWidget::OnSystemSelectionChanged(QObject *sender)
{
if (sender == this)
return;
this->disconnect(SIGNAL(SelectionChanged()));
this->SetSelection(System::Instance()->GetSelectedObjects());
this->connect(
this, SIGNAL(SelectionChanged()),
this, SLOT(OnSystemSelectionChanged(QObject*)));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2124 次 |
| 最近记录: |