Qt的新功能。 對於一本書中的「C++ GUI Qt 4」,在第二章中看到一段代碼,書中沒有包括這幾個相關的頭文件(例如),但是使用了一些前置聲明,這個是一些更快的彙編。可是我錯了編譯: 「無效的使用不完全類型的:」預先聲明並在Qt中包含頭文件/ bese
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());
}
在你的cpp文件中,你需要包含, ......基本上你所有的頭部註釋掉的'Qt'頭文件都會將這些頭文件包含在你的cpp文件中。 –
drescherjm
你的意思是添加頭文件(#include)還是預聲明(QLabel類)? –
我只懂英語,也許有點西班牙語。抱歉。 – drescherjm