// ***************************************************************************** // 版权所有(C)2023~2099 上海骄成超声波技术有限公司 // 保留所有权利 // ***************************************************************************** // 作者 : 杨坚 // 版本 : 1.0 // 功能说明: // 自定义lab // ***************************************************************************** #ifndef __CUSTOMLABELCONTROLS_H__ #define __CUSTOMLABELCONTROLS_H__ 1 #include "TreeViewManagerHead.h" #include "Src/common/JLogAllOutput.h" #include class IoStateLab : public QLabel { Q_OBJECT public: IoStateLab(QWidget* parent = nullptr) : QLabel(parent) { setFixedSize(30, 30); } public: /**设置io 轴名称 */ void setIoName(const QString& strName) { m_strName = strName; UpDataIoStatus(true); } void UpDataIoStatus(bool isInit) { if (m_pCamera == nullptr) { m_pCamera = ns_module::CViewInterface::GetInstance(); if (m_pCamera == nullptr) { // 为了调试准备的 JLogAllOutput::cmd_error("io 轴设置失败"); return; } } if (isInit) { // 信号量 ns_db::DIGIT_IO_LEVEL nOutput; m_pCamera->GetViewMotion()->GetIoStatus(m_strName.toStdString(), nOutput); m_bClicked = nOutput; } else { m_pCamera->GetViewMotion()->SetIoStatus(m_strName.toStdString(), m_bClicked); } } protected: void paintEvent(QPaintEvent* event) override { QLabel::paintEvent(event); QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); int radius = qMin(width(), height()) / 2 - 5; int x = (width() - 2 * radius) / 2; int y = (height() - 2 * radius) / 2; painter.setPen(Qt::NoPen); painter.setBrush(m_bClicked ? Qt::green : Qt::red); painter.drawEllipse(x, y, 2 * radius, 2 * radius); } void mousePressEvent(QMouseEvent* event) override { if (event->button() == Qt::LeftButton) { m_bClicked = !m_bClicked; UpDataIoStatus(false); update(); } QLabel::mousePressEvent(event); } private: bool m_bClicked; // 当前状态 QString m_strName; ns_module::CViewInterface* m_pCamera = nullptr; }; // 自适应控件 class AutoResizeLabel : public QLabel { public: explicit AutoResizeLabel(QWidget* parent = nullptr) : QLabel(parent) { setWordWrap(true); } protected: void resizeEvent(QResizeEvent* event) override { adjustFontSize(); QLabel::resizeEvent(event); } private: void adjustFontSize() { int labelWidth = width(); int labelHeight = height(); QString text = this->text(); if (text.isEmpty()) { return; } QFont font = this->font(); int fontSize = font.pointSize(); // 简单算法:逐步减小字体,直到文字适合标签 while (fontSize > 1) { font.setPointSize(fontSize); this->setFont(font); // 测量文本的像素大小 QFontMetrics fm(font); QRect rect = fm.boundingRect(this->rect(), Qt::TextWordWrap, text); if (rect.width() <= labelWidth && rect.height() <= labelHeight) { break; } fontSize--; } // 设置调整后的字体 font.setPointSize(fontSize); this->setFont(font); } }; // 定义一个结构体来表示你的数据项 struct ST_DATA_ITEM { QString key; QString value; // "1" 表示选中, "2" 表示未选中 }; class JCheckBoxCont : public QWidget { Q_OBJECT public: JCheckBoxCont(QList& data, QWidget* parent = nullptr) : QWidget(parent), m_data(data) { QHBoxLayout* layout = new QHBoxLayout(this); for (ST_DATA_ITEM& item : m_data) { // 创建 QCheckBox QCheckBox* checkBox = new QCheckBox(item.key, this); // 设置状态 checkBox->setChecked(item.value == "1"); // 连接信号和槽,checkbox的状态发生改变时调用指定函数 connect(checkBox, &QCheckBox::stateChanged, this, &JCheckBoxCont::onStateChanged); // 存储 QCheckBox 指针,方便后续访问 m_checkboxMap[item.key] = checkBox; // 添加到布局 layout->addWidget(checkBox); } setLayout(layout); } private slots: void onStateChanged(int state) { QCheckBox* checkBox = qobject_cast(sender()); if (!checkBox) { return; } QString name = checkBox->text(); //// 更新数据 //for (ST_DATA_ITEM& item : m_data) //{ // if (item.key == name) // { // item.value = checkBox->isChecked() ? "1" : "2"; // //qDebug() << name << " 状态变为: " << item.value; // break; // } //} } private: QList& m_data; // 数据的引用 QMap m_checkboxMap; // 保存checkbox的指针,以便更新 }; class YourClass : public QWidget { private: QLineEdit* lineEdit; QString lastValidText; // 保存上一次的有效值 public: void setupLineEdit() { lineEdit = new QLineEdit(this); QIntValidator* validator = new QIntValidator(0, 3, this); lineEdit->setValidator(validator); // 保存初始值 lastValidText = "0"; // 连接信号 connect(lineEdit, &QLineEdit::textChanged, this, &YourClass::onTextChanged); } private: void onTextChanged(const QString& text) { bool ok; int value = text.toInt(&ok); if (ok && value >= 0 && value <= 3) { lastValidText = text; // 保存有效值 } else { // 恢复上一次的有效值 lineEdit->setText(lastValidText); // 将光标移到末尾 lineEdit->setCursorPosition(lineEdit->text().length()); } } }; #endif //__CUSTOMLABELCONTROLS_H__