gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:01:26 26 4
gpt4 key购买 nike

我是 C++/Qt 的新手

我正在阅读 Jasmin Blanchette 和 Mark Summerfield 合着的“C++ GUI Programming with Qt 4”一书。

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

谢谢。

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

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

主要.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: In function 'int qMain(int, char**)': c:/Qt/2010.02.1/qt/include/QtGui/../../src/gui/kernel/qwidget.h:485: error: 'void QWidget::show()' is inaccessible main.cpp:7: error: within this context main.cpp:7: error: 'QWidget' is not an accessible base of 'FindDialog'

最佳答案

您应该从QWidgetQDialog 公开继承:

 class FindDialog : public QDialog {
// ...

show() 实际上是由 FindDialogQWidget 的基础实现的 - 但您没有从它公开继承,因此不能'访问它。

类的继承默认是私有(private)的,即

class A : B {}; 

class A : private B {};

是等价的。

关于c++ - 使用Qt编译测试程序时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2320426/

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