#include "CUIComponentILineEdit.h" #include #include #include #include #include #include #include /** * @brief CUIComponentLineEdit::CUIComponentLineEdit 构造函数 * @param conf 配置信息 * @param parent */ CUIComponentLineEdit::CUIComponentLineEdit(CUIConfig *conf, QVector &subCUI, QWidget *parent) : CUIComponentBaseWidget(parent),CUIComponentBase() { this->m_conf = conf; this->initSetting(); this->initUI(subCUI); } /** * @brief CUIComponentLineEdit::getLabelWidth 获取当前组件的label的最小宽度 * @return label的最小宽度 */ qint32 CUIComponentLineEdit::getLabelWidth() { return m_label->minimumSizeHint().width(); } /** * @brief CUIComponentLineEdit::setLabelWidth 设置label的宽度 * @param width 宽度 */ void CUIComponentLineEdit::setLabelWidth(qint32 width) { m_label->setMinimumWidth(width); } /** * @brief CUIComponentLineEdit::initUI 根据配置信息初始化 */ void CUIComponentLineEdit::initUI(QVector &subCUI) { m_layout = new QHBoxLayout(); m_label = new QLabel(m_conf->getPropertyValue("name")); m_label->setFixedWidth(m_labelLength); m_lineEdit = new QLineEdit(this->getValueString()); m_layout->addWidget(m_label); m_layout->addWidget(m_lineEdit); // m_dataType = static_cast(this->m_conf->getPropertyOriginValue("data_type").toInt()); this->setLayout(m_layout); // 设置格式校验 this->setValidator(); this->setInputTips(); connect(m_lineEdit, &QLineEdit::textEdited, this, &CUIComponentLineEdit::onTextChanged); } void CUIComponentLineEdit::setValidator() { qDebug()<setValidator(new QDoubleValidator(m_rangeMin.toDouble(), m_rangeMax.toDouble(), 10)); break; } case CUI_DATA_TYPE_INT: { qDebug()<setValidator( new QIntValidator(m_rangeMin.toInt(), m_rangeMax.toInt())); break; } } } /** * @brief CUIComponentLineEdit::showMsg 显示提示信息 */ void CUIComponentLineEdit::setInputTips() { QString msg = ""; if (m_checkRange) { msg += m_inclusiveMin ? "[" : "("; msg += this->getRangeMinString() + "," + this->getRangeMaxString(); msg += m_inclusiveMax ? "]" : ")"; } if (m_required) { msg += "\t" + tr("(required)"); } m_lineEdit->setPlaceholderText(msg); } /** * @brief CUIComponentLineEdit::checkRange 检查范围,并给出提示 */ void CUIComponentLineEdit::showRangeError() { if (m_checkRange) { QPalette palette = m_lineEdit->palette(); if (this->inRange(this->getQVFrom(m_lineEdit->text()))) { palette.setColor(QPalette::Text, Qt::black); } else { palette.setColor(QPalette::Text, Qt::red); } m_lineEdit->setPalette(palette); } } /** * @brief CUIComponentLineEdit::onTextChanged 槽函数 * * 当文本改变时,根据设置的类型修改LineEdit的内容 * 当文本改变时,如果有范围设置,不在范围内则标红 * * @param text 当前输入后的文本内容 */ void CUIComponentLineEdit::onTextChanged(const QString &text) { this->showRangeError(); if (this->inRange(m_lineEdit->text())) { // switch (m_dataType) { // case CUI_DATA_TYPE_DOUBLE: { // m_value = qvariant_cast(m_lineEdit->text().toDouble()); // break; // } // case CUI_DATA_TYPE_INT: { //// qDebug()<< qvariant_cast(m_lineEdit->text().toInt()); // m_value = qvariant_cast(m_lineEdit->text().toInt()); // break; // } // case CUI_DATA_TYPE_STRING: { // m_value = m_lineEdit->text(); // break; // } // } m_value = this->getQVFrom(m_lineEdit->text()); this->setValueToOrigin(); } }