关于QT 计时器的问题,求大神指导。

apuded apuded 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区



状态栏上放一个计时器,有一个textEdit,在textEdit中输入内容可以触发计时器,
问题来了,在textEdit中不断输入内容(或者不断复制粘贴)会导致计时器计时变慢(停止),
如果改为由button触发,问题也是一样的。不知道是什么问题。求大神指导。
源码地址:
http://pan.baidu.com/s/1mgkOB3M

main.cpp/
#include "mainwindow.h"
#include <QApplication>
#include <mainwindow.cpp>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;

// w.StartStopwatch();

w.show();

return a.exec();
}



mainwindow.cpp/
#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
textEdit = new QTextEdit("问题:在这框中不断输入内容(或者不断复制粘贴)会导致计时器计时变慢(停止)",this);

button = new QPushButton("开始",this);
label = new QLabel("时间:00:00:00",this);

layout = new QVBoxLayout();
layout->addWidget(textEdit);
layout->addWidget(button);


widget = new QWidget();
widget->setLayout(layout);
this->setCentralWidget(widget);

statusBar()->show();
statusBar()->setStyleSheet("background:white");
ui->statusBar->addWidget(label,1);





countTemp=0;
secondTemp=0;
minuteTemp=0;
hourTemp=0;






msTimer= new QTimer;


connect(msTimer,SIGNAL(timeout()),this,SLOT(TimeSlot()));
connect(button,SIGNAL(clicked()),this,SLOT(Start()));
connect(textEdit,SIGNAL(textChanged()),this,SLOT(Start()));




}





void MainWindow::TimeSlot()
{
countTemp+=1;
if(countTemp==100)
{
countTemp=0;
secondTemp+=1;
if(secondTemp==60)
{
secondTemp=0;
minuteTemp+=1;
if(minuteTemp==60)
{
minuteTemp=0;
hourTemp+=1;
if(hourTemp==24)
{
hourTemp=0;
}
}
}
}
//把整数转换成字符串
QString hourstr = QString::number(hourTemp);
QString minutestr= QString::number(minuteTemp);
QString secondstr = QString::number(secondTemp);
//设置字符串的长度为2
SetStrLength(&hourstr,2);
SetStrLength(&minutestr,2);
SetStrLength(&secondstr,2);
Display(hourstr + ":",minutestr + ":",secondstr);
}



void MainWindow::Display(QString hour, QString minute, QString second)
{



label->setText("时间:"+hour+minute+second);

}





void MainWindow::StartStopwatch()
{
msTimer->start(10); //10ms



}


void MainWindow::SetStrLength(QString *str, int length)
{
if(str->length()<length)
{
str->insert(0,"0");
}
}




void MainWindow::Start()
{
StartStopwatch();
}





MainWindow::~MainWindow()
{
delete ui;
}


mainwindow.h/
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTextEdit>
#include <QVBoxLayout>
#include <QWidget>
#include <QLabel>
#include <QTimer>
#include <QPushButton>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void StartStopwatch();



private:
Ui::MainWindow *ui;

QTextEdit *textEdit;
QVBoxLayout * layout;
QWidget * widget;
QLabel * label;

QPushButton *button;

int hourTemp;
int minuteTemp;
int secondTemp;
int countTemp;


QTimer *msTimer;




void Display(QString,QString,QString);
void SetStrLength(QString *str, int length);
private slots:
void TimeSlot();
void Start();





};

#endif // MAINWINDOW_H

...全文
评论 16
收藏 已收藏
分享
返回顶部
apuded
Qt 21,504 成员 26,693 内容