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

bdh*_*har 4 c++ qt

我是C++/Qt的新手

我正在关注Jasmin Blanchette和Mark Summer现场的"C++ GUI Programming with Qt 4"一书.

我正在研究一个示例程序,并且遇到了一些我无法解决的编译错误.代码和错误如下.任何帮助表示赞赏.

谢谢.

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
Run Code Online (Sandbox Code Playgroud)

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());
}
Run Code Online (Sandbox Code Playgroud)

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();
}
Run Code Online (Sandbox Code Playgroud)

以下错误..

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'的可访问基础

Geo*_*che 15

你应该公开地继承QWidgetQDialog:

 class FindDialog : public QDialog {
      // ...
Run Code Online (Sandbox Code Playgroud)

show()实际上是由一个基础实现的FindDialog,QWidget但是你不是公开地继承它,因此无法访问它.

类的继承默认是私有的,即

class A : B {}; 
Run Code Online (Sandbox Code Playgroud)

class A : private B {};
Run Code Online (Sandbox Code Playgroud)

是等价的.