• 1817阅读
  • 1回复

Qt编写控件属性设计器3-拉伸控件 [复制链接]

上一主题 下一主题
离线liudianwu
 

只看楼主 倒序阅读 楼主  发表于: 2019-09-10
一、前言
插件控件加载了,拖曳控件也实现了,接下来就是一个最难点了,跟QtDesigner或者其他开发环境一样,能够任意自由的拉伸控件大小,移动位置,为了这个功能,还特别编写了一个控件来实现这个功能,名字叫SelectWidget描点跟随窗体控件,大致的原理就是安装事件过滤器,在生成控件的时候将该控件传入描点跟随控件,自动识别鼠标的位置,按下拉动的距离来改变控件的大小,绘制描点指示器以便用户拉伸使用。
描点跟随控件可设置是否绘制描点、边距、描点颜色、描点尺寸、描点样式 正方形+圆形、选中边框宽度,支持上下左右按键移动窗体,支持delete键删除窗体,支持八个描点改变窗体大小尺寸。
体验地址:https://pan.baidu.com/s/1A5Gd77kExm8Co5ckT51vvQ  提取码:877p  文件:可执行文件.zip

二、实现的功能
1. 自动加载插件文件中的所有控件生成列表,默认自带的控件超过120个。
2. 拖曳到画布自动生成对应的控件,所见即所得。
3. 右侧中文属性栏,改变对应的属性立即应用到对应选中控件,直观简洁,非常适合小白使用。
4. 独创属性栏文字翻译映射机制,效率极高,可以非常方便拓展其他语言的属性栏。
5. 所有控件的属性自动提取并显示在右侧属性栏,包括枚举值下拉框等。
6. 支持手动选择插件文件,外部导入插件文件。
7. 可以将当前画布的所有控件配置信息导出到xml文件。
8. 可以手动选择xml文件打开控件布局,自动根据xml文件加载控件。
9. 可拉动滑动条、勾选模拟数据复选框、文本框输入,三种方式来生成数据应用所有控件。
10. 控件支持八个方位拉动调整大小,自适应任意分辨率,可键盘上下左右微调位置。
11. 打通了串口采集、网络采集、数据库采集三种方式设置数据。
12. 代码极其精简,注释非常详细,可以作为组态的雏形,自行拓展更多的功能。
13. 纯Qt编写,支持任意Qt版本+任意编译器+任意系统。

三、效果图



