4
votes
  1. I created a GUI Application -> QMainWindow
  2. I added 1 item to the menu + the slot.
  3. I created a new item -> QDialog
  4. I the slot method i try to show the created dialog but i get this errors:

    mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl EditStudentDialog::EditStudentDialog(class QWidget *)" (??0EditStudentDialog@@QEAA@PEAVQWidget@@@Z) referenced in function "private: void __cdecl MainWindow::on_actionNew_triggered(void)" (?on_actionNew_triggered@MainWindow@@AEAAXXZ)

    mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: virtual __cdecl EditStudentDialog::~EditStudentDialog(void)" (??1EditStudentDialog@@UEAA@XZ) referenced in function "private: void __cdecl MainWindow::on_actionNew_triggered(void)" (?on_actionNew_triggered@MainWindow@@AEAAXXZ)

This is the main window:

#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 slots:
    void on_actionNew_triggered();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "editstudentdialog.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actionNew_triggered()
{
    EditStudentDialog editDialog;
    editDialog.setModal(true);
    editDialog.exec();
}

This is the dialog ( just an empty one, no controls on it ):

#ifndef EDITSTUDENTDIALOG_H
#define EDITSTUDENTDIALOG_H

#include <QDialog>

namespace Ui {
class EditStudentDialog;
}

class EditStudentDialog : public QDialog
{
    Q_OBJECT

public:
    explicit EditStudentDialog(QWidget *parent = 0);
    ~EditStudentDialog();

private:
    Ui::EditStudentDialog *ui;
};

#endif // EDITSTUDENTDIALOG_H


#include "editstudentdialog.h"
#include "ui_editstudentdialog.h"

EditStudentDialog::EditStudentDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::EditStudentDialog)
{
    ui->setupUi(this);
}

EditStudentDialog::~EditStudentDialog()
{
    delete ui;
}

What am I doing wrong?

EDIT: This is the .pro file

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = GUI1
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp \
    editstudentdialog.cpp

HEADERS  += mainwindow.h \
    editstudentdialog.h

FORMS    += mainwindow.ui \
    editstudentdialog.ui

PS: I tried to clean the project and then build it but still the same issue.

EDIT 2: I am using Qt Creator 2.7 with Qt 5.0.2

7

7 Answers

6
votes

It seems that editstudentdialog.obj file is not created properly. Try to clean the project (Build->Clean all) and the build it again. If it does not help check if editstudentdialog.cpp is added to the SOURCES variable in your .pro file. If still nothing happens please provide the .pro file.

EDIT: As was mentioned below, you may also try to delete the whole app and create it again.

21
votes

Solution

  1. Right click on project > Clean

  2. Right click on project > Run qmake

  3. Right click on project > Build

  4. Run - worked first time

Why it works

The reason this worked is because Run qmake updates your Makefile. For some reason qt is not automatically updating paths when you make changes to your project such as adding or removing files. Running qmake forces qt to update the paths for your project which enables it to find the mainwindow.obj file. You probably could just run qmake and your problem would be solved.

6
votes

I just experienced the same issue. "Build All", "Rebuild All", "Clean All" did not help me to solve the "unresolved external symbol" problem.

However, after I have completely deleted the build directory (in my case C:\dev\projects\qt\build-qt-gui-test-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug) the rebuild worked as a charm.

Hope this help you, vladimir

3
votes

I just wanted to add this answer for anyone else (mainly noobs) who had to create an entirely new project to resolve the issue:

I had an extra slot definition in my headers file which prevented the entire application from running, and gave me errors pointing to a function which was not the problem. FYI.

1
votes

Another reason why this may occur is because the .pro file references something that is nonexistent. It happened to me and once I removed those references, the errors were gone.

1
votes

In my case the fix was to add a Q_OBJECT macro in the beginning of the QObject-deriving class that was mentioned in the error messages. I don't know why I didn't get messages that more specifically pointed me to the missing macro.

0
votes

another reason this happens is if your function it's referencing is declared as an inline function in the header