Qt控件编辑功能

Qt控件编辑功能

简述功能

1.支持拉伸,拖拽;
2.双击控件支持编辑功能;

效果图

这里写图片描述

这里写图片描述

代码

//用法试列代码
CompontEditor* editlabel = new CompontEditor(ui.label, ui.label->text(), this);
CompontEditor* editbutton = new CompontEditor(ui.pushButton, ui.pushButton->text(), this);
//以下为核心代码
TextEditor::TextEditor(QWidget* parent) 
    : QWidget(parent)
    , m_lineEdit(new QLineEdit(this))
{
    installEventFilter(this);
    m_lineEdit->setObjectName("q_texteditor");
    m_lineEdit->setFrame(false);
    m_lineEdit->setBackgroundRole(parent->backgroundRole());

    setFocusProxy(m_lineEdit);
    connect(m_lineEdit, &QLineEdit::editingFinished, this, &TextEditor::editingFinished);
    connect(m_lineEdit, &QLineEdit::returnPressed, this, &TextEditor::slotEditingFinished);
    connect(m_lineEdit, &QLineEdit::textChanged, this, &TextEditor::slotTextChanged);
}

TextEditor::~TextEditor()
{

}

void TextEditor::slotTextChanged(const QString &text)
{
    m_cachedText = text;
}

void TextEditor::slotEditingFinished()
{
    emit textChanged(m_cachedText);
}

QString TextEditor::text() const{
    return m_cachedText;
}

void TextEditor::setText(const QString &text){
    m_cachedText = text;
    m_lineEdit->setText(text);
}

void TextEditor::setAlignment(Qt::Alignment align)
{
    m_lineEdit->setAlignment(align);
}

void TextEditor::selectAll() {
    m_lineEdit->selectAll();
}

void TextEditor::clear() {
    m_lineEdit->clear();
}

void TextEditor::resizeEvent(QResizeEvent * event) {
    m_lineEdit->resize(event->size());
}

QSize TextEditor::sizeHint() const {
    return  m_lineEdit->sizeHint();
}

QSize TextEditor::minimumSizeHint() const {
    return  m_lineEdit->minimumSizeHint();
}

void TextEditor::installEventFilter(QObject *filterObject)
{
    if (m_lineEdit)
        m_lineEdit->installEventFilter(filterObject);
}

CompontEditor::CompontEditor(QWidget *widget, const QString& text, QWidget* parent)
    : TextEditor(parent)
    , m_widget(widget)
    , m_leftButtonPress(false)
    , m_type(Type::NORMAL)
{
    qApp->installEventFilter(this);
    setText(text);
    selectAll();
    setAlignment(alignment());

    QRect r = editRectangle();
    setGeometry(QRect(widget->mapTo(widget->window(), r.topLeft()), r.size()));
    this->hide();

    connect(this, &TextEditor::editingFinished, [this](){
        this->hide();
        m_widget->setProperty("text", this->text());
    });
}

Qt::Alignment CompontEditor::alignment() const {
    if (m_widget->metaObject()->indexOfProperty("alignment") != -1)
        return Qt::Alignment(m_widget->property("alignment").toInt());

    if (qobject_cast<const QPushButton *>(m_widget) || qobject_cast<const QToolButton *>(m_widget))
        return Qt::AlignHCenter;

    return Qt::AlignJustify;
}

CompontEditor::~CompontEditor(){

}

QRect CompontEditor::editRectangle() const
{
    QStyleOptionButton opt;
    opt.init(m_widget);
    if (m_widget->inherits("QPushButton")){
        return m_widget->style()->subElementRect(QStyle::SE_PushButtonContents, &opt, m_widget);
    }
    else if (m_widget->inherits("QRadioButton")){
        return m_widget->style()->subElementRect(QStyle::SE_RadioButtonContents, &opt, m_widget);
    }
    else if (m_widget->inherits("QCheckBox")){
        return m_widget->style()->subElementRect(QStyle::SE_CheckBoxContents, &opt, m_widget);
    }
    else{
        return opt.rect;
    }   
}

