题
我想包括在我的QComboBox条目的“删除”图标,但我有麻烦抓鼠标的新闻发布会。我试图抓住它的组合框,我已经试过reimplemting的QIcon类赶上mousepress那里。没有骰子。没有任何人知道如何做到这一点?
-D
解决方案
我写的代码有点像这样,在这里我希望把树视图组合框里面,我需要被点击树上的复选框时采取行动。我落得这样做是安装在组合框中选择一个事件过滤器拦截鼠标点击,揣摩出鼠标点击发生了什么,然后采取行动。也许你可以做这样的事情与你的图标。下面是代码:
bool TreeComboBox::eventFilter(QObject* object, QEvent* event)
{
if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease)
{
QMouseEvent* m = static_cast<QMouseEvent*>(event);
QModelIndex index = view()->indexAt(m->pos());
QRect vrect = view()->visualRect(index);
if(event->type() == QEvent::MouseButtonPress &&
(model()->flags(index) & Qt::ItemIsUserCheckable) &&
vrect.contains(m->pos()))
{
// Your action here
ToggleItem(index);
UpdateSelectionString();
}
if (view()->rect().contains(m->pos()))
skipNextHide = true;
}
return QComboBox::eventFilter(object, event);
}
其他提示
也许你可以与QComboBox::mousePressEvent(QMouseEvent *e)
重新实现e.x()
和使用QComboBox::iconSize()
找到,如果事件发生在图标上。
此意愿关断的原因,如果一个Qt样式决定切换标签和图标位置在组合框。不知道这是可能的?
不隶属于 StackOverflow