#include "nmWxSelectWellsWidget.h" #include #include #include #include #include #include #include #include #include "nmData/nmDataLogFile.h" nmWxSelectWellsWidget::nmWxSelectWellsWidget(QWidget* aParent) : QWidget(aParent), m_pListWidget(new QListWidget(this)), m_pLineEdit(new QLineEdit(this)) { QVBoxLayout* layout = new QVBoxLayout; layout->addWidget(m_pLineEdit); layout->addWidget(m_pListWidget); this->setLayout(layout); } void nmWxSelectWellsWidget::addItems(QStringList items, QList values) { if (m_items.count() > values.count()) { return; } for (int i = 0; i < items.count(); i++) { this->addItem(items[i], values[i]); } } void nmWxSelectWellsWidget::addItem(QString name, QVariant value) { m_items.append(name); m_values.append(value); nmWxSelectWellItemWidget* itemWidget = new nmWxSelectWellItemWidget(NULL, name, value); itemWidget->setFixedHeight(25); QListWidgetItem* item = new QListWidgetItem(m_pListWidget); item->setSizeHint(QSize(item->sizeHint().width(), 25)); m_pListWidget->addItem(item); m_pListWidget->setItemWidget(item, itemWidget); connect(itemWidget, SIGNAL(sigItemClicked()), this, SLOT(onUpdateSelections())); } void nmWxSelectWellsWidget::onUpdateSelections() { QStringList nameList; // 遍历所有的itemWidget,获取所有被选中的信息 for (int i = 0; i < m_pListWidget->count(); i++) { nmWxSelectWellItemWidget* itemWidget = static_cast(m_pListWidget->itemWidget(m_pListWidget->item(i))); if (itemWidget->getSelected()) { nameList.append(m_items[i]); } } m_pLineEdit->setText(nameList.join(",")); } nmWxSelectWellItemWidget::nmWxSelectWellItemWidget() { } nmWxSelectWellItemWidget::nmWxSelectWellItemWidget(QWidget *parent, QString name, QVariant value) { m_sName = name; m_iValue = value; m_pCBox = NULL; this->initUI(); } void nmWxSelectWellItemWidget::setSelected(bool selected) { if (m_pCBox) { m_pCBox->setChecked(selected); } } bool nmWxSelectWellItemWidget::getSelected() { if (m_pCBox) { return m_pCBox->isChecked(); } return false; } void nmWxSelectWellItemWidget::toggleSelected() { this->setSelected(!this->getSelected()); } void nmWxSelectWellItemWidget::initUI() { // 可以设置鼠标样式,表明这个部件可以被点击 setCursor(Qt::PointingHandCursor); // 设置QCheckBox QHBoxLayout* layout = new QHBoxLayout; setLayout(layout); m_pCBox = new QCheckBox(this); m_pCBox->setText(m_sName); layout->setAlignment(Qt::AlignVCenter); } void nmWxSelectWellItemWidget::mousePressEvent(QMouseEvent *event) { // 当鼠标左键被按下时,执行一些操作 if (event->button() == Qt::LeftButton) { // 处理点击事件,例如发出一个信号或者调用一个槽函数 qDebug() << "ClickableWidget was clicked!"; } QWidget::mousePressEvent(event); // 调用基类的处理函数 this->toggleSelected(); emit this->sigItemClicked(); }