2

In the book "C++ GUI Programming with Qt 4" there's a FindDialog Widget example show step by step in Part 1, Chapter 2, now I'm trying to build this example using the Makefile generated by qmake but it complains saying:

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

Using the shell command prompt (on Linux Mint) I've created a directory Hello I've created a project file (with qmake) Hello.pro This is the ls command output in /Hello:

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

I've been trying to figure out the problem for 2 hours now, I would be very grateful of any help thank you.

This is the file containing main :

#include "FindDialog.h"

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

Header file: 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

Implementation File: 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());
  }
eightShirt
  • 1,457
  • 2
  • 15
  • 29
Chedy2149
  • 2,821
  • 4
  • 33
  • 56

2 Answers2

2

Lol4t0's answer solved your problem. You have just to rerun qmake -project.

NG_
  • 6,895
  • 7
  • 45
  • 67
0

In the book on the bottom of the page where main.cpp is created, it addresses this problem.

Classes that use the Q_OBJECT macro must have moc run on them. This isn't a problem because qmake automatically adds the necessary rules to the makefile. But if you forget to regenerate your makefile using qmake and moc isn't run, the linker will complain that some functions are declared but not implemented. The messages can be fairly obscure. GCC produces error messages like this one:

finddialog.o: In function 'FindDialog::tr(char const*, char const*)':/usr/lib/qt/src/corelib/global/qglobal.h:1430: undefined reference to 'FindDialog::staticMetaObject'"

In this case the staticMetaObject being FindDialog(QWidget*).

Visual C++'s output starts like this:

finddialog.obj : error LNK2001: unresolved external symbol "public:~virtual int __this call MyClass::qt_metacall(enum QMetaObject::Call, int, void * *)"

If this ever happens to you, run qmake again to update the makefile, then rebuild the application.