用一个qt 番茄时钟来学习qt界面,通过做来学习知识,工科应该是先浅理论,再深实践,再回顾理论。
在Qt Creator中创建番茄时钟应用 - 完整指南
下面是从零开始在Qt Creator中创建番茄时钟应用的详细步骤:
- 创建新项目
打开Qt Creator,点击"文件"→"新建文件或项目"
选择"Application"→"Qt Widgets Application"
设置项目名称(如"PomodoroTimer")和路径
在"Kit Selection"页面选择你的编译工具链
在"类信息"页面保持默认(QMainWindow)
点击"完成"创建项目
- 设计UI界面
修改mainwindow.ui文件
在项目文件列表中双击mainwindow.ui打开设计器
从左侧部件盒中拖拽以下控件到窗口中:
一个QLabel(用于显示时间) - 设置objectName为timeLabel
三个QPushButton(开始、暂停、重置) - 设置objectName为startButton, pauseButton, resetButton
一个QLabel(用于显示当前状态) - 设置objectName为statusLabel
调整控件大小和布局,可以使用垂直布局管理器
设置初始文本:
timeLabel: “25:00”
statusLabel: “准备开始工作”
按钮: “开始”, “暂停”, “重置”
- 实现功能代码
修改mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTimer>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_startButton_clicked();
void on_pauseButton_clicked();
void on_resetButton_clicked();
void updateTimer();
private:
Ui::MainWindow *ui;
QTimer *timer;
int timeRemaining;
bool isWorking; // true=工作时间, false=休息时间
bool isRunning;
void switchMode();
void updateDisplay();
};
#endif // MAINWINDOW_H
修改mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTime>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 初始化
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &MainWindow::updateTimer);
isWorking = true;
isRunning = false;
timeRemaining = 25 * 60; // 25分钟工作时间
updateDisplay();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_startButton_clicked()
{
if (!isRunning) {
timer->start(1000); // 1秒触发一次
isRunning = true;
ui->startButton->setEnabled(false);
ui->pauseButton->setEnabled(true);
}
}
void MainWindow::on_pauseButton_clicked()
{
if (isRunning) {
timer->stop();
isRunning = false;
ui->startButton->setEnabled(true);
ui->pauseButton->setEnabled(false);
}
}
void MainWindow::on_resetButton_clicked()
{
timer->stop();
isRunning = false;
isWorking = true;
timeRemaining = 25 * 60;
updateDisplay();
ui->startButton->setEnabled(true);
ui->pauseButton->setEnabled(false);
}
void MainWindow::updateTimer()
{
timeRemaining--;
updateDisplay();
if (timeRemaining <= 0) {
switchMode();
}
}
void MainWindow::switchMode()
{
isWorking = !isWorking;
timeRemaining = isWorking ? 25 * 60 : 5 * 60; // 25分钟工作/5分钟休息
// 可以在这里添加声音提示
updateDisplay();
}
void MainWindow::updateDisplay()
{
QTime time(0, 0, 0);
time = time.addSecs(timeRemaining);
ui->timeLabel->setText(time.toString("mm:ss"));
QString status = isWorking ? "工作时间" : "休息时间";
if (!isRunning) {
status += " (已暂停)";
}
ui->statusLabel->setText(status);
}
- 构建和运行
点击左下角的"构建"按钮(或按Ctrl+B)编译项目
点击"运行"按钮(或按Ctrl+R)启动应用
测试功能:
点击"开始"按钮开始计时
点击"暂停"按钮暂停计时
点击"重置"按钮重置计时器
计时结束后会自动切换工作/休息模式
- 可选改进
添加声音提示:
在工作/休息切换时播放提示音
使用QSoundEffect或QMediaPlayer
添加设置功能:
允许用户自定义工作/休息时长
使用QSettings保存用户偏好
美化界面:
使用QSS样式表美化控件
添加番茄时钟的图标
添加计时记录:
记录完成的番茄钟数量
保存到文件或数据库
- 完整项目结构
text
PomodoroTimer/
├── PomodoroTimer.pro
├── main.cpp
├── mainwindow.cpp
├── mainwindow.h
└── mainwindow.ui
这个实现包含了番茄时钟的核心功能,你可以根据需要进一步扩展和完善它。