Qt 时间显示

来源:互联网 发布:qq网络营销软件 编辑:程序博客网 时间:2024/05/21 11:33

一、槽函数:

void shopping_homepage::timeoutslot()

{

   QDateTime time = QDateTime::currentDateTime();    //获取日期和时间

   QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd"); //显示日期和时间

   ui->label_2->setText(str);

}

二、在.h文件

Private slots:   //声明槽函数

void timeoutslot();

 

三、在构造函数加入:(红色为添加部分)

shopping_homepage::shopping_homepage(QWidget *parent) :

    QDialog(parent),

    ui(newUi::shopping_homepage)

{

ui->setupUi(this);

       QDateTime time =QDateTime::currentDateTime();  //获取日期和时间

        QString str = time.toString("yyyy-MM-ddhh:mm:ss dddd"); //显示日期和时间

        ui->label_2->setText(str);

        QTimer *timer=new QTimer(this);

        timer->start(1000);                               //定时器(1秒)

        connect(timer, SIGNAL(timeout()), this,SLOT(timeoutslot()));  //timeoutslot()为自定义槽

       }

原创粉丝点击