Write control properties Qt designer 3- stretch control

I. Introduction

Plug-control loads, and drag controls also achieved, the next step is one of the most difficulty, with the same QtDesigner or other development environments, we were able to freely stretch control size, mobile location, for this function, but also prepared a special control to to achieve this function, the name SelectWidget plot points follow form controls, general principle is to install an event filter, when generating the control of the control plot points follow the incoming control, automatic identification of the location of the mouse, press pulled away to change control size, plotted point indicator for the user to describe the use of stretching.
Plot points following the controls set whether to draw the plot points, margins, plot points color, describe the dot size, plot points form a square + round, select the border width, support up and down buttons to move the window, support Delete key to delete a form, supports eight trace points change window body sizes.
Experience Address: https://pan.baidu.com/s/1A5Gd77kExm8Co5ckT51vvQ extraction code: 877p File: An executable file .zip

Second, the realization of functions

  1. All controls automatically load the plug-in generates a list file, the default built-in controls over 120.
  2. To drag the canvas automatically generate the corresponding control, WYSIWYG.
  3. Chinese property right of the bar, change the corresponding property is immediately applied to the corresponding selected control, simple and intuitive, very suitable for white use.
  4. Original text translation attribute field mapping mechanism, high efficiency, you can easily expand the property bar other languages.
  5. Automatically extracting properties of all the controls and properties shown on the right column, including drop-down box enumerated value and the like.
  6. Support manually select the plug-in file import plug-in external files.
  7. All controls current canvas configuration information can be exported to a xml file.
  8. You can manually select the xml file open layout control, automatic load control based on the xml file.
  9. Can pull the slider, check box analog data, text input box, the data application in three ways to generate all of the controls.
  10. Control supports eight directions pulling resize, adaptive to any resolution, right and left can fine-tune the keyboard up and down position.
  11. They opened the serial collection, gathering network, database data set collected in three ways.
  12. Code is very concise, very detailed notes, can be configured as a prototype, develop their own more features.
  13. Written in pure Qt, Qt support any version of the compiler + any + any system.

Third, renderings

Fourth, the core code

