PDA

View Full Version : Creating a Dialog in a Slot of QMainWindow



tilm
13th August 2008, 09:45
Hello,

I created a MainWindow and a very simple Dialog using the Qt Designer. I want to use both components with a single inheritance approach. In a Slot of the MainWindow (connected to an Action of the MainWindow) I want to create and execute an instance of the Dialog class. But when I try to compile the programm there is the error:

release\MainWindow.o(.text+0x28f):MainWindow.cpp: undefined reference to 'Dialog::Dialog(QWidget*)'

I think the proplem might be connected with the fact that I want to create the Dialog in a class derived from QMainWindow. In my project I previously created the Dialog in a Slot of a QAbstractTableModel and it worked fine. Than I decided that this class wasn't the right place to do this and wanted to create the Dialog in a slot of the MainWindow instead ending up with this error.

Here are the files of a minimal example:

MainWindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "ui_MainWindow.h"

class MainWindow : public QMainWindow {
Q_OBJECT

public:
MainWindow(QWidget *parent=0,Qt::WFlags flags=0);

public slots:
void createDialog();

private:
Ui::MainWindow *ui;
};

#endif


MainWindow.cpp


#include "MainWindow.h"
#include "Dialog.h"

MainWindow::MainWindow(QWidget *parent,Qt::WFlags flags){
ui = new Ui::MainWindow;
ui->setupUi(this);

connect(ui->actionCreate,SIGNAL(activated()),this,SLOT(createD ialog()));
}

void MainWindow::createDialog(){
Dialog *d = new Dialog;
d->exec();
}


Dialog.h


#ifndef MYDIALOG_H
#define MYDIALOG_H

#include <QDialog>
#include "ui_Dialog.h"

class Dialog : public QDialog {
public:
Dialog(QWidget *parent = 0);

};

#endif


Dialog.cpp


#include "Dialog.h"

Dialog::Dialog(QWidget *parent = 0) : QWidget(parent){
Ui::Dialog *ui = new Ui::Dialog;
ui.setupUi(this);
}

spirit
13th August 2008, 10:14
there is trouble


Dialog::Dialog(QWidget *parent = 0)


should be


Dialog::Dialog(QWidget *parent )

spirit
13th August 2008, 10:15
one more, in this method


void MainWindow::createDialog(){
Dialog *d = new Dialog;
d->exec();
}

you should set parent or delete dialog manually.

tilm
13th August 2008, 10:43
Thanks for your answers but the mistakes you pointed out do not seem to be the crucial ones. I still get the same compiler error. And obviously the compiler stops even before it recognizes the second specification of a default argument in the Dialog constructor because I did not get an error for this.

The second mistake would 'only' cause a memory leak and no compiler error, right?

spirit
13th August 2008, 10:49
your code


#include "Dialog.h"

Dialog::Dialog(QWidget *parent = 0) : QWidget(parent){
Ui::Dialog *ui = new Ui::Dialog;
ui.setupUi(this);
}


must be


#include "Dialog.h"

Dialog::Dialog(QWidget *parent) : QWidget(parent){
Ui::Dialog *ui = new Ui::Dialog;
ui.setupUi(this);
}


yup, memory leak.

tilm
13th August 2008, 11:41
Yes, I corrected the mistakes in the way you suggested. But I still get the same compiler error I mentioned above in my first post:

release\MainWindow.o(.text+0x28f):MainWindow.cpp: undefined reference to 'Dialog::Dialog(QWidget*)'

The problem must be somewhere else and I suspect that I am not aware of a property of QMainWindow that prevents me to create an instance of a Dialog inside its member functions. Remember: the same code works in member functions of classes that inherit other QObjects.

I reduced the MainWindow class even further and still get the same error:



class MainWindow : public QMainWindow {

public:
MainWindow(QWidget *parent=0,Qt::WFlags flags=0);
void createDialog();

private:
Ui::MainWindow *ui;
};

MainWindow::MainWindow(QWidget *parent,Qt::WFlags flags){
ui = new Ui::MainWindow;
ui->setupUi(this);
createDialog();
}

void MainWindow::createDialog(){
Dialog d;
}

spirit
13th August 2008, 13:37
can you attach compilable sources?

jpn
13th August 2008, 15:00
Do you have dialog.cpp in the .pro file? If not, that would explain why you didn't get compilation errors but managed to pass to the linking stage.

tilm
13th August 2008, 17:38
Thanks jpn, this was the problem. I always thought that

qmake -project

was sufficient to create a complete .pro file (I'm sure that I ran it before qmake and make). For me this was the first time that something in a .pro file created with qmake was missing. Do you know the reason for this?

tilm
13th August 2008, 18:07
I think I found the answer on my own: 'qmake -project' only includes dialog.cpp into the .pro file when dialog.h is included in MainWindow.h . But I first included the Dialog header file in MainWindow.cpp. That was the reason.