问题描述
我有一个小问题:
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
无效使用的不完整类型错误.
I receive a QDialog
/QPushButton
invalid use of incomplete type error.
我已经在.cpp
文件中包含了QtWidget
,为什么它不起作用?
I have already included QtWidget
in .cpp
file so why is it not working?
推荐答案
(转发)声明对于基类类型是不够的.您必须提供定义(使QDialog
为完整类型),所以:
(Forward) declaration is not enough for a base-class type. You have to provide the definition (make QDialog
a complete type), so:
#include <QDialog>
头文件中的
.参见基类的前向声明.
根据您发布的代码,我不知道关于QPushButton
不完整的错误来自何处(当然,如果您是#include <QtWidgets>
).
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).
如果不使用#include <QtWidgets>
(具有#include <QPushButton>
等),则无法实例化这些不完整类型中的任何一种,如下所示:
If you do not #include <QtWidgets>
(which has #include <QPushButton>
, etc...), you cannot instantiate any of these incomplete types, like so:
new QPushButton(this);
这又需要一个定义(QPushButton
需要的内存大小是多少?要调用什么构造函数?只有在该编译单元-.cpp
文件中有定义的情况下才知道.)
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).
这篇关于无效使用Qt类型不完整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!