问题
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?
回答1:
(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).
回答2:
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.
来源:https://stackoverflow.com/questions/34559153/invalide-use-of-incomplete-type-qt