Qt 範例程式設計:報時器〈Windows〉
- 先使用【Qt 設計家】設計人機介面
- 在 QLabel::labelTime 顯示目前時間
- 按下 QPushButton::pushButtonApply 會停止或啟動自動更新目前時間顯示
- 按下 QPushButton::pushButtonClose 會結束程式
- 【Qt 設計家】產生人機介面設計檔 SampleUI.ui 內容如下
<?xml version="1.0" encoding="UTF-8" ?>
- <ui version="4.0">
<class>SampleUI</class>
- <widget class="QDialog" name="SampleUI">
- <property name="geometry">
- <rect>
<x>0</x>
<y>0</y>
<width>236</width>
<height>136</height>
</rect>
</property>
- <property name="font">
- <font>
<family>微軟正黑體</family>
</font>
</property>
- <property name="windowTitle">
<string>報時器</string>
</property>
- <property name="windowIcon">
- <iconset>
<normaloff>../My Pictures/Me.jpg</normaloff>
../My Pictures/Me.jpg
</iconset>
</property>
- <widget class="QLabel" name="labelTime">
- <property name="geometry">
- <rect>
<x>66</x>
<y>40</y>
<width>101</width>
<height>20</height>
</rect>
</property>
- <property name="sizePolicy">
- <sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
- <property name="text">
<string>00:00:00</string>
</property>
- <property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
- <widget class="QPushButton" name="pushButtonApply">
- <property name="geometry">
- <rect>
<x>40</x>
<y>90</y>
<width>75</width>
<height>23</height>
</rect>
</property>
- <property name="text">
<string>停止報時[&T]</string>
</property>
</widget>
- <widget class="QPushButton" name="pushButtonQuit">
- <property name="geometry">
- <rect>
<x>120</x>
<y>90</y>
<width>75</width>
<height>23</height>
</rect>
</property>
- <property name="text">
<string>結束[&Q]</string>
</property>
</widget>
</widget>
<layoutdefault spacing="6" margin="11" />
<resources />
- <connections>
- <connection>
<sender>pushButtonQuit</sender>
<signal>clicked()</signal>
<receiver>SampleUI</receiver>
<slot>close()</slot>
- <hints>
- <hint type="sourcelabel">
<x>157</x>
<y>101</y>
</hint>
- <hint type="destinationlabel">
<x>117</x>
<y>67</y>
</hint>
</hints>
</connection>
</connections>
</ui>
- 使用【Qt 設計家】【File->Open File or Project】為 SampleUI 專案
- 【Qt 設計家】產生:
- main.cpp
- SampleUI.cpp
- QTimer::myTimer 會每秒更新時間顯示
- 按下 QPushButton::pushButtonApply 會如開關停止或開始自動顯示時間
- SampleUI.h
#include <QtGui/QApplication>
#include <QTextCodec>
#include "SampleUI.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// 中文字碼:Big5
QTextCodec *Big5 = QTextCodec::codecForName("Big5");
QTextCodec::setCodecForLocale(Big5);
QTextCodec::setCodecForCStrings(Big5);
QTextCodec::setCodecForTr(Big5);
SampleUI w;
w.show();
return a.exec();
}
#ifndef SAMPLEUI_H
#define SAMPLEUI_H
#include <QDialog>
#include <QLabel>
#include <QTimer>
#include <QTime>
#include <QString>
namespace Ui {
class SampleUI;
}
class SampleUI : public QDialog {
Q_OBJECT
public:
SampleUI(QWidget *parent = 0);
~SampleUI();
protected:
void changeEvent(QEvent *e);
private:
Ui::SampleUI *ui;
// 時間觸發器
QTimer *myTimer;
QTime *myTime;
QString myTimeString;
private slots:
void on_Timer_Event();
// 按下【QPushButton::pushButtonApply】
void on_pushButtonApply_clicked();
};
#endif // SAMPLEUI_H
#include "SampleUI.h"
#include "ui_SampleUI.h"
SampleUI::SampleUI(QWidget *parent) : QDialog(parent), ui(new Ui::SampleUI)
{
ui->setupUi(this);
this->myTime = new QTime();
// 時間觸發器
this->myTimer = new QTimer(this);
connect(this->myTimer, SIGNAL(timeout()), this, SLOT(on_Timer_Event()));
// 每秒觸發
this->myTimer->start(1000);
}
SampleUI::~SampleUI()
{
delete this->myTimer;
delete this->myTime;
delete this->ui;
}
void SampleUI::changeEvent(QEvent *e)
{
QDialog::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void SampleUI::on_Timer_Event()
{
this->myTimeString = myTime->currentTime().toString("hh:mm:ss");
this->ui->labelTime->setText(this->myTimeString);
}
void SampleUI::on_pushButtonApply_clicked()
{
if (this->myTimer->isActive()) {
this->myTimer->stop();
this->ui->pushButtonApply->setText(tr("啟動報時"));
}
else {
this->myTimer->start();
this->ui->pushButtonApply->setText(tr("停止報時"));
}
}