gpt4 book ai didi

c++ - Qt 错误 : QHBoxLayout was not declaredin this scope?

转载 作者:行者123 更新时间:2023-11-28 02:56:51 25 4
gpt4 key购买 nike

我正在尝试使用 Qt 4 编译以下内容,但出现错误。我研究了很多资源,但我无法解决问题。请参阅下面的代码。这个程序来自Qt 4教科书。代码根据它们工作。

.pro文件

HEADERS += \
findDialog.h

SOURCES += \
findDialog.cpp \
main.cpp

QT += widgets \
gui

查找对话框

#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

查找对话框.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;

最佳答案

我刚刚添加了 #include指令代替 QLabel、QLineEdit、QPushButton 的“类”,QCheckBox 并添加 #include <QHBoxLayout>#include <QVBoxLayout> .它工作得很好。

我觉得问题出在Qt版本上。我使用 Qt 5 及更多,我读了一本关于 Qt 4 的书。

关于c++ - Qt 错误 : QHBoxLayout was not declaredin this scope?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21787304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com