【QT】利用QT写一个简易番茄钟


前言

  本意是想做一个包含可置顶于桌面的check list、可置顶于桌面的小型番茄钟以及日程提醒的小型工具。目前先利用QT制作了一个简易的番茄时钟,剩下的之后再完成。番茄钟效果如下:
在这里插入图片描述


一、新建QT工程

  打开QT,上方菜单栏选中文件->新建文件或项目->Application Qt-> Qt Widgets Application.
记得修改classname。
在这里插入图片描述

二、番茄钟实现

1.素材导入

在本案中需要对按键的样式进行替换因此需要导入所需的图片素材,本案采用的图案素材如下:
在这里插入图片描述

在建立的qt工程中右键该工程,选择Add new:
在这里插入图片描述

在弹出的窗口中选中Qt-> Qt Resource File,点击choose。
在这里插入图片描述
点击Add prefix,在前缀中输入一个"/"。
在这里插入图片描述
接着在工程的文件目录下建立一个imgs文件夹,将图片文件放进该文件夹。
在这里插入图片描述
再回到工程中,点击Add Files,找到刚才的图片导入,关闭.qrc文件的页面(快捷键ctrl+w),选择save all,在右侧栏中即可看见导入的素材文件。
在这里插入图片描述
如果需要对素材文件进行调整,可以右键.qrc文件,选择Open in Editor即可对素材文件进行调整。

2.ui设计

  该番茄钟的UI设计很简单,只需要一个显示倒计时的控件,一个开始计时按钮以及一个关闭按钮。在右侧栏的Forms中选中并双击.ui文件,即可对UI进行编辑。
  拖入两个PushButton以及一个LCD Number,调整位置及大小。
在这里插入图片描述
  接下来我们对按键的样式进行更改,对PushButton右键,选择更改样式表,在弹出的窗口中点击添加资源旁的小三角,选择border-image,选中刚才导入的图片素材即可完成按钮样式的更换。
在这里插入图片描述

2.代码实现

@TODO 具体代码对应功能实现之后再写

Tomato.pro

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    tomato.cpp

HEADERS += \
    tomato.h

FORMS += \
    tomato.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

RESOURCES += \
    res.qrc

QMAKE_CXXFLAGS += /wd"4819"
LIBS += -lDbgHelp
RC_ICONS=favicon.ico

tomoto.h

#ifndef TOMATO_H
#define TOMATO_H

#include <QWidget>
#include <QMouseEvent>
#include <QTime>
#include <QTimer>
#include <QPainter>
#include <QFont>
#include <QVBoxLayout>
#include <QPushButton>

QT_BEGIN_NAMESPACE
namespace Ui { class Tomato; }
QT_END_NAMESPACE

class Tomato : public QWidget
{
    Q_OBJECT

public:
    Tomato(QWidget *parent = nullptr);
    ~Tomato();
protected:
    void mousePressEvent(QMouseEvent *);
    void mouseMoveEvent(QMouseEvent *);
    void paintEvent(QPaintEvent *);

private slots:
    void updateTime();
    void InitTime();
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

private:
    Ui::Tomato *ui;
    bool state; //暂停
    int tomato_num; //番茄计数
    QString current_color;
    QPoint clickPos;
    QTimer *timer;
    QTime time;
    QPushButton *startbutton;
};
#endif // TOMATO_H

main.cpp

#include "tomato.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Tomato w;
    w.show();
    return a.exec();
}

tomoto.cpp

#include "tomato.h"
#include "ui_tomato.h"

#define BORDER_COLOR QColor(222, 222, 222, 222)
#define BACKGROUND_COLOR QColor(255, 255, 255, 255)
#define RADIUS 15

Tomato::Tomato(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Tomato)
{
    setAttribute(Qt::WA_TranslucentBackground);
    setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinMaxButtonsHint | Qt::WindowStaysOnTopHint);
    state = true; //暂停
    tomato_num = 0;
    ui->setupUi(this);
    QFont font("Rockwell", 13);
    font.setBold(true);
    timer = new QTimer;
    ui->Timer->setFont(font);
    ui->Timer->setDigitCount(5);
    ui->Timer->setLineWidth(0);
    ui->Timer->setSegmentStyle(QLCDNumber::Filled);
    timer->setInterval(1000);
    InitTime();
}


void Tomato::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);  // 抗锯齿

    QPen pen(BORDER_COLOR);
    pen.setWidth(1);  // 设置边框
    painter.setPen(pen);

    QBrush brush(BACKGROUND_COLOR);
    painter.setBrush(brush);

    QRectF rect(0, 0, this->width(), this->height()); 
    painter.drawRoundedRect(rect, RADIUS, RADIUS);  // 设置矩形的圆角
}

void Tomato::mouseMoveEvent(QMouseEvent * e)
{
    if(e->buttons()&Qt::LeftButton)
        move(e->globalPos()-clickPos); 
}

void Tomato::mousePressEvent(QMouseEvent * e)
{
    if(e->button()&Qt::LeftButton)
    {

        clickPos=e->globalPos()-this->geometry().topLeft(); 
    }
}


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

void Tomato::InitTime()
{
    time.setHMS(0,25,0);
    ui->Timer->display(time.toString("mm:ss"));
    ui->Timer->setStyleSheet ("color:red");
}

void Tomato::updateTime()
{
    time = time.addSecs(-1);
    ui->Timer->display(time.toString("mm:ss"));
    if(time.minute() == 0  && time.second() == 0 && tomato_num==1)
    {
//        休息完毕
        tomato_num = 0;
        InitTime();
        timer->stop();
        state = false;
        ui->pushButton->setStyleSheet("QPushButton{border-image: url(:/imgs/startt.png);border:none;}");
    }
    if(time.minute() == 0  && time.second() == 0 && tomato_num==0)
    {
//        进入休息
        tomato_num = 1;
        time.setHMS(0,5,0);
        ui->Timer->setStyleSheet ("color:green");
    }
}





void Tomato::on_pushButton_clicked()
{
    if(state)
    {
        timer->start();
        connect(timer,SIGNAL(timeout()),this,SLOT(updateTime()));
        ui->pushButton->setStyleSheet("QPushButton{border-image: url(:/imgs/stop.png);border:none;}");
    }
    else
    {
        timer->stop();
        ui->pushButton->setStyleSheet("QPushButton{border-image: url(:/imgs/startt.png);border:none;}");
        InitTime();
    }
    state = !state;
}


void Tomato::on_pushButton_2_clicked()
{
    this->close();
}


3.打包

  将debug改为Release,点击运行,在项目文件中找到Release文件下的exe文件,创建一个新的文件夹将该exe复制到新创建的文件夹中。
在这里插入图片描述
  打开qt的控制台,进入到新建的文件夹中,输入windeployqt 你的文件名.exe,回车即可打包。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值