【问题标题】:Errors while compiling a test program using Qt使用 Qt 编译测试程序时出错
【发布时间】:2011-01-20 04:43:00
【问题描述】:

我对 C++/Qt 很陌生

我正在关注 Jasmin Blanchette 和 Mark Summerfield 所著的“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

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());
}

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();
}

以下错误..

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' 不是一个可访问的基础 '查找对话框'

【问题讨论】:

    标签: c++ qt


    【解决方案1】:

    您应该从QWidgetQDialog 公开继承:

     class FindDialog : public QDialog {
          // ...
    

    show() 实际上是由FindDialogQWidget 的基实现的——但您没有从它公开继承,因此无法访问它。

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

    class A : B {}; 
    

    class A : private B {};
    

    是等价的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多