QT使用提升自定义组件
QT 组件提升来实现自定义功能
介绍
我们在使用QT设置界面之后,往往需要自己实现一些方法,如果是单独 的还好,但是如果遇到很多同类型的都有需求,
比如 我们使用 QLineEdit 来处理数据,每次填入的数值都要更新到一个参数值中的时候, 我们需要将这个输入框的完成信号 连接到一个自定义的函数中 来更改某个值,
如果很多的时候, 我们就可以使用 继承组件然后使用提升来做了
应用
如图所示的界面,我们需要对每个单行编辑框, 完成点击按钮就调整值的的方法
![QT界面 QT界面](./QT%E4%BD%BF%E7%94%A8%E6%8F%90%E5%8D%87%E8%87%AA%E5%AE%9A%E4%B9%89%E7%BB%84%E4%BB%B6/QT_promotion.png)
QT界面
具体的提升的方法可以参考 Qt自定义控件以及控件的提升 的具体做法, 这里只介绍我们使用的方法
我们自定义的 类为 CLineEdit
我们设置了 公开的 Associate
函数, 将我们需要显示的 QLineEdit
组件 和两个 点击按钮以及内存存储相应数据的位置 关联起来, 点击相应按钮之后 会执行内存数据的加减, 然后再显示在相应的组件上, 便于查看,
实现代码
具体实现代码
/**
* @file Codemodelclineedit.h.
* @copyright Copyright (c) 2019 IRIS_Chen IRIS Lab
*
* @brief Declares the clineedit class
* @changelog 2019/11/12 IRIS_Chen Created.
*/
#ifndef CLINEEDIT_H
#define CLINEEDIT_H
#include <QObject>
#include <QPushButton>
#include <QLineEdit>
#include <QMouseEvent>
#include <QLabel>
#include <QObject>
#include <QWidget>
#include <QString>
/**
* @class CLineEdit clineedit.h Codemodelclineedit.h
*
* @brief 继承Qt 自定义文件 提升成为自己的组件, 修改窗口的值之后 保证相应的值 直接赋值到相应的内存中 如果关联了相应的 点击按钮, 按钮点击之后可以触发 数据更改
*
* @author IRIS_Chen
* @date 2019/11/13
*/
class CLineEdit : public QLineEdit
{
private:
int *m_data_; ///< 指定数据关联的指针
QPushButton *btn_plus_; ///< 定义增加按钮
QPushButton *btn_minus_; ///< 定义减少按钮
public:
explicit CLineEdit(QWidget *parent = nullptr) :
QLineEdit(parent),
m_data_(nullptr),
btn_plus_(nullptr),
btn_minus_(nullptr)
{
// 输入完成进入自定义处理函数
}
/**
* @fn bool CLineEdit::Associate(int *data, bool over = true)
*
* @brief 将内存与组件关联
*
* @author IRIS_Chen
* @date 2019/11/13
*
* @param [in,out] data If non-null, the data
* @param over (Optional) True to over
*
* @return True if it succeeds, false if it fails
*/
bool Associate(int *data, bool over = true)
{
// 有值且不让覆盖 返回出错
if (!over && m_data_ != nullptr)
return false;
m_data_ = data;
ShowData();
return true;
}
/**
* @fn bool CLineEdit::Associate(int *data, QPushButton *plus, QPushButton *minus, bool over = true)
*
* @brief 关联数据 和两个按钮
*
* @author IRIS_Chen
* @date 2019/11/13
*
* @param [in,out] data If non-null, the data
* @param [in,out] plus If non-null, the plus
* @param [in,out] minus If non-null, the minus
* @param over (Optional) True to over
*
* @return True if it succeeds, false if it fails
*/
bool Associate(int *data, QPushButton *plus, QPushButton *minus, bool over = true)
{
// 有值且不让覆盖 返回出错
if (!over && (m_data_ || btn_minus_ || btn_plus_))
return false;
m_data_ = data;
btn_minus_ = minus;
btn_plus_ = plus;
// 关联 按钮点击信号
QObject::connect(this, &QLineEdit::editingFinished, this, &CLineEdit::LineEditDataProc);
QObject::connect(btn_plus_, &QPushButton::clicked, this, &CLineEdit::LineEditDataPlus);
QObject::connect(btn_minus_, &QPushButton::clicked, this, &CLineEdit::LineEditDataMinus);
ShowData();
return true;
}
private slots:
/**
* @fn bool CLineEdit::LineEditDataProc(void)
*
* @brief 数据输入完成 将数据存储相应内存中
* 暂时只考虑 数据部分, 将填入的数据内容给 获取出来
*
* @author IRIS_Chen
* @date 2019/11/13
*
* @return True if it succeeds, false if it fails
*/
bool LineEditDataProc(void)
{
if (!m_data_)
return false;
QString str = this->text();
int num = str.toInt();
// 转换相应的值存储
*(this->m_data_) = static_cast<int>(num);
// LTrace("line data Edit:{}--{}", (int)m_data_, *m_data_);
return true;
}
/**
* @fn bool CLineEdit::LineEditDataPlus(void)
*
* @brief 数据 增加 并做显示
*
* @author IRIS_Chen
* @date 2019/11/13
*
* @return True if it succeeds, false if it fails
*/
bool LineEditDataPlus(void)
{
if (!m_data_)
return false;
*m_data_ = *m_data_ + 1;
ShowData();
// LTrace("line data Plus:{}--{}", (int)m_data_, *m_data_);
return true;
}
/**
* @fn bool CLineEdit::LineEditDataMinus(void)
*
* @brief 数据减少 信号
*
* @author IRIS_Chen
* @date 2019/11/13
*
* @return True if it succeeds, false if it fails
*/
bool LineEditDataMinus(void)
{
if (!m_data_)
return false;
*m_data_ = *m_data_ - 1;
ShowData();
// LTrace("line data Minus:{}--{}", (int)m_data_, *m_data_);
return true;
}
private:
/**
* @fn void CLineEdit::ShowData(void)
*
* @brief Shows the data
*
* @author IRIS_Chen
* @date 2019/11/13
*/
void ShowData(void)
{
if (m_data_)
{
QString str = QString::number(*m_data_);
this->setText(str);
string tmp = str.toStdString();
}
}
};
#endif // CLINEEDIT_H
更多
我们可以设置相应的更为复杂的提升, 不仅关联 int 类型数据, 还能关联其他类型的数据比如字符串之类的 便于后续开发过程