2024.03.21作业

自由发挥实现一个登录窗口的应用场景

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QPen>
#include <QBrush>
#include <QPainter>
#include <QWidget>

QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
class Painter;
}
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

protected:
    void paintEvent(QPaintEvent* event);
    void timerEvent(QTimerEvent* event);

private slots:
    void on_pushButton_clicked();

private:
    Ui::Widget *ui;
    int bY = 70;
    int gY = 20;
    int rY = 120;
    bool bUp = false;
    bool gUp = false;
    bool rUp = false;
};

class Painter
{
private:
    QPen pen;
    QBrush brush;
    QPainter painter;

public:
    Painter(Widget* widget);
    void setColor(QColor color);
    void drawRect(int x, int y, int w, int h);
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "./ui_widget.h"
#include <QMessageBox>

Painter::Painter(Widget* widget): painter(widget)
{
    brush.setStyle(Qt::SolidPattern);
}

void Painter::drawRect(int x, int y, int w, int h)
{
    painter.drawRect(x, y, w, h);
}

void Painter::setColor(QColor color)
{
    pen.setColor(color);
    brush.setColor(color);
    painter.setPen(pen);
    painter.setBrush(brush);
}

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->resize(400, 400);
    this->startTimer(30);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::paintEvent(QPaintEvent* event)
{
    Q_UNUSED(event);


    Painter painter(this);
    if (bUp)
    {
        painter.setColor(QColor(0, 0, 255, 255));
        painter.drawRect(100, bY, 200, 50);
    }
    if (gUp)
    {
        painter.setColor(QColor(0, 255, 0, 255));
        painter.drawRect(100, gY, 200, 50);
    }
    if (rUp)
    {
        painter.setColor(QColor(255, 0, 0, 255));
        painter.drawRect(100, rY, 200, 50);
    }

    if (!bUp)
    {
        painter.setColor(QColor(0, 0, 255, 255));
        painter.drawRect(100, bY, 200, 50);
    }
    if (!gUp)
    {
        painter.setColor(QColor(0, 255, 0, 255));
        painter.drawRect(100, gY, 200, 50);
    }
    if (!rUp)
    {
        painter.setColor(QColor(255, 0, 0, 255));
        painter.drawRect(100, rY, 200, 50);
    }

    painter.setColor(QColor(240, 240, 240, 255));
    painter.drawRect(100, 145, 200, 50);

    if (!bUp)
    {
        bY += 1;
        if (122 == bY)
        {
            bUp = true;
        }
    }
    else
    {
        bY -= 1;
        if (bY + 49 == rY)
        {
            bUp = false;
        }
    }

    if (!gUp)
    {
        gY += 1;
        if (122 == gY)
        {
            gUp = true;
        }
    }
    else
    {
        gY -= 1;
        if (gY + 50 == bY)
        {
            gUp = false;
        }
    }

    if (!rUp)
    {
        rY += 1;
        if (122 == rY)
        {
            rUp = true;
        }
    }
    else
    {
        rY -= 1;
        if (rY + 50 == gY)
        {
            rUp = false;
        }
    }
}

void Widget::timerEvent(QTimerEvent* event)
{
    Q_UNUSED(event);
    this->update();
}


void Widget::on_pushButton_clicked()
{
    QString user = ui->user_lineEdit->text();
    QString passwd = ui->passwd_lineEdit->text();

    if ("lkjhxx" == user && "123456" == passwd)
    {
        QMessageBox::information(NULL, "提示", "登录成功!", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
    }
    else
    {
        QMessageBox::information(NULL, "提示", "登录失败!", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
    }
}

效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值