我已经使用了很多 QTimer。但现在它失败了,我不知道为什么:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtGui/QPushButton>
#include <QtGui/QTextEdit>
#include <QtGui/QMessageBox>
#include <QtCore/QCoreApplication>
// Server
#include <sys/socket.h>
#include <netinet/in.h>
// Client
//#include <sys/socket.h>
//#include <netinet/in.h>
#include <netdb.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QPushButton *m_btn1;
QPushButton *m_btn2;
QTextEdit *m_txt1;
QTextEdit *m_txt2;
QTimer *timerDisplay;
void UpdateDisplay();
private slots:
void handleBtn1();
void handleBtn2();
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_btn1 = new QPushButton("Start", this);
m_btn1->setGeometry(QRect(QPoint(10,20), QSize(100,50)));
connect(m_btn1, SIGNAL(released()), this, SLOT(handleBtn1()));
m_btn2 = new QPushButton("Send", this);
m_btn2->setGeometry(QRect(QPoint(110, 20), QSize(100, 50)));
connect(m_btn2, SIGNAL(released()), this, SLOT(handleBtn2()));
m_txt1 = new QTextEdit("hello",this);
m_txt1->setGeometry(QRect(QPoint(10,100), QSize(300, 50)));
timerDisplay = new QTimer(this);
connect(timerDisplay, SIGNAL(timeout()), this, SLOT(updateDisplay()));
timerDisplay->start(10);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::handleBtn1()//Start
{
if (1){
QMessageBox *msgBox = new QMessageBox(0);
msgBox->setGeometry(QRect(QPoint(200,200),QSize(400,400)));
msgBox->setInformativeText(m_txt1->toPlainText());
msgBox->exec();
}
}
void MainWindow::handleBtn2()//Send
{
}
void MainWindow::UpdateDisplay()
{
static int c = 0;
QString strC = "number: " + QString::number(c, 'd', 0);
m_txt1 = strC;
}