jdl
jdl

Reputation: 6323

QTimer incomplete type

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

Upvotes: 4

Views: 6687

Answers (1)

Nikos C.
Nikos C.

Reputation: 51832

You forgot to:

#include <QTimer>

in your cpp file. The reason the symbol is known is because some other header along the chain of includes is doing a forward declaration of QTimer, meaning you can declare QTimer pointers and references, but not actually instantiate it.

Needless to say, you shouldn't depend on even that. Instead, change:

QTimer *timerDisplay;

to:

class QTimer *timerDisplay;

and then #include <QTimer> in the cpp file.

Another problem is that your UpdateDisplay() function is not a slot, even though you're trying to connect a signal to it. So move the declaration of that function to the private slots: section.

Upvotes: 10

Related Questions