bool SelectWidget::eventFilter(QObject *watched, QEvent *event)
{
    if (watched == widget) {
        if (event->type() == QEvent::Resize) {
            //设置当前窗体大小为跟随窗体的大小增加部分
            this->resize(this->widget->size() + QSize(padding * 2, padding * 2));
        } else if (event->type() == QEvent::Move) {
            //将当前窗体移到偏移位置
            this->move(this->widget->pos() - QPoint(padding, padding));
        }
    } else {
        if (event->type() == QEvent::KeyPress) {
            QKeyEvent *keyEvent = dynamic_cast<QKeyEvent *>(event);
            if (keyEvent->key() == Qt::Key_Left) {
                this->move(this->pos() - QPoint(1, 0));
            } else if (keyEvent->key() == Qt::Key_Right) {
                this->move(this->pos() + QPoint(1, 0));
            } else if (keyEvent->key() == Qt::Key_Up) {
                this->move(this->pos() - QPoint(0, 1));
            } else if (keyEvent->key() == Qt::Key_Down) {
                this->move(this->pos() + QPoint(0, 1));
            } else if (keyEvent->key() == Qt::Key_Delete) {
                emit widgetDelete(widget);
                widget->deleteLater();
                this->deleteLater();
                widget = 0;
            }

            //重新设置附带窗体的位置和大小
            if (widget != 0) {
                widget->setGeometry(this->x() + padding, this->y() + padding, this->width() - padding * 2, this->height() - padding * 2);
            }

            return QWidget::eventFilter(watched, event);
        }

        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
        if (mouseEvent->type() == QEvent::MouseButtonPress) {
            //记住当前控件坐标和宽高以及鼠标按下的坐标
            rectX = this->x();
            rectY = this->y();
            rectW = this->width();
            rectH = this->height();
            lastPos = mouseEvent->pos();

            //判断按下的手柄的区域位置
            if (rectLeft.contains(lastPos)) {
                pressedLeft = true;
            } else if (rectRight.contains(lastPos)) {
                pressedRight = true;
            } else if (rectTop.contains(lastPos)) {
                pressedTop = true;
            } else if (rectBottom.contains(lastPos)) {
                pressedBottom = true;
            } else if (rectLeftTop.contains(lastPos)) {
                pressedLeftTop = true;
            } else if (rectRightTop.contains(lastPos)) {
                pressedRightTop = true;
            } else if (rectLeftBottom.contains(lastPos)) {
                pressedLeftBottom = true;
            } else if (rectRightBottom.contains(lastPos)) {
                pressedRightBottom = true;
            } else {
                pressed = true;
            }

            if (widget != 0) {
                emit widgetPressed(widget);
            }
        } else if (mouseEvent->type() == QEvent::MouseMove) {
            //根据当前鼠标位置,计算XY轴移动了多少
            QPoint pos = mouseEvent->pos();
            int dx = pos.x() - lastPos.x();
            int dy = pos.y() - lastPos.y();

            //根据按下处的位置判断是否是移动控件还是拉伸控件
            if (pressed) {
                this->move(this->x() + dx, this->y() + dy);
            } else if (pressedLeft) {
                int resizeW = this->width() - dx;
                if (this->minimumWidth() <= resizeW) {
                    this->setGeometry(this->x() + dx, rectY, resizeW, rectH);
                }
            } else if (pressedRight) {
                this->setGeometry(rectX, rectY, rectW + dx, rectH);
            } else if (pressedTop) {
                int resizeH = this->height() - dy;
                if (this->minimumHeight() <= resizeH) {
                    this->setGeometry(rectX, this->y() + dy, rectW, resizeH);
                }
            } else if (pressedBottom) {
                this->setGeometry(rectX, rectY, rectW, rectH + dy);
            } else if (pressedLeftTop) {
                int resizeW = this->width() - dx;
                int resizeH = this->height() - dy;
                if (this->minimumWidth() <= resizeW) {
                    this->setGeometry(this->x() + dx, this->y(), resizeW, resizeH);
                }
                if (this->minimumHeight() <= resizeH) {
                    this->setGeometry(this->x(), this->y() + dy, resizeW, resizeH);
                }
            } else if (pressedRightTop) {
                int resizeW = rectW + dx;
                int resizeH = this->height() - dy;
                if (this->minimumHeight() <= resizeH) {
                    this->setGeometry(this->x(), this->y() + dy, resizeW, resizeH);
                }
            } else if (pressedLeftBottom) {
                int resizeW = this->width() - dx;
                int resizeH = rectH + dy;
                if (this->minimumWidth() <= resizeW) {
                    this->setGeometry(this->x() + dx, this->y(), resizeW, resizeH);
                }
                if (this->minimumHeight() <= resizeH) {
                    this->setGeometry(this->x(), this->y(), resizeW, resizeH);
                }
            } else if (pressedRightBottom) {
                int resizeW = rectW + dx;
                int resizeH = rectH + dy;
                this->setGeometry(this->x(), this->y(), resizeW, resizeH);
            }

            //重新设置附带窗体的位置和大小
            if (widget != 0) {
                widget->setGeometry(this->x() + padding, this->y() + padding, this->width() - padding * 2, this->height() - padding * 2);
            }
        } else if (mouseEvent->type() == QEvent::MouseButtonRelease) {
            pressed = false;
            pressedLeft = false;
            pressedRight = false;
            pressedTop = false;
            pressedBottom = false;
            pressedLeftTop = false;
            pressedRightTop = false;
            pressedLeftBottom = false;
            pressedRightBottom = false;

            if (widget != 0) {
                emit widgetRelease(widget);
            }
        }
    }

    return QWidget::eventFilter(watched, event);
}

void SelectWidget::resizeEvent(QResizeEvent *)
{
    //重新计算八个描点的区域,描点区域的作用还有就是计算鼠标坐标是否在某一个区域内
    int width = this->width();
    int height = this->height();

    //左侧描点区域
    rectLeft = QRectF(0, height / 2 - pointSize / 2, pointSize, pointSize);
    //上侧描点区域
    rectTop = QRectF(width / 2 - pointSize / 2, 0, pointSize, pointSize);
    //右侧描点区域
    rectRight = QRectF(width - pointSize, height / 2 - pointSize / 2, pointSize, pointSize);
    //下侧描点区域
    rectBottom = QRectF(width / 2 - pointSize / 2, height - pointSize, pointSize, pointSize);

    //左上角描点区域
    rectLeftTop = QRectF(0, 0, pointSize, pointSize);
    //右上角描点区域
    rectRightTop = QRectF(width - pointSize, 0, pointSize, pointSize);
    //左下角描点区域
    rectLeftBottom = QRectF(0, height - pointSize, pointSize, pointSize);
    //右下角描点区域
    rectRightBottom = QRectF(width - pointSize, height - pointSize, pointSize, pointSize);
}

