Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

c++ - Invalide use of incomplete type Qt

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

(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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...