使用Qt设计一个带下拉CheckBox的ComboBox控件 您所在的位置:网站首页 下拉界面 使用Qt设计一个带下拉CheckBox的ComboBox控件

使用Qt设计一个带下拉CheckBox的ComboBox控件

2023-11-10 09:49| 来源: 网络整理| 查看: 265

1.背景

项目需要设计一个带下拉选择的ComboBox,这里重写一个QComboBox实现一下

2.思路

查了一下文档发现,选择QComboBox作为模板,设计ComboBox下拉界面的model和view,用checkBox作为item,实现一下点击功能,即可实现。 这里解决了点击CheckBox空白处无法进行选择,滚动条显示的异常,二次打开下拉菜单item项缺失的问题,可以参考一下

3.代码

1.头文件

#include #include #include #include #include #include #include #include #include class CheckComboBox: public QComboBox { Q_OBJECT public: explicit CheckComboBox(QWidget *parent = 0); ~CheckComboBox(); /*重置*/ void resetListWidget(); /*赋值*/ void setDataList(const QList &dataList, const QString &recordText = ""); /*获取combox含有的所有名称*/ QList getLstName(); /*获取combox选中的所有名称*/ QList getSelectLstName(); private slots: void updateCheckData(const int &index); void updateShowText(); protected: virtual void showPopup(); virtual void hidePopup(); private: QLineEdit *m_textShow; QListWidget *m_widgetList; QList m_checkBoxList; QList m_lstName; };

2.功能和实现

#include "checkcombobox.h" CheckComboBox::CheckComboBox(QWidget *parent) : QComboBox(parent) , m_textShow(new QLineEdit(this)) , m_widgetList(new QListWidget(this)) { m_textShow->setText(" "); m_textShow->setReadOnly(true); m_textShow->setStyleSheet("background:#444444;"); setLineEdit(m_textShow); setMinimumWidth(120); setDataList(QList(),QString()); // 获取点击状态 connect(this, static_cast(&QComboBox::activated), [=](int index) { updateCheckData(index); }); } CheckComboBox::~CheckComboBox() { } void CheckComboBox::resetListWidget() { if(m_widgetList != NULL) { delete m_widgetList; m_widgetList = new QListWidget(this); } m_checkBoxList.clear(); } void CheckComboBox::setDataList(const QList &data, const QString &recordText) { m_lstName = data; //清空listwidget resetListWidget(); QStringList itemNameList = recordText.split(";"); for (int i = 0; i checkBox->setChecked(true); } connect(checkBox, SIGNAL(stateChanged(int)), this, SLOT(updateShowText())); } setModel(m_widgetList->model()); setView(m_widgetList); m_textShow->setText(recordText); } QList CheckComboBox::getLstName() { return m_lstName; } QList CheckComboBox::getSelectLstName() { QList sellstName; for (int i = 0; i sellstName.append(m_checkBoxList[i]->text()); } } return sellstName; } void CheckComboBox::updateCheckData(const int &index) { if (index setCurrentIndex(0); } // 取反 if (m_checkBoxList.count() > index) { if (m_checkBoxList[index]->isChecked()) { m_checkBoxList[index]->setChecked(false); } else { m_checkBoxList[index]->setChecked(true); } } } void CheckComboBox::updateShowText() { QList showTextList = getSelectLstName(); QString showText; for (int i = 0; i showText += ";"; } } m_textShow->setText(showText); } void CheckComboBox::showPopup() { QComboBox::showPopup(); } void CheckComboBox::hidePopup() { // 重置一下滚动条,防止出现widgetList的滚动条在界面最底下 QScrollBar *bar = m_widgetList->verticalScrollBar(); bar->setValue(bar->minimum()); QComboBox::hidePopup(); }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有