Qt學習筆記 4 QTimer和QTime

2021-07-11 21:26:06 字數 3498 閱讀 3807

qtimer是乙個計時器類

它的使用分三步,建立物件,連線signal和slot函式,start()

qtimer *timer = new qtimer(this)

; connect(timer, signal(timeout())

, this, slot(update())

);timer-

>start(

1000

);

其中,signal(timeout())表示:每當計時結束,計時器歸零並重新計時,並傳送乙個訊號啟用slot函式。

timer->start(1000);當中的1000,就是1000毫秒的意思,表示每次timeout的時間間隔是1000ms

如果我們想讓這個計時器只計時一次,那麼必須使用void setsingleshot(bool singleshot)函式。

qtimer *timer = new qtimer(this)

; connect(timer, signal(timeout())

, this, slot(update())

);timer-

>setsetsingleshot(true)

timer-

>start(

60000

);

這樣計時器只會倒計時1分鐘,然後結束。

當然我們還可以改變計時週期

void setinterval(

int msec)

qtime 提供時間函式給使用者使用,它和qtimer的區別就和手錶與秒錶的區別一樣。

qtime主要用於對時間的操作,他提供了大量的函式便於使用者對時間進行轉換和計算。

型別名稱

說明qtime()

構造乙個時間為0的物件

qtime(int h, int m, int s = 0, int ms = 0)

構造乙個具有初始時間的物件

qtime

addmsecs(int ms) const

在當前時間基礎上增加ms毫秒,ms可為負

qtime

addsecs(int s) const

在當前時間基礎上增加s秒,s可為負

inthour() const

返回小時數

intminute() const

返回分鐘數

intsecond() const

返回秒int

msec() const

返回毫秒

bool

isvalid() const

判斷當前物件的時間是否有效,畢竟1天不可能有25小時,也不會存在1分61秒

bool

isvalid(int h, int m, int s, int ms = 0)

判斷輸入的時間是否有效

intmsecsto(const qtime & t) const

計算距離時間t的毫秒數,如果t早於當前時間,則為負

intsecsto(const qtime & t) const

計算距離時間t的秒數

bool

sethms(int h, int m, int s, int ms = 0)

設定標準hms時間,如果不符合標準,返回false

下面是最重要的幾個

void

start()

將當前系統時間記錄為當前時間

intrestart()

將當前系統時間記錄為當前時間,並返回距離上次呼叫start()或者restart()函式間隔的毫秒數

intelapsed() const

計算與最近一次呼叫start()或者restart()函式間隔的毫秒數,相當於計時器

qstring

tostring(const qstring & format) const

將時間轉化為特定的字串格式

qstring

tostring(qt::dateformat format = qt::textdate) const

按照qt::dateformat的格式轉化

qtime

fromstring(const qstring & string, qt::dateformat format = qt::textdate)

從qt::dateformat轉化為qtime物件

qtime

fromstring(const qstring & string, const qstring & format)

從特定的字串格式轉化為qtime物件

qtime

currenttime()

得到當前的系統時間

此外,const qstring & format需要特別說明,**如下:

expression

output

hthe hour without a leading zero (0 to 23 or 1 to 12 if am/pm display)

hhthe hour with a leading zero (00 to 23 or 01 to 12 if am/pm display)

hthe hour without a leading zero (0 to 23, even with am/pm display)

hhthe hour with a leading zero (00 to 23, even with am/pm display)

mthe minute without a leading zero (0 to 59)

mmthe minute with a leading zero (00 to 59)

sthe second without a leading zero (0 to 59)

ssthe second with a leading zero (00 to 59)

zthe milliseconds without leading zeroes (0 to 999)

zzzthe milliseconds with leading zeroes (000 to 999)

apinterpret as an am/pm time. ap must be either 「am」 or 「pm」.

apinterpret as an am/pm time. ap must be either 「am」 or 「pm」.

tthe timezone (for example 「cest」)

例子:format

result

hh:mm:ss.zzz

14:13:09.042

hⓜ️s ap

2:13:9 pm

hⓜ️s a

14:13:9 pm

而qt::dateformat又分為很多種,比如qt::textdate、qt::isodate等,詳請見官方說明,這裡就不一一指出了。

Q學習 4 QT的元物件系統

qt對c 進行了擴充套件,提供了三個主要的功能 訊號槽 執行時型別資訊和動態屬性,這三個擴充套件功能都是由 元物件系統 提供的。元物件系統基於三個支撐點 1 oobject為需要使用元物件系統有點的類提供了基類。2 q object巨集宣告在類的私有段中,可用來啟用元物件特徵,如動態屬性,訊號槽。3...

《C和指標》學習筆記(4)

進入 pointer on c 的第二章,不過感覺這一章沒講什麼實質性的內容,主要是一些風格 字元以及一些古老的東西 比如三字母詞 自己也不是很感興趣。不過也算是了解一下c的歷史了吧。不過問題和程式還是好好看了看。比如又知道了一些轉義字元 在書寫多個分號的情況下使用,防止被解釋為三字母詞。用於表示乙...

QT學習筆記(4)對話方塊(1)

對話方塊分為模態和非模態兩種,模態又分為應用級別和視窗級別的。模態對話方塊意為只能在當前視窗下操作,而非模態可以在不同視窗間進行切換 qdialog中,使用exec 是模態對話方塊,show 是非模態對話方塊 最好建立qdialog指標,否則對話方塊在堆上建立,根本看不見 include mainw...