预先声明并在Qt

时间:2017-07-15 12:36:24

标签: c++ qt

Qt的新手。 对于" C ++ GUI Qt 4"在一本书中看到,在第二章中,有一段代码,书中没有包含这几个相关的头文件(如),但是使用了一些预先声明,这是一些编译速度更快。但我编译错了:"无效使用不完整类型:"

finddialog.h

#ifndef FINDDIALOG_H
#define FINDDIALOG_H
#include <QDialog>
#include <QHBoxLayout>

//#include <QLabel>
//#include <QCheckBox>
//#include <QLineEdit>
//#include <QPushButton>

class QLabel;
class QCheckBox;
class QLineEdit;
class QPushButton;

class finddialog:public QDialog
{
    Q_OBJECT
public:
    finddialog(QWidget *parent = 0);
signals:
    void findNext(const QString &str,Qt::CaseSensitivity cs);
    void findPrevious(const QString &str,Qt::CaseSensitivity cs);
private slots:
    void findClicked();
    void enableFindButton(const QString &text);
private:
    QLabel *label;
    QLineEdit *lineEdit;
    QCheckBox *caseCheckBox;
    QCheckBox *backwardCheckBox;
    QPushButton *findButton;
    QPushButton *closeButton;
};

#endif // FINDDIALOG_H

finddialog.cpp

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


finddialog::finddialog(QWidget *parent):QDialog(parent)
{
    label = new QLabel(tr("find &what:"));
    lineEdit = new QLineEdit;
    label->setBuddy(lineEdit);

    caseCheckBox = new QCheckBox(tr("Match &case"));
    backwardCheckBox = new QCheckBox(tr("seach &backward"));

    findButton = new QPushButton(tr("&find"));
    findButton->setDefault(true);
    findButton->setEnabled(false);

    closeButton = new QPushButton(tr("close"));

    connect(lineEdit,SIGNAL(textChanged(const QString &)),this,SLOT(enableFindButton(const QSting &)));
    connect(findButton,SIGNAL(clicked()),this,SLOT(findClicked()));
    connect(closeButton,SIGNAL(clicked()),this,SLOT(close()));
    QHBoxLayout *topLeftLayout = new QHBoxLayout;
    topLeftLayout->addWidget(label);
    topLeftLayout->addWidget(lineEdit);
    QVBoxLayout *leftLayout = new QVBoxLayout;
    leftLayout->addLayout(topLeftLayout);
    leftLayout->addWidget(caseCheckBox);
    leftLayout->addWidget(backwardCheckBox);

    QVBoxLayout *rightLayout = new QVBoxLayout;
    rightLayout->addWidget(findButton);
    rightLayout->addWidget(closeButton);
    rightLayout->addStretch();

    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->addLayout(leftLayout);
    mainLayout->addLayout(rightLayout);
    setLayout(mainLayout);
    setWindowTitle(tr("Find:"));
    setFixedHeight(sizeHint().height());
}

void finddialog::findClicked()
{
    QString text = lineEdit->text();
    Qt::CaseSensitivity cs = caseCheckBox->isChecked()?Qt::CaseSensitive:Qt::CaseInsensitive;
    if(backwardCheckBox->isChecked())
    {
        emit findPrevious(text,cs);

    }
    else
    {
        emit findNext(text,cs);
    }

}
void finddialog::enableFindButton(const QString &text)
{
    findButton->setEnabled(!text.isEmpty());
}

enter image description here

1 个答案:

答案 0 :(得分:0)

请注意,默认情况下,Qt定义的类型放在命名空间中,但是头文件执行using namespace指令,因此您大多不需要担心它。但这意味着一个普通的旧前瞻声明与正确的名称不匹配。转发声明属于Qt库的类的正确方法是:

QT_FORWARD_DECLARE_CLASS(QLabel)
QT_FORWARD_DECLARE_CLASS(QCheckBox)
QT_FORWARD_DECLARE_CLASS(QLineEdit)
QT_FORWARD_DECLARE_CLASS(QPushButton)

正如评论中所暗示的,编译器不会让你做new Typeptr->member或其他各种事情,直到它已经看到了类的完整定义,而不仅仅是前瞻性声明因此,无法避免在* .cpp文件中使用包含:

#include <QLabel>
#include <QCheckBox>
#include <QLineEdit>
#include <QPushButton>

现在你的一个* .cpp文件具有它需要的所有依赖项,加上包含* .h文件的其他* .cpp文件,不一定需要花费额外的编译时间来读取这些标题并解析内容