一、槽函数:
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()为自定义槽
}