C++ 使用Qt编译测试程序时出错

C++ 使用Qt编译测试程序时出错,c++,qt,C++,Qt,我对C++/Qt相当陌生 我正在学习Jasmin Blanchette和Mark Summer Field的《使用Qt 4进行C++GUI编程》一书 我正在编写一个示例程序,遇到了一些无法解决的编译错误。下面是代码和错误。感谢您的帮助 谢谢 finddialog.h #ifndef FINDDIALOG_H #define FINDDIALOG_H #include <QDialog> class QCheckBox; class QLabel; class QLineEdit;

我对C++/Qt相当陌生

我正在学习Jasmin Blanchette和Mark Summer Field的《使用Qt 4进行C++GUI编程》一书

我正在编写一个示例程序,遇到了一些无法解决的编译错误。下面是代码和错误。感谢您的帮助

谢谢

finddialog.h

#ifndef FINDDIALOG_H
#define FINDDIALOG_H

#include <QDialog>

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

class FindDialog : 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
\ifndef FINDDIALOG\H
#定义FINDDIALOG_H
#包括
QCheckBox类;
类QLabel;
类QLineEdit;
类按钮;
类QWidget;
类FindDialog:QDialog
{
Q_对象
公众:
FindDialog(QWidget*parent=0);
信号:
void findNext(常量QString&str,Qt::CaseSensitivity cs);
void findPrevious(const QString&str,Qt::CaseSensitivity cs);
专用插槽:
void findClicked();
void enableFindButton(常量QString&text);
私人:
QLabel*标签;
QLineEdit*lineEdit;
QCheckBox*案例复选框;
QCheckBox*backwardCheckBox;
QPushButton*findButton;
QPushButton*关闭按钮;
};
#endif//FINDDIALOG\u 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("Search &backward"));

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

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

    connect(lineEdit, SIGNAL(textChanged(QString)), this, SLOT(enableFindButton(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::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());
}
#包括
#包括“finddialog.h”
FindDialog::FindDialog(QWidget*parent):QDialog(parent)
{
label=新的QLabel(tr(“Find&what:”);
lineEdit=新的QLineEdit;
标签->setBuddy(行编辑);
caseCheckBox=新的QCheckBox(tr(“匹配和案例”);
后退复选框=新的QCheckBox(tr(“搜索和后退”);
findButton=newqpushbutton(tr(“&Find”);
findButton->setDefault(true);
findButton->setEnabled(false);
closeButton=新的QPushButton(tr(“关闭”));
连接(lineEdit,SIGNAL(textChanged(QString)),此,插槽(enableFindButton(QString));
连接(findButton,SIGNAL(clicked()),此,插槽(findClicked());
连接(关闭按钮、信号(单击())、此、插槽(关闭());
QHBoxLayout*topLeftLayout=新的QHBoxLayout;
topLeftLayout->addWidget(标签);
topLeftLayout->addWidget(lineEdit);
QVBoxLayout*leftLayout=新的QVBoxLayout;
leftLayout->addLayout(topLeftLayout);
leftLayout->addWidget(caseCheckBox);
leftLayout->addWidget(backwardCheckBox);
QVBoxLayout*rightLayout=新的QVBoxLayout;
rightLayout->addWidget(findButton);
rightLayout->addWidget(关闭按钮);
rightLayout->addStretch();
QHBoxLayout*mainLayout=新的QHBoxLayout;
主布局->添加布局(左布局);
主布局->添加布局(右布局);
设置布局(主布局);
setWindowTitle(tr(“查找”);
setFixedHeight(sizeHint().height());
}
void FindDialog::findClicked()
{
QString text=lineEdit->text();
案例敏感性=
caseCheckBox->isChecked()?Qt::区分大小写
:Qt::不区分大小写;
如果(backwardCheckBox->isChecked()){
发射findPrevious(文本,cs);
}否则{
发出findNext(文本,cs);
}
}
void FindDialog::enableFindButton(常量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.h”
int main(int argc,char*argv[])
{
QApplication应用程序(argc、argv);
FindDialog*对话框=新建FindDialog;
对话框->显示();
返回app.exec();
}
下面的错误

c:/Qt/2010.02.1/Qt/include/QtGui/./../src/gui/kernel/qwidget.h: 在函数“int qMain(int,char**)”中: c:/Qt/2010.02.1/Qt/include/QtGui/../../src/gui/kernel/qwidget.h:485: 错误:“void QWidget::show()”无效 无法访问main.cpp:7:错误:在 此上下文main.cpp:7:错误: “QWidget”不是可访问的 “FindDialog”


您应该从
QWidget
QDialog
公开继承:

 class FindDialog : public QDialog {
      // ...
show()
实际上是由
FindDialog
QWidget
的一个基础实现的,但是您不是从它公开继承的,因此无法访问它

类的继承在默认情况下是私有的,即

class A : B {}; 

它们是等价的

class A : private B {};