Qt設計:時間顯示(QTimer)

謝橋發表於2019-04-27

這裡寫圖片描述

##//mymainwindow.cpp

#include "mymainwindow.h"
#include "ui_mymainwindow.h"
#include <QTime>
#include <QTimer>
#include <QDebug>
#include <QPalette>

myMainWindow::myMainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::myMainWindow)
{
    QPalette pe;
    QFont font("Arial",16);
    ui->setupUi(this);

    pe.setColor(QPalette::Background,Qt::white);
    pe.setColor(QPalette::WindowText,Qt::black);
    ui->label->setAutoFillBackground(true);
    ui->label->setPalette(pe);
    ui->label->setFont(font);
    QTimer *timer=new QTimer(this);
    timer->start(1000);
    connect(timer,SIGNAL(timeout()),this,SLOT(update()));

}

myMainWindow::~myMainWindow()
{
    delete ui;
}
void myMainWindow::paintEvent(QPaintEvent *)
{
    QDateTime dt=QDateTime::currentDateTime();
    QString p;
    p=dt.toString("  yyyy-MM-dd  hh:mm:ss PA  ");
    this->ui->label->setText(p);
}

##//mymainwindow.h

#ifndef MYMAINWINDOW_H
#define MYMAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class myMainWindow;
}

class myMainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit myMainWindow(QWidget *parent = 0);
    ~myMainWindow();

private:
    Ui::myMainWindow *ui;

private slots:
    void paintEvent(QPaintEvent *);


};

#endif // MYMAINWINDOW_H

##//main.cpp

#include "mymainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    myMainWindow w;
    w.show();

    return a.exec();
}

閒聊或者有任何建議都可以加QQ群交流:712573569

需回答問題:加群幹什麼吖

答案/回答:查水錶

相關文章