QComboBox hidePopup()函数踩坑

问题复现

需要做一个 ComboBox 下拉后显示列表的每个 item ,每一个item都有一个复选框,用户点击后。在lineedit上面显示对应item右边的文字。然后在下拉框收起的时候主动发送一个信号。问题就在下拉框收起的时候主动发送一个信号,做法是继承QCombBox然后重写void QComboBox::hidePopup()。在里面发送一个自定义的信号。

代码

#include "MultiComboBox.h"
#include <QDebug>

MultiComboBox::MultiComboBox(QWidget *parent) : QComboBox(parent)
{

}

void MultiComboBox::hidePopup()
{
    QComboBox::hidePopup();
    qDebug() << QStringLiteral("调用 hidePopup");
    emit finish();
}

[virtual] void QComboBox::hidePopup()

Hides the list of items in the combobox if it is currently visible and resets the internal state, so that if the custom pop-up
 was shown inside the reimplemented showPopup(), then you also need to reimplement the hidePopup() function to hide 
 your custom pop-up and call the base class implementation to reset the internal state whenever your custom pop-up 
 widget is hidden.

翻译

如果组合框中的项目列表当前可见,则隐藏该列表并重置内部状态,因此,如果自定义弹出框显示在重新实现的
showPopup()中,则还需要重新实现hidePopup()函数来隐藏自定义弹出框,并在自定义弹出窗口小部件隐藏时调用基类实
现来重置内部状态

。看不懂。**hidePopup()**的调用时机除了用户正常操作ComboBox。隐藏下拉框如:显示下拉框的情况下鼠标点击其它位置。或者显示下拉框的情况下键盘点击确认键。使下拉框隐藏或者其它操作也会导致调用hidePopup()。如下
在这里插入图片描述
想要只在用户正常操作(如鼠标点击,确认键按下)下发送信号代码如下

void MultiComboBox::hidePopup()
{
    QComboBox::hidePopup();
    if(m_UserOperator)
    {
        qDebug() << QStringLiteral("调用 hidePopup");
        emit finish();
        m_UserOperator = false;
    }
}

void MultiComboBox::showPopup()
{
    QComboBox::showPopup();
    m_UserOperator = true;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值