Qt: PushButton 和 Timer

參考書籍《Qt5 開發及示例 :  2.5 控件》 

(PS  : 不知道爲啥作者講控件的時候,聊起了Timer )

1. PushButton

代碼示例:創建一個按鍵,單擊退出窗口。

#include "mywidget.h"
#include "qpushbutton.h"
#include "qfont.h"

MyWidget::MyWidget(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);

	// 設置窗口尺寸
	setMinimumSize(200, 120);  
	setMaximumSize(200, 120);
	// 創建按鍵對象
	QPushButton* quit = new QPushButton("quit", this);
	// 創建字體實例
	QFont qfont;
	qfont.setFamily("Times");
	qfont.setPointSizeF(18);
	qfont.setBold(true);
	qfont.setItalic(true);
	// 將按鍵的左上角座標設置爲(62,40),按鍵長75, 高30 並配置字體
	quit->setGeometry(62, 40, 75, 30);
	quit->setFont(qfont);
	// 將按鍵單擊事件信號與窗口退出槽關聯
	// aApp 爲 qApplication的全局指針
	connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
}

2. QDateTime

獲取系統時間

#include <QDateTime>
... ...
	QDateTime tm;
	tm = QDateTime::currentDateTime();
	qDebug() << tm.toString();

運行結果

3. Qtimer

設置定時器的觸發間隔,指定超時事件的槽,啓動定時器

#include "qtest.h"


QTEST::QTEST(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
    m_pProgressBar = new QProgressBar(this);
    m_pTimer = new QTimer();
    m_pProgressBar->setRange(0, 100);
    m_pProgressBar->setValue(0);
    m_pTimer->setInterval(1000);
    connect(m_pTimer, SIGNAL(timeout()), this, SLOT(updateProgress()));
    m_pTimer->start();
}
void QTEST::updateProgress(QWidget* parent)
{
    int nCurrentValue = m_pProgressBar->value();
    nCurrentValue++;
    if (nCurrentValue >= 100)
        m_pTimer->stop();
    m_pProgressBar->setValue(nCurrentValue);
}
#pragma once
#include <QTimer>
#include <QPushButton>
#include <QProgressBar>
#include <QtWidgets/QMainWindow>
#include "ui_qtest.h"

class QTEST : public QMainWindow
{
	Q_OBJECT

public:
	QTEST(QWidget *parent = Q_NULLPTR);
	QTimer* m_pTimer;
    QProgressBar* m_pProgressBar;
private:
	Ui::QTESTClass ui;

private slots:
	void updateProgress(QWidget* parent = Q_NULLPTR);
};

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章