四、核心代码
  1. bool SelectWidget::eventFilter(QObject *watched, QEvent *event)
  2. {
  3.     if (watched == widget) {
  4.         if (event->type() == QEvent::Resize) {
  5.             //设置当前窗体大小为跟随窗体的大小增加部分
  6.             this->resize(this->widget->size() + QSize(padding * 2, padding * 2));
  7.         } else if (event->type() == QEvent::Move) {
  8.             //将当前窗体移到偏移位置
  9.             this->move(this->widget->pos() - QPoint(padding, padding));
  10.         }
  11.     } else {
  12.         if (event->type() == QEvent::KeyPress) {
  13.             QKeyEvent *keyEvent = dynamic_cast<QKeyEvent *>(event);
  14.             if (keyEvent->key() == Qt::Key_Left) {
  15.                 this->move(this->pos() - QPoint(1, 0));
  16.             } else if (keyEvent->key() == Qt::Key_Right) {
  17.                 this->move(this->pos() + QPoint(1, 0));
  18.             } else if (keyEvent->key() == Qt::Key_Up) {
  19.                 this->move(this->pos() - QPoint(0, 1));
  20.             } else if (keyEvent->key() == Qt::Key_Down) {
  21.                 this->move(this->pos() + QPoint(0, 1));
  22.             } else if (keyEvent->key() == Qt::Key_Delete) {
  23.                 emit widgetDelete(widget);
  24.                 widget->deleteLater();
  25.                 this->deleteLater();
  26.                 widget = 0;
  27.             }
  28.             //重新设置附带窗体的位置和大小
  29.             if (widget != 0) {
  30.                 widget->setGeometry(this->x() + padding, this->y() + padding, this->width() - padding * 2, this->height() - padding * 2);
  31.             }
  32.             return QWidget::eventFilter(watched, event);
  33.         }
  34.         QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
  35.         if (mouseEvent->type() == QEvent::MouseButtonPress) {
  36.             //记住当前控件坐标和宽高以及鼠标按下的坐标
  37.             rectX = this->x();
  38.             rectY = this->y();
  39.             rectW = this->width();
  40.             rectH = this->height();
  41.             lastPos = mouseEvent->pos();
  42.             //判断按下的手柄的区域位置
  43.             if (rectLeft.contains(lastPos)) {
  44.                 pressedLeft = true;
  45.             } else if (rectRight.contains(lastPos)) {
  46.                 pressedRight = true;
  47.             } else if (rectTop.contains(lastPos)) {
  48.                 pressedTop = true;
  49.             } else if (rectBottom.contains(lastPos)) {
  50.                 pressedBottom = true;
  51.             } else if (rectLeftTop.contains(lastPos)) {
  52.                 pressedLeftTop = true;
  53.             } else if (rectRightTop.contains(lastPos)) {
  54.                 pressedRightTop = true;
  55.             } else if (rectLeftBottom.contains(lastPos)) {
  56.                 pressedLeftBottom = true;
  57.             } else if (rectRightBottom.contains(lastPos)) {
  58.                 pressedRightBottom = true;
  59.             } else {
  60.                 pressed = true;
  61.             }
  62.             if (widget != 0) {
  63.                 emit widgetPressed(widget);
  64.             }
  65.         } else if (mouseEvent->type() == QEvent::MouseMove) {
  66.             //根据当前鼠标位置,计算XY轴移动了多少
  67.             QPoint pos = mouseEvent->pos();
  68.             int dx = pos.x() - lastPos.x();
  69.             int dy = pos.y() - lastPos.y();
  70.             //根据按下处的位置判断是否是移动控件还是拉伸控件
  71.             if (pressed) {
  72.                 this->move(this->x() + dx, this->y() + dy);
  73.             } else if (pressedLeft) {
  74.                 int resizeW = this->width() - dx;
  75.                 if (this->minimumWidth() <= resizeW) {
  76.                     this->setGeometry(this->x() + dx, rectY, resizeW, rectH);
  77.                 }
  78.             } else if (pressedRight) {
  79.                 this->setGeometry(rectX, rectY, rectW + dx, rectH);
  80.             } else if (pressedTop) {
  81.                 int resizeH = this->height() - dy;
  82.                 if (this->minimumHeight() <= resizeH) {
  83.                     this->setGeometry(rectX, this->y() + dy, rectW, resizeH);
  84.                 }
  85.             } else if (pressedBottom) {
  86.                 this->setGeometry(rectX, rectY, rectW, rectH + dy);
  87.             } else if (pressedLeftTop) {
  88.                 int resizeW = this->width() - dx;
  89.                 int resizeH = this->height() - dy;
  90.                 if (this->minimumWidth() <= resizeW) {
  91.                     this->setGeometry(this->x() + dx, this->y(), resizeW, resizeH);
  92.                 }
  93.                 if (this->minimumHeight() <= resizeH) {
  94.                     this->setGeometry(this->x(), this->y() + dy, resizeW, resizeH);
  95.                 }
  96.             } else if (pressedRightTop) {
  97.                 int resizeW = rectW + dx;
  98.                 int resizeH = this->height() - dy;
  99.                 if (this->minimumHeight() <= resizeH) {
  100.                     this->setGeometry(this->x(), this->y() + dy, resizeW, resizeH);
  101.                 }
  102.             } else if (pressedLeftBottom) {
  103.                 int resizeW = this->width() - dx;
  104.                 int resizeH = rectH + dy;
  105.                 if (this->minimumWidth() <= resizeW) {
  106.                     this->setGeometry(this->x() + dx, this->y(), resizeW, resizeH);
  107.                 }
  108.                 if (this->minimumHeight() <= resizeH) {
  109.                     this->setGeometry(this->x(), this->y(), resizeW, resizeH);
  110.                 }
  111.             } else if (pressedRightBottom) {
  112.                 int resizeW = rectW + dx;
  113.                 int resizeH = rectH + dy;
  114.                 this->setGeometry(this->x(), this->y(), resizeW, resizeH);
  115.             }
  116.             //重新设置附带窗体的位置和大小
  117.             if (widget != 0) {
  118.                 widget->setGeometry(this->x() + padding, this->y() + padding, this->width() - padding * 2, this->height() - padding * 2);
  119.             }
  120.         } else if (mouseEvent->type() == QEvent::MouseButtonRelease) {
  121.             pressed = false;
  122.             pressedLeft = false;
  123.             pressedRight = false;
  124.             pressedTop = false;
  125.             pressedBottom = false;
  126.             pressedLeftTop = false;
  127.             pressedRightTop = false;
  128.             pressedLeftBottom = false;
  129.             pressedRightBottom = false;
  130.             if (widget != 0) {
  131.                 emit widgetRelease(widget);
  132.             }
  133.         }
  134.     }
  135.     return QWidget::eventFilter(watched, event);
  136. }
  137. void SelectWidget::resizeEvent(QResizeEvent *)
  138. {
  139.     //重新计算八个描点的区域,描点区域的作用还有就是计算鼠标坐标是否在某一个区域内
  140.     int width = this->width();
  141.     int height = this->height();
  142.     //左侧描点区域
  143.     rectLeft = QRectF(0, height / 2 - pointSize / 2, pointSize, pointSize);
  144.     //上侧描点区域
  145.     rectTop = QRectF(width / 2 - pointSize / 2, 0, pointSize, pointSize);
  146.     //右侧描点区域
  147.     rectRight = QRectF(width - pointSize, height / 2 - pointSize / 2, pointSize, pointSize);
  148.     //下侧描点区域
  149.     rectBottom = QRectF(width / 2 - pointSize / 2, height - pointSize, pointSize, pointSize);
  150.     //左上角描点区域
  151.     rectLeftTop = QRectF(0, 0, pointSize, pointSize);
  152.     //右上角描点区域
  153.     rectRightTop = QRectF(width - pointSize, 0, pointSize, pointSize);
  154.     //左下角描点区域
  155.     rectLeftBottom = QRectF(0, height - pointSize, pointSize, pointSize);
  156.     //右下角描点区域
  157.     rectRightBottom = QRectF(width - pointSize, height - pointSize, pointSize, pointSize);
  158. }
  159. void SelectWidget::mouseMoveEvent(QMouseEvent *e)
  160. {
  161.     //计算当前鼠标位置是否在某个区域内,自动更新鼠标形状
  162.     QPoint p = e->pos();
  163.     if (rectLeft.contains(p)) {
  164.         this->setCursor(Qt::SizeHorCursor);
  165.     } else if (rectTop.contains(p)) {
  166.         this->setCursor(Qt::SizeVerCursor);
  167.     } else if (rectRight.contains(p)) {
  168.         this->setCursor(Qt::SizeHorCursor);
  169.     } else if (rectBottom.contains(p)) {
  170.         this->setCursor(Qt::SizeVerCursor);
  171.     } else if (rectLeftTop.contains(p)) {
  172.         this->setCursor(Qt::SizeFDiagCursor);
  173.     } else if (rectRightTop.contains(p)) {
  174.         this->setCursor(Qt::SizeBDiagCursor);
  175.     } else if (rectLeftBottom.contains(p)) {
  176.         this->setCursor(Qt::SizeBDiagCursor);
  177.     } else if (rectRightBottom.contains(p)) {
  178.         this->setCursor(Qt::SizeFDiagCursor);
  179.     } else {
  180.         this->setCursor(Qt::ArrowCursor);
  181.     }
  182. }




