美文网首页
Qt控件编辑功能

Qt控件编辑功能

作者: 雨田哥工作号 | 来源:发表于2018-11-01 19:33 被阅读0次

简述功能

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世界!

相关文章

  • Qt控件编辑功能

    简述功能 1.支持拉伸,拖拽;2.双击控件支持编辑功能; 效果图 代码 工程文件 Qt交流大会 853086607...

  • Qt控件编辑功能(二)

    简述 根据QtDesigner的控件选中,拉伸效果,用过Qt的盆友都很熟悉Qt的Designer,这个我就不多说了...

  • Qt任务策略控件

    Qt任务策略控件 @[toc] 简述 一网友,让帮忙弄的任务策略控件,其实我也不知道该怎么称呼这个控件。 功能 1...

  • Qt编写自定义控件4-旋转仪表盘

    前言 旋转仪表盘,一般用在需要触摸调节设置值的场景中,其实Qt本身就提供了QDial控件具有类似的功能,本控件最大...

  • Qt任务事件日历控件

    Qt任务事件日历控件 @[toc] 功能 1.炫酷日历控件2.自定义修改日历肤色(接口没开放,可自行添加)3.任务...

  • iOS-UITextField代理协议中方法

    UITextField:(输入框)控件作为UI中经常用到的一种控件.它用来显示文字和编辑文字.核心功能是文字编辑....

  • pyqt5 主界面构建利器:QStackedWidget

    QStackedWidget介绍 QT里面,有一个控件QStackedWidget,叫做堆载窗口控件。这个控件,很...

  • pyqt5-主界面构建利器:QStackedWidget

    QStackedWidget介绍 QT里面,有一个控件QStackedWidget,叫做堆载窗口控件。这个控件,很...

  • Qt 录音播放控件

    Qt 录音播放控件 [TOC] 功能 录音(自动保存WAV文件) 播放音频文件 音频频谱显示 背景色,频谱色可调 ...

  • QT中将gbk字符转换为utf-8字符

    对于中文显示,由于QT默认采用UTF-8编码显示,若在控件中使用GBK类型的中文,则会显示乱码。 给Qt控件设置内...

网友评论

      本文标题:Qt控件编辑功能

      本文链接:https://www.haomeiwen.com/subject/keffxqtx.html