QLabel 动态显示时间的方法(积累,包含QLabel显示图片的方法)

#include <qapplication.h>
#include <qlabel.h>
//#include <qpixmap.h>    //qt中的类
#include <qtimer.h>
#include <qmovie.h>
int main (int argc, char *argv[])
{
QApplication app(argc,argv);
QLabel *label=new QLabel ("", 0); //初始化qlabel
QMovie pm("logo.gif");   //设定要显示的图片
label->setMovie(pm); //将图片加载到label上
label->setGeometry( 0, 0, 240, 320 ); //屏幕大小,初始位置
app.setMainWidget(label); //将图片设为放置在中间
// QTimer::singleShot( 3*1000, label, SLOT(close()));//显示时间
label->show ();
return app.exec();
}

这是百度上的一个例子,实际测试singleShot不是用来显示系统时间的,下面是对此静态函数的官方说明和举例:

void QTimer::singleShot ( int msec, QObject * receiver, const char * member ) [static]
This static function calls a slot after a given time interval.

It is very convenient to use this function because you do not need to bother with a timerEvent or create a local QTimer object.

Example:

 #include <QApplication>
 #include <QTimer>

 int main(int argc, char *argv[])
 {
     QApplication app(argc, argv);
     QTimer::singleShot(600000, &app, SLOT(quit()));
     ...
     return app.exec();
 }
This sample program automatically terminates after 10 minutes (600,000 milliseconds).

The receiver is the receiving object and the member is the slot. The time interval is msec milliseconds.

Note: This function is reentrant.

See also setSingleShot() and start().

可见他更是一个信号槽的时间控制函数,留着以后能用到。

而真正在QLabel中动态显示时间的方法,在网上找了一个比较靠谱的:

构造函数中加入:

           QTimer *timer = new QTimer(this);

           connect(timer,SINGAL(timeout()),this,SLOT(timer_deal_slot_function()));

timer->start(1000); //1000ms触发一次

  槽函数这样写:(获取系统时间)

          QDateTime dateTime = QDateTime::currentDateTime();

          int y=dateTime.date().year();

          int m=dateTime.date().month();

          int d=dateTime.date().day();

       

          QString strTime=dateTime.time().toString();

          ui->label->setText(strTime+"   "+QString::number(y)+"/"+QString::number(m)+"/"+QString::number(d));

                      //显示时间格式为例如  12:24:33    2008/8/28

目前问题已经解决,但有必要贴上一个更傻瓜的显示方式,有必要。
不做敖述,贴上连接:http://www.cnblogs.com/lucasfeng/archive/2012/11/28/2793325.html

你可能感兴趣的:(类,图片,APP,qt,label)