Creating a Dialog in a Slot of QMainWindow
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
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "ui_MainWindow.h"
Q_OBJECT
public:
MainWindow
(QWidget *parent
=0,Qt
::WFlags flags
=0);
public slots:
void createDialog();
private:
Ui::MainWindow *ui;
};
#endif
MainWindow.cpp
Code:
#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(createDialog()));
}
void MainWindow::createDialog(){
Dialog *d = new Dialog;
d->exec();
}
Dialog.h
Code:
#ifndef MYDIALOG_H
#define MYDIALOG_H
#include <QDialog>
#include "ui_Dialog.h"
public:
};
#endif
Dialog.cpp
Code:
#include "Dialog.h"
Ui::Dialog *ui = new Ui::Dialog;
ui.setupUi(this);
}
Re: Creating a Dialog in a Slot of QMainWindow
there is trouble
Code:
Dialog
::Dialog(QWidget *parent
= 0)
should be
Re: Creating a Dialog in a Slot of QMainWindow
one more, in this method
Code:
void MainWindow::createDialog(){
Dialog *d = new Dialog;
d->exec();
}
you should set parent or delete dialog manually.
Re: Creating a Dialog in a Slot of QMainWindow
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?
Re: Creating a Dialog in a Slot of QMainWindow
your code
Code:
#include "Dialog.h"
Ui::Dialog *ui = new Ui::Dialog;
ui.setupUi(this);
}
must be
Code:
#include "Dialog.h"
Ui::Dialog *ui = new Ui::Dialog;
ui.setupUi(this);
}
yup, memory leak.
Re: Creating a Dialog in a Slot of QMainWindow
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:
Code:
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;
}
Re: Creating a Dialog in a Slot of QMainWindow
can you attach compilable sources?
Re: Creating a Dialog in a Slot of QMainWindow
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.
Re: Creating a Dialog in a Slot of QMainWindow
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?
Re: Creating a Dialog in a Slot of QMainWindow
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.