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();
}

工程文件

Qt交流大会 853086607 免费群中


在这里插入图片描述

结尾

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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,110评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,443评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,474评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,881评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,902评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,698评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,418评论 3 419
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,332评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,796评论 1 316
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,968评论 3 337
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,110评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,792评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,455评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,003评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,130评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,348评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,047评论 2 355

推荐阅读更多精彩内容