QComboBox实现复选功能
非常好用
参考 教你如何实现带复选框的ComboBox(自定义QComboBox)
Qt实现复选下拉框 C++
//相机位置
QStringList Types;
Types << QString(QStringLiteral("正视")) << QString(QStringLiteral("侧视"));
m_camPosCListWidget = new QListWidget(this);
QLineEdit* camPosCLineEdit = new QLineEdit(this);
camPosCLineEdit->setReadOnly(true);
for (auto& it : Types)
{
QListWidgetItem* pItem = new QListWidgetItem(m_camPosCListWidget);
m_camPosCListWidget->addItem(pItem);
QCheckBox* pCheckBox = new QCheckBox(this);
pCheckBox->setText(it);
m_camPosCListWidget->addItem(pItem);
m_camPosCListWidget->setItemWidget(pItem, pCheckBox);
connect(pCheckBox, SIGNAL(stateChanged(int)), this, SLOT(slotCamPosCBoxstateChanged(int)));
}
ui.camPosCBox->setModel(m_camPosCListWidget->model());
ui.camPosCBox->setView(m_camPosCListWidget);
ui.camPosCBox->setLineEdit( camPosCLineEdit);
void AddDatasetDialog::slotCamPosCBoxstateChanged(int index)
{
ui.camPosCBox->lineEdit()->clear();
QString text;
for (int row = 0, rows = m_camPosCListWidget->count(); row < rows; ++row)
{
QListWidgetItem* item = m_camPosCListWidget->item(row);
QWidget* pWidget = m_camPosCListWidget->itemWidget(item);
QCheckBox* pCheckBox = (QCheckBox*)pWidget;
if (pCheckBox-> isChecked())
text.append(pCheckBox->text() + ";");
}
ui.camPosCBox->lineEdit()->setText(text);
}