• 5591阅读
  • 3回复

[提问]QT时钟程序 [复制链接]

上一主题 下一主题
离线qq526665621
 

只看楼主 倒序阅读 楼主  发表于: 2011-05-18
— 本帖被 XChinux 从 C/C++基础 移动到本区(2011-05-18) —
麻烦大家帮忙看下,为什么我的程序里时钟显示不变化,不能动态显示时间呢?
class MyMainWindow : public QWidget
{
        public:
                MyMainWindow();
        private:
                QLabel *c_label;
                QLabel *d_label;
                QLabel *t_label;
                QLabel *n_label;
                QLabel *c_line;
                QLabel *d_line;
                QLabel *t_line;
                QLabel *n_line;
};
MyMainWindow::MyMainWindow()
{
        setMinimumSize(320,240);
        setMaximumSize(320,240);
        QDate date=QDate::currentDate();
        QTime time=QTime::currentTime();
        QTimer *timer = new QTime(t_line);

        c_label=new QLabel(this);
        d_label=new QLabel(this);
        t_label=new QLabel(this);
        n_label=new QLabel(this);
        c_label->setGeometry(40,20,60,40);
        c_label->setText("class:");
        c_label->setAlignment(Qt::AlignCenter);
        c_label->setFont(QFont("Time",16));
        d_label->setGeometry(40,70,60,40);
        d_label->setText("data:");
        d_label->setAlignment(Qt::AlignCenter);
        d_label->setFont(QFont("Time",16));
        t_label->setGeometry(40,120,60,40);
        t_label->setText("time:");
        t_label->setAlignment(Qt::AlignCenter);
        t_label->setFont(QFont("Time",16));
        n_label->setGeometry(40,170,60,40);
        n_label->setText("num:");
        n_label->setAlignment(Qt::AlignCenter);
        n_label->setFont(QFont("Time",16));
        c_line=new QLabel("4301",this);
        d_line=new QLabel(date.toString(),this);
        t_line=new QLabel(time.toString(),this);
        n_line=new QLabel(this);
        c_line->setGeometry(110,20,170,40);
        c_line->setFont(QFont("Time",16));
        d_line->setGeometry(110,70,170,40);
        d_line->setFont(QFont("Time",16));
        t_line->setGeometry(110,120,170,40);
        t_line->setFont(QFont("Time",16));
        t_line->setText(time.toString("hh:mm:ss"));
        connect(timer,SIGNAL(timeout()),t_line,SLOT(update()));
       timer->start(1000);

        n_line->setGeometry(110,170,170,40);
        n_line->setFont(QFont("Time",16));
};
int main(int argc, char **argv)
{
        QApplication app(argc, argv);
        MyMainWindow mymainwindow;
        mymainwindow.show();
        return app.exec();
};

离线jdwx

只看该作者 1楼 发表于: 2011-05-18
QDate date=QDate::currentDate();
QTime time=QTime::currentTime();
QTimer *timer = new QTime(t_line);
这几个都定义在构造函数里,构造函数执行完,就被销毁了。
应该定义成类成员变量。
发帖时要说明:操作系统、Qt版本、编译器,这样能更快的得到回复。
离线morriszy

只看该作者 2楼 发表于: 2011-05-18
    //创建定时器
  slotShowDateTime();
    QTimer *timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(slotShowDateTime()));
    timer->start(1000);


void MainWindow::slotShowDateTime()
{
    QDateTime date_time = QDateTime::currentDateTime();
    QString dateTime = date_time.toString("yyyy-MM-dd hh:mm:ss");
    label_time->setText(dateTime);
}
VS2010+Qt4.7.0
离线roywillow

只看该作者 3楼 发表于: 2011-05-18
我想楼主一定是理解错了update这个函数的意思
确实是不可能动态,因为t_line->setText(time.toString("hh:mm:ss"));这句只在构造函数中执行了一次
[ 此帖被roywillow在2011-05-18 18:00重新编辑 ]
专业维修核潜艇,回收二手航母、二手航天飞机,大修核反应堆,拆洗导弹发动机更换机油,无人侦察机手动挡改自动,航天飞机保养换三滤,飞碟外太空年检 ,各型号导弹加装迎宾踏板,高空作业擦洗卫星表面除尘、打蜡及抛光,东风全系列巡航导弹。并提供原子对撞机。量大从优,有正规发票。
快速回复
限100 字节
 
上一个 下一个