我有一个小问题:
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)
{
}
我收到一个 QDialog
/QPushButton
invalid use of incomplete type 错误。
我已经将 QtWidget
包含在 .cpp
文件中,为什么它不起作用?
最佳答案
(Forward) 声明对于基类类型是不够的。您必须提供定义(使 QDialog
成为完整类型),因此:
#include <QDialog>
在头文件中。参见 Forward Declaration of a Base Class .
根据您发布的代码,我不知道关于 QPushButton
的错误在哪里不完整来自(如果你是 #include <QtWidgets>
,当然)。
如果你不#include <QtWidgets>
(其中有 #include <QPushButton>
等...),您不能实例化任何这些不完整的类型,如下所示:
new QPushButton(this);
这又需要一个定义(QPushButton
需要的内存大小是多少?要调用什么构造函数?只有在该编译单元 - .cpp
文件中有定义时才知道)。
关于c++ - 不完整类型 Qt 的无效使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34559153/