bool CompontEditor::eventFilter(QObject *watched, QEvent *event)
{
    if (event->type() == QEvent::MouseButtonDblClick){
        if (watched == m_widget){
            this->selectAll();
            this->setFocus();
            this->show();
        }
    }
    else if (event->type() == QEvent::MouseButtonPress){
        QMouseEvent* mouse = dynamic_cast<QMouseEvent*>(event);
        if (watched == m_widget){
            if (mouse->button() == Qt::LeftButton){
                m_leftButtonPress = true;
            }
            m_mousepressPos = mouse->globalPos();
        }
        else{
            QRect rect(this->mapToGlobal(QPoint(0, 0)), this->size());
            if (this->isVisible() && !rect.contains(mouse->globalPos())){
                emit editingFinished();
            }
        }
    }
    else if (event->type() == QEvent::MouseMove){
        QMouseEvent* mouse = dynamic_cast<QMouseEvent*>(event);
        if (watched == m_widget){
            m_mousemovePos = mouse->globalPos();
            if (m_leftButtonPress && m_type == NORMAL){
                QPoint movepoint = mouse->globalPos() - m_mousepressPos;
                m_mousepressPos = mouse->globalPos();
                m_widget->move(m_widget->pos() + movepoint);
            }
            else if (m_leftButtonPress && m_type != NORMAL){
                resizeSection();
            }
            else{
                updateCursorType();
            }
        }
    }
    else if (event->type() == QEvent::MouseButtonRelease){
        QMouseEvent* mouse = dynamic_cast<QMouseEvent*>(event);
        if (watched == m_widget){
            m_mousepressPos = mouse->globalPos();
            m_leftButtonPress = false;
        }
    }
    else if (event->type() == QEvent::Resize) {
        if (watched == m_widget){
            const QResizeEvent *resizeevent = static_cast<const QResizeEvent*>(event);
        }
    }
    else if (event->type() == QEvent::Show) {
        if (watched == this){
            QRect r = editRectangle();
            setGeometry(QRect(m_widget->mapTo(m_widget->window(), r.topLeft()), r.size()));
        }
    }
    return QWidget::eventFilter(watched, event);
}

void CompontEditor::updateCursor()
{
    switch (m_type) {
    case LeftTop:
        m_widget->setCursor(Qt::SizeFDiagCursor);
        break;
    case Top:
        m_widget->setCursor(Qt::SizeVerCursor);
        break;
    case RightTop:
        m_widget->setCursor(Qt::SizeBDiagCursor);
        break;
    case Right:
        m_widget->setCursor(Qt::SizeHorCursor);
        break;
    case RightBottom:
        m_widget->setCursor(Qt::SizeFDiagCursor);
        break;
    case Bottom:
        m_widget->setCursor(Qt::SizeVerCursor);
        break;
    case LeftBottom:
        m_widget->setCursor(Qt::SizeBDiagCursor);
        break;
    case Left:
        m_widget->setCursor(Qt::SizeHorCursor);
        break;
    default:
        m_widget->setCursor(Qt::ArrowCursor);
        break;
    }
}

