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_OBJECT

public:
    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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

C葭葭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值