标签:else deb plist targe role nec div 分享图片 简单
今天介绍一下一个小东西 — 如何让QComboBox实现复选功能?
pListWidget = new QListWidget(this); pLineEdit = new QLineEdit(this); for (int i = 0; i < 5; ++i) { QListWidgetItem *pItem = new QListWidgetItem(pListWidget); pListWidget->addItem(pItem); pItem->setData(Qt::UserRole, i); QCheckBox *pCheckBox = new QCheckBox(this); pCheckBox->setText(QStringLiteral("Qter%1").arg(i)); pListWidget->addItem(pItem); pListWidget->setItemWidget(pItem, pCheckBox); connect(pCheckBox, SIGNAL(stateChanged(int)), this, SLOT(stateChanged(int))); } ui.comboBox->setModel(pListWidget->model()); ui.comboBox->setView(pListWidget); ui.comboBox->setLineEdit(pLineEdit); pLineEdit->setReadOnly(true); //ui.comboBox->setEditable(true); connect(pLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(textChanged(const QString &)));
void MultiComboBox::stateChanged(int state) { bSelected = true; QString strSelectedData(""); strSelectedText.clear(); QObject *object = QObject::sender(); QCheckBox *pSenderCheckBox = static_cast(object); int nCount = pListWidget->count(); for (int i = 0; i < nCount; ++i) { QListWidgetItem *pItem = pListWidget->item(i); QWidget *pWidget = pListWidget->itemWidget(pItem); QCheckBox *pCheckBox = (QCheckBox *)pWidget; if (pCheckBox->isChecked()) { QString strText = pCheckBox->text(); strSelectedData.append(strText).append(";"); } //所点击的复选框 if (pSenderCheckBox == pCheckBox) { int nData = pItem->data(Qt::UserRole).toInt(); qDebug() << QString("I am sender...id : %1").arg(nData); } } if (strSelectedData.endsWith(";")) strSelectedData.remove(strSelectedData.count() - 1, 1); if (!strSelectedData.isEmpty()) { //ui.comboBox->setEditText(strSelectedData); strSelectedText = strSelectedData; pLineEdit->setText(strSelectedData); pLineEdit->setToolTip(strSelectedData); } else { pLineEdit->clear(); //ui.comboBox->setEditText(""); } bSelected = false; }
void MultiComboBox::textChanged(const QString &text) { if (!bSelected) pLineEdit->setText(strSelectedText); }
QComboBox实现复选功能(三种方法:嵌套QListWidget, 设置QStandardItemModel, 设置Delegate)
标签:else deb plist targe role nec div 分享图片 简单
原文地址:http://www.cnblogs.com/findumars/p/8001390.html