组合框内的 QIcon

发布于 2024-08-23 23:51:44 字数 118 浏览 7 评论 0原文

我想在 QComboBox 中的条目上包含“删除”图标,但我在捕获鼠标按下事件时遇到问题。我尝试在组合框中捕获它,并且尝试重新实现 QIcon 类以捕获那里的鼠标按下。没有骰子。有人知道该怎么做吗?

-D

I want to include a "remove" icon on entries in my QComboBox, but I am having trouble catching the mouse press event. I've tried to catch it on the combobox, and I've tried reimplemting the QIcon class to catch the mousepress there. No dice. Does anybody know how to do this?

-D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

番薯 2024-08-30 23:51:44

我编写的代码有点像这样,我想将树视图放在组合框中,并且在单击树上的复选框时需要执行操作。我最终做的是在组合框上安装一个事件过滤器来拦截鼠标单击,找出鼠标单击发生的位置,然后采取行动。也许你可以用你的图标做同样的事情。这是代码:

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);
}

I've written code a bit like this, where I wanted to put a tree view inside a combo box and I needed to take an action when the check box on the tree was clicked. What I ended up doing was installing an event filter on the combo box to intercept mouse clicks, figure out where the mouse click was happening, and then take an action. Probably you can do the same kind of thing with your icon. Here is the code:

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);
}
烙印 2024-08-30 23:51:44

也许您可以重新实现 QComboBox::mousePressEvent(QMouseEvent *e) 并使用 ex()QComboBox::iconSize() 一起查找是否事件发生在图标上方。

如果 Qt 样式决定在组合框中切换标签和图标位置,这将导致中断。不知道这样可以吗?

Maybe you can reimplement QComboBox::mousePressEvent(QMouseEvent *e) and use e.x() together with QComboBox::iconSize() to find if the event occurred over the icon.

This will off cause break if a Qt style decides to switch label and icon position in combo boxes. Don't know if that is possible?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文