欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > Qt项目【上位机十字屏开发】

Qt项目【上位机十字屏开发】

2024/10/25 10:24:02 来源:https://blog.csdn.net/YHB123___ahnd/article/details/141200661  浏览:    关键词:Qt项目【上位机十字屏开发】

效果图

在这里插入图片描述

说明

重写 QWidget 中的 paintEvent() 处理绘图事件,废话不多说,上代码

源码

#ifndef MYWIDGETFORM_H
#define MYWIDGETFORM_H#include <QWidget>namespace Ui {
class myWidgetForm;
}enum MYTYPE{SIZEWIDTH,SIZEHEIGHT,TOPWIDTH,TOPHEIGHT,MIDDLEWIDTH,MIDDLEHEIGHT,BOTTOMWIDTH,BOTTOMHEIGHT,NONE = 10,
};class myWidgetForm : public QWidget
{Q_OBJECTpublic:explicit myWidgetForm(QWidget *parent = nullptr);~myWidgetForm();void paintEvent(QPaintEvent *event);void setTopWidth(int topWidth);void setTopHeight(int topHeight);void setCenterWidth(int centerWidth);void setCenterHeight(int centerHeight);void setBottomWidth(int bottomWidth);void setBottomHeight(int bottomHeight);void setZoom(int times, QSize topSize, QSize centerSize, QSize bottomSize);public slots:void CrossType(MYTYPE type);private:Ui::myWidgetForm *ui;MYTYPE myType_;int topWidth_;int topHeight_;int centerWidth_;int centerHeight_;int bottomWidth_;int bottomHeight_;
};#endif // MYWIDGETFORM_H
#include "mywidgetform.h"
#include "ui_mywidgetform.h"
#include <QPainter>
#include <QDebug>myWidgetForm::myWidgetForm(QWidget *parent): QWidget(parent), ui(new Ui::myWidgetForm)
{ui->setupUi(this);
}myWidgetForm::~myWidgetForm()
{delete ui;
}void myWidgetForm::paintEvent(QPaintEvent *event)
{QPainter painter(this);painter.setBrush(QColor("#000000"));//原点int posX = (width() - centerWidth_) / 2;int posY = (height() - (topHeight_ + centerHeight_ + bottomHeight_)) / 2;int sizeW = centerWidth_;int sizeH = topHeight_ + centerHeight_ + bottomHeight_;//painter.drawRect(posX, posY, sizeW, sizeH);int leftUpX = (sizeW - topWidth_) / 2;//十字painter.drawRect(posX + leftUpX, posY, topWidth_, topHeight_ + centerHeight_ + bottomHeight_);painter.drawRect(posX, posY + topHeight_, sizeW, centerHeight_ );//四个遮挡部分painter.setBrush(Qt::Dense6Pattern);QRect rect1(posX, posY, leftUpX, topHeight_);QRect rect2(posX + leftUpX + topWidth_, posY, leftUpX, topHeight_);QRect rect3(posX, posY + topHeight_ + centerHeight_, leftUpX, bottomHeight_);QRect rect4(posX + leftUpX + topWidth_, posY + topHeight_ + centerHeight_, leftUpX, bottomHeight_);painter.drawRect(rect1);painter.drawRect(rect2);painter.drawRect(rect3);painter.drawRect(rect4);QPen pen;pen.setWidth(5);pen.setColor(QColor("#ffffff"));painter.setPen(pen);//左上painter.drawLine(posX + leftUpX, posY + 0, posX + leftUpX, posY + topHeight_);painter.drawLine(posX + 0, posY + topHeight_, posX + leftUpX, posY + topHeight_);//右上painter.drawLine(posX + centerWidth_, posY + topHeight_, posX + topWidth_ + leftUpX, posY + topHeight_);painter.drawLine(posX + topWidth_ + leftUpX, posY + 0, posX + topWidth_ + leftUpX, posY + topHeight_);//左下painter.drawLine(posX + 0, posY + centerHeight_ + topHeight_, posX + leftUpX, posY + centerHeight_ + topHeight_);painter.drawLine(posX + leftUpX, posY + sizeH, posX + leftUpX, posY + centerHeight_ + topHeight_);//右下painter.drawLine(posX + centerWidth_, posY + topHeight_ + centerHeight_, posX + topWidth_ + leftUpX, posY + topHeight_ + centerHeight_);painter.drawLine(posX + topWidth_ + leftUpX, posY + sizeH, posX + topWidth_ + leftUpX, posY + topHeight_ + centerHeight_);//宽高painter.drawLine(posX + 0, posY + 0, posX + sizeW, posY + 0);painter.drawLine(posX + 0, posY + 0, posX + 0, posY + sizeH);pen.setWidth(10);pen.setColor(QColor("#ffff00"));painter.setPen(pen);switch(myType_) {case SIZEWIDTH:painter.drawLine(posX + 0, posY + 0, posX + sizeW, posY + 0);break;case SIZEHEIGHT:painter.drawLine(posX + 0, posY + 0, posX + 0, posY + sizeH);break;case TOPWIDTH:painter.drawLine(posX + leftUpX, posY + 0, posX + leftUpX + topWidth_, posY + 0);break;case TOPHEIGHT:painter.drawLine(posX + leftUpX, posY + 0, posX + leftUpX, posY + topHeight_);break;case MIDDLEWIDTH:painter.drawLine(posX + 0, posY + topHeight_, posX + sizeW, posY + topHeight_);break;case MIDDLEHEIGHT:painter.drawLine(posX + 0, posY + topHeight_, posX + 0, posY + topHeight_ + centerHeight_);break;case BOTTOMWIDTH:painter.drawLine(posX + leftUpX, posY + topHeight_ + centerHeight_, posX + leftUpX + topWidth_, posY + topHeight_ + centerHeight_);break;case BOTTOMHEIGHT:painter.drawLine(posX + leftUpX, posY + topHeight_ + centerHeight_, posX + leftUpX, posY + sizeH);break;default:break;}painter.end();update();
}void myWidgetForm::setTopWidth(int topWidth)
{topWidth_ = topWidth;
}void myWidgetForm::setTopHeight(int topHeight)
{topHeight_ = topHeight;
}void myWidgetForm::setCenterWidth(int centerWidth)
{centerWidth_ = centerWidth;
}void myWidgetForm::setCenterHeight(int centerHeight)
{centerHeight_ = centerHeight;
}void myWidgetForm::setBottomWidth(int bottomWidth)
{bottomWidth_ = bottomWidth;
}void myWidgetForm::setBottomHeight(int bottomHeight)
{bottomHeight_ = bottomHeight;
}void myWidgetForm::setZoom(int times, QSize topSize, QSize centerSize, QSize bottomSize)
{topWidth_ = topSize.width() * times;topHeight_ = topSize.height() * times;centerWidth_ = centerSize.width() * times;centerHeight_ = centerSize.height() * times;bottomWidth_ = bottomSize.width() * times;bottomHeight_ = bottomSize.height() * times;
}void myWidgetForm::CrossType(MYTYPE type)
{myType_ = type;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com