I'm making a little GUI program using Qt creator, but I get stuck with two "unresolved externals" errors. Here are all of my code: (By the way, I following the book C++ GUI Programming with Qt4, second edition. This are covered in chapter 2)
I got five files in total: (The code are kinda long, so you can skip some and go directly to the end for my explanation.)
First, mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
finddialog.h:
#ifndef FINDDIALOG_H
#define FINDDIALOG_H
#include<QDialog>
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;
class FindDialog:public 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 enableFindbuton(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=0)
{
label=new QLabel(tr("Find %what"));
lineEdit=new lineEdit;
label->setBuddy(lineEdit);
caseCheckBox=new QCheckBox(tr("Match &case"));
backwardCheckBox=new QCheckBox(tr("search &backward"));
findButton=new QPushButton("&Find");
findButton->setDefault(true);
findButton->setDefault(false);
closeButton=new QPushButton(tr("close"));
connect(lineEdit,SIGNAL(textchanged(const QString &)),
this,SLOT(enableFindbuton(const 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(); //what the hell is this function?
QHBoxLayout* mainlayout=new QHBoxLayout;
mainlayout->addLayout(leftLayout);
mainLayout->addLayout(rightLayout);
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::enableFindbuton(const QString &text)
{
findButton->setEnabled(!text.isEmpty());
}
main.cpp:
#include"mainwindow.h"
#include<QApplication>
#include"finddialog.h"
class FindDialog;
int main(int argc,char*argv[])
{
QApplication app(argc,argv);
MainWindow w;
w.show();
FindDialog* dialog=new FindDialog;
dialog->show();
return app.exec();
}
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QObject>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
I am making a dialog window exactly like this:
so that is the whole main structure of my code. Hope you can understand it.
I'm so sorry that it is that long.
I didn't use the "qmake" and "make" as the author did in the book. I directly use the Qt creator. And then the whole thing mess up.... The two error messages look like this:
main.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl FindDialog::FindDialog(class QWidget *)" (??0FindDialog@@QEAA@PEAVQWidget@@@Z) referenced in function main
debug\test2.exe:-1: error: LNK1120: 1 unresolved externals