[求助]qt的timer怎么发不出timeout signal
这是我们考试的一道题小时钟:继承QDialog类开发一个图形用户界面程序,用QLabel显示当前系统的时间,格式为“hour:minute:second”,使显示时钟的QLabel在每秒更新时间。
提示:可能需要用到QTime、QTimer和QLabel等类的知识。
我的这个能显示时间,但更新不了,希望大牛们帮我看看。
我的源代码:
//dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QTime>
#include <QTimer>
#include <QLabel>
class dialog: public QDialog
{
public:
dialog(QDialog* parent = 0);
QLabel* labe;
QTimer* timer;
QTime* time;
public slots:
void refresh();
};
#endif // DIALOG_H
//dialog.cpp
#include "dialog.h"
#include <QTime>
#include <QTimer>
#include <QLabel>
#include <QDialog>
#include <qtimer.h>
#include <QHBoxLayout>
#include <QMessageBox>
#include "dialog.h"
dialog::dialog(QDialog* parent):QDialog(parent)
{
labe = new QLabel;
timer = new QTimer;
QTime ti = QTime::currentTime();
time = &ti;
char str1[5];
itoa(time->hour(),str1,10);
char str2[5];
itoa(time->minute(),str2,10);
char str3[5];
itoa(time->second(),str3,10);
QString str = QString(str1) +":"+QString(str2)+":"+QString(str3);
labe ->setText(str);
QHBoxLayout* layout = new QHBoxLayout;
layout -> addWidget(labe);
this -> setLayout(layout);
connect(timer,SIGNAL(timeout()),this,SLOT(refresh()));
timer ->setSingleShot(false);
timer ->setInterval(1000);
}
void dialog::refresh()
{
labe->clear();
QTime ti = QTime::currentTime();
time = &ti;
char str1[5];
itoa(time->hour(),str1,10);
char str2[5];
itoa(time->minute(),str2,10);
char str3[5];
itoa(time->second(),str3,10);
QMessageBox::information(this, tr(""), tr("Mouse Right Button Pressed:"));
QString str = QString(str1) +":"+QString(str2)+":"+QString(str3);
labe ->setText(str);
}
//main.cpp
#include <QtGui>
#include <QApplication>
#include "dialog.h"
int main(int argc, char* argv[])
{
QApplication app(argc,argv);
dialog* dia = new dialog;
dia ->show();
return app.exec();
}
--------------------编程问答-------------------- 掌握调试技巧:
1. 观察控制台输出:如果你观察的话,就知道它已经告诉你,“槽不存在!”
2. 学会调试:信号槽不工作,要记得去判断connect的返回值是否真的为true。如果你判断的话,应该知道,“Connect已经失败”
3. 不要忘记Q_OBJECT。(添加后不要忘记重新运行qmake) --------------------编程问答-------------------- 是不是看了1L的大牛的答复已经解决了?
--------------------编程问答-------------------- 你的timer在哪里start呢……? --------------------编程问答-------------------- 你的timer new出来以后,是不是要调用start();如果你想让它一秒钟执行一次,就是start(1000),这样每过一秒就触发timeout()吧。然后才能refresh. 我不知道说的对不对,我也自学没多久,错了别怪我 --------------------编程问答-------------------- 显示系统时间很好搞吧
--------------------编程问答-------------------- 有专门的timeEvent函数啊
构造函数
timerEvent(0);
startTimer(1000);
void Window::timerEvent(QTimerEvent *)
{
//定时器读秒
QDateTime datatime=QDateTime::currentDateTime();
QString strTime =datatime.time().toString();
timeLabel->setText(strTime);
QTimer *time=new QTimer(timeLabel);
connect(time,SIGNAL(timeout()),timeLabel,SLOT(show()));
time->start(1000);
}
补充:移动开发 , Qt