本文介绍了带复选框的QComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建带有复选框的QComboBox.如何防止鼠标单击时视图崩溃?我希望能够设置复选框,但是每次单击项目时-QComboBox的下拉列表都会折叠.
I'm creating QComboBox with checkboxes. How I can prevent collapsing of view on mouse clicking? I want to be able to set up checkboxes, but each time I click on item - drop-down of QComboBox is collapsed.
注意:目前,我正在调试Qt源并正在寻找解决方法...
Note: currently I'm debugging Qt sources and looking for workaround...
推荐答案
首先,您需要在组合框视图中安装事件过滤器,即:
First of all you need to install an event filter to the combo box view, i.e.:
combobox->view()->viewport()->installEventFilter(someobj);
您需要过滤组合框视图上发生的所有鼠标释放事件,以防止在单击时关闭它:
than you need to filter all mouse release events that happen on the combo box view to prevent its closing when you click on it:
bool SomeObject::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::MouseButtonRelease) {
int index = view()->currentIndex().row();
if (itemData(index, Qt::CheckStateRole) == Qt::Checked) {
setItemData(index, Qt::Unchecked, Qt::CheckStateRole);
} else {
setItemData(index, Qt::Checked, Qt::CheckStateRole);
}
[..]
return true;
} else {
// Propagate to the parent class.
return QObject::eventFilter(obj, event);
}
}
这篇关于带复选框的QComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!