五、控件介绍
1. 超过150个精美控件,涵盖了各种仪表盘、进度条、进度球、指南针、曲线图、标尺、温度计、导航条、导航栏,flatui、高亮按钮、滑动选择器、农历等。远超qwt集成的控件数量。
2. 每个类都可以独立成一个单独的控件,零耦合,每个控件一个头文件和一个实现文件,不依赖其他文件,方便单个控件以源码形式集成到项目中,较少代码量。qwt的控件类环环相扣,高度耦合,想要使用其中一个控件,必须包含所有的代码。
3. 全部纯Qt编写,QWidget+QPainter绘制,支持Qt4.6到Qt5.12的任何Qt版本,支持mingw、msvc、gcc等编译器,支持任意操作系统比如windows+linux+mac+嵌入式linux等,不乱码,可直接集成到Qt  Creator中,和自带的控件一样使用,大部分效果只要设置几个属性即可,极为方便。
4. 每个控件都有一个对应的单独的包含该控件源码的DEMO,方便参考使用。同时还提供一个所有控件使用的集成的DEMO。
5. 每个控件的源代码都有详细中文注释,都按照统一设计规范编写,方便学习自定义控件的编写。
6. 每个控件默认配色和demo对应的配色都非常精美。
7. 超过130个可见控件,6个不可见控件。
8. 部分控件提供多种样式风格选择,多种指示器样式选择。
9. 所有控件自适应窗体拉伸变化。
10. 集成自定义控件属性设计器,支持拖曳设计,所见即所得,支持导入导出xml格式。
11. 自带activex控件demo,所有控件可以直接运行在ie浏览器中。
12. 集成fontawesome图形字体+阿里巴巴iconfont收藏的几百个图形字体,享受图形字体带来的乐趣。
13. 所有控件最后生成一个动态库文件(dll或者so等),可以直接集成到qtcreator中拖曳设计使用。
14. 目前已经有qml版本,后期会考虑出pyqt版本,如果用户需求量很大的话。
15. 自定义控件插件开放动态库使用(永久免费),无任何后门和限制,请放心使用。
16. 目前已提供26个版本的dll,其中包括了qt5.12.3 msvc2017 32+64 mingw 32+64 的。
17. 不定期增加控件和完善控件,不定期更新SDK,欢迎各位提出建议,谢谢!
18. Qt入门书籍推荐霍亚飞的《Qt Creator快速入门》《Qt5编程入门》,Qt进阶书籍推荐官方的《C++ GUI Qt4编程》。
19. 强烈推荐程序员自我修养和规划系列书《大话程序员》《程序员的成长课》《解忧程序员》,受益匪浅,受益终生!
20. SDK下载链接:https://pan.baidu.com/s/1A5Gd77kExm8Co5ckT51vvQ  提取码:877p
欢迎关注微信公众号:Qt实战/Qt入门和进阶(各种开源作品、经验整理、项目实战技巧,专注Qt/C++软件开发,视频监控、物联网、工业控制、嵌入式软件、国产化系统应用软件开发) QQ:517216493  WX:feiyangqingyun  QQ群:751439350
离线zhao4565821

只看该作者 1楼 发表于: 2019-09-10
大师  你这个可以实现布局的功能吗
快速回复
限100 字节
 
上一个 下一个