3

I have used QTimer quite a bit. But right now it is failing and I can't figure it why:

enter image description here

#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;
}
4

1 に答える 1

10

あなたは忘れました:

#include <QTimer>

あなたのcppファイルで。シンボルが既知である理由は、インクルードのチェーンに沿った他のヘッダーが QTimer の前方宣言を行っているためです。つまり、QTimer ポインターと参照を宣言できますが、実際にはインスタンス化できません。

言うまでもなく、それだけに頼るべきではありません。代わりに、次のように変更します。

QTimer *timerDisplay;

に:

class QTimer *timerDisplay;

そして#include <QTimer>、cpp ファイルで。

もう 1 つの問題は、信号を接続しようとしているにもかかわらず、UpdateDisplay() 関数がスロットではないことです。したがって、その関数の宣言をprivate slots:セクションに移動します。

于 2013-07-18T16:51:01.503 に答える