#include "CUIComponentComboBox.h" #include #include #include #include #include #include /** * @brief CUIComponentComboBox::CUIComponentComboBox 构造函数 * @param conf 配置信息 * @param parent */ CUIComponentComboBox::CUIComponentComboBox(CUIConfig *conf, QVector &subCUI, QWidget *parent) : CUIComponentBaseWidget(parent) { this->m_conf = conf; initUI(subCUI); } /** * @brief CUIComponentComboBox::getLabelWidth 获取combobox的label的最小宽度 * @return label的最小宽度 */ qint32 CUIComponentComboBox::getLabelWidth() { return m_label->minimumSizeHint().width(); } /** * @brief CUIComponentComboBox::setLabelWidth 设置label的宽度 * @param width 宽度 */ void CUIComponentComboBox::setLabelWidth(qint32 width) { m_label->setMinimumWidth(width); } /** * @brief CUIComponentComboBox::initUI 初始化widget,添加label和comboBox */ void CUIComponentComboBox::initUI(QVector &subCUI) { m_label = new QLabel(m_conf->getPropertyValue("name")); m_label->setFixedWidth(m_labelLength); m_comboBox = new QComboBox; m_layout = new QHBoxLayout; this->setLayout(m_layout); m_layout->addWidget(m_label); m_layout->addWidget(m_comboBox); ((QHBoxLayout *)m_layout)->setStretchFactor(m_comboBox, 1); iniComoboBox(); connect(m_comboBox, QOverload::of(&QComboBox::activated), [=](int idx) { this->m_conf->setValue(idx); }); } /** * @brief CUIComponentComboBox::iniComoboBox 初始化ComboBox */ void CUIComponentComboBox::iniComoboBox() { for (auto cui : m_conf->getSub()) { QString type = cui->getPropertyValue("type"); if (type != "Item") { return; } CUIPropertyWidget *temp = new CUIPropertyWidget(nullptr, cui); m_items.push_back(temp); if (temp->getProperty("data_type") == "int") { m_comboBox->addItem(temp->getProperty("name"), temp->getProperty("data").toInt()); } else if (temp->getProperty("data_type") == "double") { m_comboBox->addItem(temp->getProperty("name"), temp->getProperty("data").toDouble()); } else { m_comboBox->addItem(temp->getProperty("name"), temp->getProperty("data")); } } QVariant vOrigin = m_conf->getPropertyOriginValue("value_origin"); int *ptr = qvariant_cast(vOrigin); if (ptr != nullptr) { m_comboBox->setCurrentIndex(*ptr); } }