void SelectWidget::mouseMoveEvent(QMouseEvent *e)
{
    //计算当前鼠标位置是否在某个区域内,自动更新鼠标形状
    QPoint p = e->pos();
    if (rectLeft.contains(p)) {
        this->setCursor(Qt::SizeHorCursor);
    } else if (rectTop.contains(p)) {
        this->setCursor(Qt::SizeVerCursor);
    } else if (rectRight.contains(p)) {
        this->setCursor(Qt::SizeHorCursor);
    } else if (rectBottom.contains(p)) {
        this->setCursor(Qt::SizeVerCursor);
    } else if (rectLeftTop.contains(p)) {
        this->setCursor(Qt::SizeFDiagCursor);
    } else if (rectRightTop.contains(p)) {
        this->setCursor(Qt::SizeBDiagCursor);
    } else if (rectLeftBottom.contains(p)) {
        this->setCursor(Qt::SizeBDiagCursor);
    } else if (rectRightBottom.contains(p)) {
        this->setCursor(Qt::SizeFDiagCursor);
    } else {
        this->setCursor(Qt::ArrowCursor);
    }
}

Fifth, introduce controls

  1. More than 150 exquisite control, covers a variety of dashboards, progress bar, the progress of the ball, compass, graphs, scales, thermometers, navigation bar, navigation bar, flatui, highlight the button, slide the selector, the lunar calendar and so on. Qwt far more than the number of controls integration.
  2. Each class can be independently as a separate control, zero coupling each control file and a header file to achieve a code amount, independent of other files to facilitate individual control integrated into the project source code form, less. qwt interlocking control class, highly coupled, want to use one of the controls, must contain all the code.
  3. Write all pure Qt, QWidget + QPainter to draw, to support any Qt version Qt4.6 Qt5.12, support for mingw, msvc, gcc compiler, etc., support any operating system such as windows + linux + mac + embedded linux, which does not garbled can be directly integrated into Qt Creator, a built-in controls and use the same, most of the effects can be as long as several properties are set, very convenient.
  4. DEMO separate source containing the control corresponding to each control has a convenient reference. It also provides integrated use of all controls a DEMO.
  5. Source code for each control has detailed Chinese annotation, are prepared in accordance with unified design specifications, easy to learn to write custom controls.
  6. Each control default color and demo corresponding color is very beautiful.
  7. More than 130 visible control, six invisible control.
  8. Portion control provides a variety of styles style selection, multiple choice style indicator.
  9. All controls changes adaptive stretched form.
  10. Integrated design custom attribute that supports drag design, WYSIWYG support the import and export in xml format.
  11. Activex control that comes with demo, all controls can be run directly in the browser ie.
  12. Fontawesome integrated graphics font + Alibaba iconfont collection of hundreds of graphic fonts, font fun graphic brings.
  13. All controls and finally generate a dynamic library files (dll or so, etc.) can be integrated directly into qtcreator designed for use in drag.
  14. Already qml version, the latter will consider a pyqt version, if the user is in great demand then.
  15. Custom plug-in open dynamic library (permanent free), and the back door without any restrictions, ease of use.
  16. 26 now available version dll, which includes qt5.12.3 msvc2017 32 + 64 mingw 32 + 64 in.
  17. From time to time to increase control and improve controls, regularly updated SDK, to welcome all suggestions, thank you!
  18. Qt introductory books recommended Huo Yafei of "Qt Creator Quick Start" "Qt5 programming entry", Qt official Advanced book recommendations "C ++ GUI Qt4 programming."
  19. Highly recommended programmer self-discipline and planning book series "lying programmer" "Programmer's growth course", "grief programmer", benefited from a lifetime!
  20. SDK download link: https://pan.baidu.com/s/1A5Gd77kExm8Co5ckT51vvQ extraction code: 877p

Guess you like

Origin www.cnblogs.com/feiyangqingyun/p/11498292.html