void CompontEditor::resizeSection()
{
    QPoint widgetGloabPoint(m_widget->mapToGlobal(QPoint(0, 0)));
    QPoint widgetpoint = m_widget->mapToParent(m_widget->mapFromGlobal(m_mousemovePos));
    switch (m_type) {
    case LeftTop:{
        int resizeH = widgetGloabPoint.y() - m_mousemovePos.y() + m_widget->height();
        int resizeW = widgetGloabPoint.x() - m_mousemovePos.x() + m_widget->width();
        if (m_widget->minimumHeight() <= resizeH && resizeH <= m_widget->maximumHeight()){
            m_widget->setGeometry(m_widget->x(), widgetpoint.y(), resizeW, resizeH);
        }
        if (m_widget->minimumWidth() <= resizeW && resizeW <= m_widget->maximumWidth()){
            m_widget->setGeometry(widgetpoint.x(), m_widget->y(), resizeW, resizeH);
        }
    }
        break;
    case Top:{
        int resizeH = widgetGloabPoint.y() - m_mousemovePos.y() + m_widget->height();
        if (m_widget->minimumHeight() <= resizeH && resizeH <= m_widget->maximumHeight()){
            m_widget->setGeometry(m_widget->x(), widgetpoint.y(), m_widget->width(), resizeH);
        }
    }
        break;
    case RightTop:{
        int resizeH = widgetGloabPoint.y() + m_widget->height() - m_mousemovePos.y();
        int resizeW = m_mousemovePos.x() - widgetGloabPoint.x();
        int pointY = widgetpoint.y();
        if (m_widget->minimumHeight() <= resizeH && resizeH <= m_widget->maximumHeight()){
            m_widget->setGeometry(m_widget->x(), widgetpoint.y(), resizeW, resizeH);
        }
    }
        break;
    case Right:{
        int resizeW = m_mousemovePos.x() - widgetGloabPoint.x();
        m_widget->setGeometry(m_widget->x(), m_widget->y() , resizeW, m_widget->height());
    }
        break;
    case RightBottom:{
        int resizeW = m_mousemovePos.x() - widgetGloabPoint.x();
        int resizeH = m_mousemovePos.y() - widgetGloabPoint.y();
        m_widget->setGeometry(m_widget->x(), m_widget->y(), resizeW, resizeH);
    }
        break;
    case Bottom:{
        int resizeH = m_mousemovePos.y() - widgetGloabPoint.y();
        m_widget->setGeometry(m_widget->x(), m_widget->y(), m_widget->width(), resizeH);
    }
        break;
    case LeftBottom:{
        int resizeH = m_mousemovePos.y() - widgetGloabPoint.y();
        int resizeW = widgetGloabPoint.x() - m_mousemovePos.x() + m_widget->width();
        if (m_widget->minimumWidth() <= resizeW && resizeW <= m_widget->maximumWidth()){
            m_widget->setGeometry(widgetpoint.x(), m_widget->y(), resizeW, resizeH);
        }
        if (m_widget->minimumHeight() <= resizeH && resizeH <= m_widget->maximumHeight()){
            m_widget->setGeometry(m_widget->x(), m_widget->y(), resizeW, resizeH);
        }
    }
        break;
    case Left:{
        int resizeW = widgetGloabPoint.x() - m_mousemovePos.x() + m_widget->width();
        if (m_widget->minimumWidth() <= resizeW && resizeW <= m_widget->maximumWidth()){
            m_widget->setGeometry(widgetpoint.x(), m_widget->y(), resizeW, m_widget->height());
        }
    }
        break;
    default:{

        }
        break;
    }
}

void CompontEditor::updateCursorType()
{
    QRect widgetGloabRect(m_widget->mapToGlobal(QPoint(0, 0)), m_widget->size());
    if (QRect(widgetGloabRect.bottomLeft() - QPoint(DISTANCE / 2, DISTANCE / 2), widgetGloabRect.bottomLeft() + QPoint(DISTANCE / 2, DISTANCE / 2)).contains(m_mousemovePos)){
        m_type = LeftBottom;
    }
    else if (QRect(widgetGloabRect.bottomRight() - QPoint(DISTANCE / 2, DISTANCE / 2), widgetGloabRect.bottomRight() + QPoint(DISTANCE / 2, DISTANCE / 2)).contains(m_mousemovePos)){
        m_type = RightBottom;
    }
    else if (QRect(widgetGloabRect.topRight() - QPoint(DISTANCE / 2, DISTANCE / 2), widgetGloabRect.topRight() + QPoint(DISTANCE / 2, DISTANCE / 2)).contains(m_mousemovePos)){
        m_type = RightTop;
    }
    else if (QRect(widgetGloabRect.topLeft() - QPoint(DISTANCE / 2, DISTANCE / 2), widgetGloabRect.topLeft() + QPoint(DISTANCE / 2, DISTANCE / 2)).contains(m_mousemovePos)){
        m_type = LeftTop;
    }
    else if (qAbs(m_mousemovePos.x() - widgetGloabRect.left()) < DISTANCE){
        m_type = Left;
    }
    else if (qAbs(m_mousemovePos.y() - widgetGloabRect.bottom()) < DISTANCE){
        m_type = Bottom;
    }
    else if (qAbs(m_mousemovePos.x() - widgetGloabRect.right()) < DISTANCE){
        m_type = Right;
    }
    else if (qAbs(m_mousemovePos.y() - widgetGloabRect.top()) < DISTANCE){
        m_type = Top;
    }
    else{
        m_type = NORMAL;
    }
    updateCursor();
}

结尾

需要完整工程可以加我QQ,后期也会找个时间上传。

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雨田哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值