1

I have a little problem :

finddialog.h

#ifndef FINDDIALOG_H
#define FINDDIALOG_H

class QDialog;
class QWidget;
class QLineEdit;
class QPushButton;
class QCheckBox;
class QLabel;

class FindDialog : public QDialog
{
    Q_OBJECT


public:
    FindDialog(QWidget *parent);

private slots:
    void findClicked();
    void enableFindButton(const QString &text);

signals :
    void findNext(const QString &str, Qt::CaseSensitivity cs);
    void findPrev(const QString &str, Qt::CaseSensitivity cs);

private:
    QLabel *findLabel;
    QLineEdit *textEdit;
    QCheckBox *caseCheckBox;
    QCheckBox *backwCheckBox;
    QPushButton *findButton;
    QPushButton *closeButton;

};

#endif // FINDDIALOG_H

finddialog.c

#include <QtWidgets>
#include "finddialog.h"


FindDialog::FindDialog(QWidget *parent) : QDialog(parent)
{
    QPushButton *button = new QPushButton(this);
}

void FindDialog::findClicked()
{

}

void FindDialog::enableFindButton(const QString &text)
{

}

I receive a QDialog/QPushButton invalid use of incomplete type error.

I have already included QtWidget in .cpp file so why is it not working?

LogicStuff
  • 19,397
  • 6
  • 54
  • 74
KarimS
  • 3,812
  • 9
  • 41
  • 64

2 Answers2

5

(Forward) declaration is not enough for a base-class type. You have to provide the definition (make QDialog a complete type), so:

#include <QDialog>

in the header file. See Forward Declaration of a Base Class.

Based on the code you posted, I don't know where the error about QPushButton being incomplete comes from (if you #include <QtWidgets>, of course).

If you do not #include <QtWidgets> (which has #include <QPushButton>, etc...), you cannot instantiate any of these incomplete types, like so:

new QPushButton(this);

That again, requires a definition (What's the size of the memory QPushButton needs? What constructor to call? That's known only if there's a definition in that compilation unit - .cpp file).

Community
  • 1
  • 1
LogicStuff
  • 19,397
  • 6
  • 54
  • 74
  • For the sake of clarity, it is not a declaration, it's a forward declaration. – skypjack Jan 01 '16 at 19:20
  • @skypjack "forward" just indicates the usage, right? It's still a declaration. – LogicStuff Jan 01 '16 at 19:23
  • @LogicStuff *For the sake of clarity* – Acha Bill Jan 01 '16 at 19:23
  • @LogicStuff Absolutely, it is called so because of the use we do of it. See [here](http://en.cppreference.com/w/cpp/language/class#Forward_declaration) for further details. – skypjack Jan 01 '16 at 19:25
  • thank you, another think learned about c++, but i don't know why i have error for QPushButton and any other Qt type i want to use, but when i include in the header file everything work fine – KarimS Jan 01 '16 at 19:32
0

By the forward declaration of QPushButton and others in finddialog.h, the compiler expects to find their constructors and other methods in some linked files.
#include the required header files to get it working.

Acha Bill
  • 1,255
  • 1
  • 7
  • 20
  • @jafar there is no inline method derefencing the pointer in header file, and in source file i have included – KarimS Jan 01 '16 at 19:30