2014-02-14 32 views
0

我想編譯以下使用Qt 4,但我得到的錯誤。我研究了很多來源,但是我一直無法解決這個問題。見下面的代碼。這個程序來自Qt 4教科書。代碼根據他們工作。Qt錯誤:QHBoxLayout未在此範圍內聲明?

.pro文件

HEADERS += \ 
    findDialog.h 

SOURCES += \ 
    findDialog.cpp \ 
    main.cpp 

QT += widgets \ 
     gui 

FindDialog.h

#ifndef FINDDIALOG_H 
#define FINDDIALOG_H 

#include <QDialog> 

class QCheckbox; 
class QLabel; 
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.cpp

#include <QtGui> 
#include "findDialog.h" 
#include <QCheckBox> 

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("Search &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 QString &))); 

    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::CaseSensitivity : Qt::CaseInsensitive; 
    if(backwardCheckBox->isChecked()) { 
     emit findPrevious(text, cs); 
    } else { 
     emit findNext(text, cs); 
    } 
} 

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

Main.cpp的

#include <QApplication> 
#include "findDialog.h" 

int main (int argc, char *argv[]) 
{ 
    QApplication app(argc, argv); 
    FindDialog *dialog = new FindDialog(); 
    dialog->show(); 
    return app.exec(); 
} 

的錯誤如下所示:

finddialog.cpp:25:2: error: ‘QHBoxLayout’ was not declared in this scope 
    QHBoxLayout *topLeftLayout = new QHBoxLayout; 
^
finddialog.cpp:25:15: error: ‘topLeftLayout’ was not declared in this scope 
    QHBoxLayout *topLeftLayout = new QHBoxLayout; 
      ^
finddialog.cpp:25:35: error: expected type-specifier before ‘QHBoxLayout’ 
    QHBoxLayout *topLeftLayout = new QHBoxLayout; 
           ^
finddialog.cpp:25:35: error: expected ‘;’ before ‘QHBoxLayout’ 
finddialog.cpp:29:2: error: ‘QVBoxLayout’ was not declared in this scope 
    QVBoxLayout *leftLayout = new QVBoxLayout; 
^
finddialog.cpp:29:15: error: ‘leftLayout’ was not declared in this scope 
    QVBoxLayout *leftLayout = new QVBoxLayout; 
      ^
finddialog.cpp:29:32: error: expected type-specifier before ‘QVBoxLayout’ 
    QVBoxLayout *leftLayout = new QVBoxLayout; 
           ^
finddialog.cpp:29:32: error: expected ‘;’ before ‘QVBoxLayout’ 
finddialog.cpp:34:15: error: ‘rightLayout’ was not declared in this scope 
    QVBoxLayout *rightLayout = new QVBoxLayout; 
      ^
finddialog.cpp:34:33: error: expected type-specifier before ‘QVBoxLayout’ 
    QVBoxLayout *rightLayout = new QVBoxLayout; 
           ^
finddialog.cpp:34:33: error: expected ‘;’ before ‘QVBoxLayout’ 
finddialog.cpp:39:15: error: ‘mainLayout’ was not declared in this scope 
    QHBoxLayout *mainLayout = new QHBoxLayout; 
      ^
finddialog.cpp:39:32: error: expected type-specifier before ‘QHBoxLayout’ 
    QHBoxLayout *mainLayout = new QHBoxLayout; 
           ^
finddialog.cpp:39:32: error: expected ‘;’ before ‘QHBoxLayout’ 
finddialog.cpp: In member function ‘void FindDialog::findClicked()’: 
finddialog.cpp:50:25: error: invalid use of incomplete type ‘class QLineEdit’ 
    QString text = lineEdit->text(); 
         ^
In file included from finddialog.cpp:3:0: 
finddialog.h:8:7: error: forward declaration of ‘class QLineEdit’ 
class QLineEdit; 
    ^
finddialog.cpp:51:39: error: invalid use of incomplete type ‘class QCheckbox’ 
    Qt::CaseSensitivity cs = caseCheckBox->isChecked() ? Qt::CaseSensitivity 
            ^
In file included from finddialog.cpp:3:0: 
finddialog.h:6:7: error: forward declaration of ‘class QCheckbox’ 
class QCheckbox; 
    ^
finddialog.cpp:52:17: error: expected primary-expression before ‘:’ token 
       : Qt::CaseInsensitive; 
       ^
finddialog.cpp:53:21: error: invalid use of incomplete type ‘class QCheckbox’ 
    if(backwardCheckBox->isChecked()) { 
        ^
In file included from finddialog.cpp:3:0: 
finddialog.h:6:7: error: forward declaration of ‘class QCheckbox’ 
class QCheckbox; 
    ^
finddialog.cpp: In member function ‘void FindDialog::enableFindButton(const QString&)’: 
finddialog.cpp:62:12: error: invalid use of incomplete type ‘class QPushButton’ 
    findButton->setEnabled(!text.isEmpty()); 
      ^
In file included from /opt/QtSDK/Desktop/Qt/4.8.1/gcc/include/QtGui/QDialog:1:0, 
       from finddialog.h:4, 
       from finddialog.cpp:3: 
/opt/QtSDK/Desktop/Qt/4.8.1/gcc/include/QtGui/qdialog.h:53:7: error: forward declaration of ‘class QPushButton’ 
class QPushButton; 
+0

您確定,您使用Qt4。*,是否包括「#include 」幫助?是否包含在您的項目文件「+ = gui」中? – dgrat

+0

沒有沒有幫助。 –

+0

那麼,後可能是qmake文件。 – dgrat

回答

0

我認爲是更好地只包括你要使用的標題。另外,我做了一些修改您的包括:

文件:proyect.pro:

HEADERS += FindDialog.hpp 
SOURCES += FindDialog.cpp Main.cpp 
QT += widgets 

文件:FindDialog.hpp

#ifndef FINDDIALOG_H 
#define FINDDIALOG_H 

#include <QApplication> 
#include <QDialog> 
#include <QLabel> 
#include <QLineEdit> 
#include <QPushButton> 
#include <QCheckBox> 
#include <QHBoxLayout> 
#include <QVBoxLayout> 

# .. rest of your code 
#endif 

文件FindDialog.cpp:

#include "FindDialog.hpp" 
# resto of code 

File Main.cpp:

#include "FindDialog.hpp" 
# .. rest of your code 

以上編譯罰款在我的環境

+0

嘿上述錯誤消失了,但我現在有一個錯誤。 .cpp文件中的第68行與?運營商。 「:」標記之前的預期主表達式。 –

+0

@HellMan在三元運算符之後(即在「?」之後)將'Qt :: CaseSensitivity'改爲'Qt :: CaseSensitive'。 –

2

我剛添加的#include指令到位 「一流」 爲QLabel,QLineEdit的,QPushButton, QCheckBox,並加入#include <QHBoxLayout>#include <QVBoxLayout>。它工作得很好。

我認爲這個問題來自於Qt版本。我使用Qt 5以及更多,我爲Qt 4讀了一本書。