gpt4 book ai didi

c++ - 无法从 "C++ GUI Programming with Qt 4"构建 FindDialog 示例

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

在“C++ GUI Programming with Qt 4”一书中,第 1 部分第 2 章中有一个 FindDialog Widget 示例逐步显示,现在我正在尝试使用 qmake 生成的 Makefile 构建此示例,但它提示说:

g++ -Wl,-O1 -o Hello hello.o    -L/usr/lib/i386-linux-gnu -lQtGui -lQtCore -lpthread 
hello.o: In function `main':
hello.cpp:(.text.startup+0x47): undefined reference to `FindDialog::FindDialog(QWidget*)'
collect2: ld returned 1 exit status
make: *** [Hello] Error 1

使用 shell 命令提示符(在 Linux Mint 上)我创建了一个目录 Hello我创建了一个项目文件(使用 qmake)Hello.pro这是/Hello 中的 ls 命令输出:

FindDialog.cpp  FindDialog.h  hello.cpp  hello.o  Hello.pro  Makefile

我已经尝试找出问题 2 个小时了,如果有任何帮助,我将不胜感激。

这是包含 main 的文件:

#include "FindDialog.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
FindDialog *dialog = new FindDialog();
dialog->show();
return app.exec();
}

头文件:FindDialog.h

#ifndef FIND_DIALOG_H
#define FIND_DIALOG_H
#include <QDialog>
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;

class FindDialog : public QDialog
{
Q_OBJECT

public:
FindDialog(QWidget* parent = 0);
~FindDialog();
signals:
void findNext(const QString &str, Qt::CaseSensitivity cs);
void findPrevious(const QString &str, Qt::CaseSensitivity cs);
private slots:
void findClicked();
void enableFindFunction(const QString &text);
private:
QLabel *label;
QLineEdit *lineEdit;
QCheckBox *caseCheckBox;
QCheckBox *backwardCheckBox;
QPushButton *findButton;
QPushButton *closeButton;
};

#endif

实现文件:FindDialog.cpp

#include <QtGui>
#include "FindDialog.h"

FindDialog::FindDialog(QWidget *parent = 0):QDialog(parent)
{
//Creating components
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"));
//adding event handling
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()));
//setting layout
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);
//window title
setWindowTitle(tr("Find"));
setFixedHeight(sizeHint().height());
}
FindDialog::~FindDialog(){
}
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());
}

最佳答案

Lol4t0 的回答解决了你的问题。您只需重新运行 qmake -project

关于c++ - 无法从 "C++ GUI Programming with Qt 4"构建 FindDialog 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13433387/

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