Qt控件编辑功能

简述功能

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

效果图

Qt控件编辑功能_第1张图片
这里写图片描述
Qt控件编辑功能_第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(m_widget) || qobject_cast(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(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(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(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(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();
}

工程文件

Qt交流大会 853086607 免费群中


Qt控件编辑功能_第3张图片
在这里插入图片描述

结尾

不定期上传新作品,解答群中作品相关问题。相关外,能解答则解答。欢迎大家一起探索Qt世界!

你可能感兴趣的:(Qt控件编辑功能)