使用QTimer更新UI , 还是在UI主线程做更新
重点以红色字体标示
//
#include ""
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
();
return ();
}
//
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTimer>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QTimer *timer;
int counter;
private slots:
void buttonClicked();
void updateUI();
};
#endif // MAINWINDOW_H
//
#include ""
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
timer = new QTimer(this);
timer->setInterval(0.01);
counter = 0;
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(buttonClicked()));
connect(timer,SIGNAL(timeout()),this,SLOT(updateUI()));
timer->start();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::buttonClicked()
{
}
void MainWindow::updateUI()
{
QString str = QString::number(counter++);
ui->textEdit->append("update!" + str);
}