问题复现
需要做一个 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;
}