Results 1 to 10 of 10

Thread: Creating a Dialog in a Slot of QMainWindow

  1. #1
    Join Date
    Aug 2008
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default 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
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include "ui_MainWindow.h"
    6.  
    7. class MainWindow : public QMainWindow {
    8. Q_OBJECT
    9.  
    10. public:
    11. MainWindow(QWidget *parent=0,Qt::WFlags flags=0);
    12.  
    13. public slots:
    14. void createDialog();
    15.  
    16. private:
    17. Ui::MainWindow *ui;
    18. };
    19.  
    20. #endif
    To copy to clipboard, switch view to plain text mode 

    MainWindow.cpp
    Qt Code:
    1. #include "MainWindow.h"
    2. #include "Dialog.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent,Qt::WFlags flags){
    5. ui = new Ui::MainWindow;
    6. ui->setupUi(this);
    7.  
    8. connect(ui->actionCreate,SIGNAL(activated()),this,SLOT(createDialog()));
    9. }
    10.  
    11. void MainWindow::createDialog(){
    12. Dialog *d = new Dialog;
    13. d->exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    Dialog.h
    Qt Code:
    1. #ifndef MYDIALOG_H
    2. #define MYDIALOG_H
    3.  
    4. #include <QDialog>
    5. #include "ui_Dialog.h"
    6.  
    7. class Dialog : public QDialog {
    8. public:
    9. Dialog(QWidget *parent = 0);
    10.  
    11. };
    12.  
    13. #endif
    To copy to clipboard, switch view to plain text mode 

    Dialog.cpp
    Qt Code:
    1. #include "Dialog.h"
    2.  
    3. Dialog::Dialog(QWidget *parent = 0) : QWidget(parent){
    4. Ui::Dialog *ui = new Ui::Dialog;
    5. ui.setupUi(this);
    6. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by tilm; 13th August 2008 at 09:51.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Creating a Dialog in a Slot of QMainWindow

    there is trouble
    Qt Code:
    1. Dialog::Dialog(QWidget *parent = 0)
    To copy to clipboard, switch view to plain text mode 

    should be
    Qt Code:
    1. Dialog::Dialog(QWidget *parent )
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Creating a Dialog in a Slot of QMainWindow

    one more, in this method
    Qt Code:
    1. void MainWindow::createDialog(){
    2. Dialog *d = new Dialog;
    3. d->exec();
    4. }
    To copy to clipboard, switch view to plain text mode 
    you should set parent or delete dialog manually.

  4. #4
    Join Date
    Aug 2008
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default 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?

  5. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Creating a Dialog in a Slot of QMainWindow

    your code
    Qt Code:
    1. #include "Dialog.h"
    2.  
    3. Dialog::Dialog(QWidget *parent = 0) : QWidget(parent){
    4. Ui::Dialog *ui = new Ui::Dialog;
    5. ui.setupUi(this);
    6. }
    To copy to clipboard, switch view to plain text mode 

    must be
    Qt Code:
    1. #include "Dialog.h"
    2.  
    3. Dialog::Dialog(QWidget *parent) : QWidget(parent){
    4. Ui::Dialog *ui = new Ui::Dialog;
    5. ui.setupUi(this);
    6. }
    To copy to clipboard, switch view to plain text mode 

    yup, memory leak.

  6. #6
    Join Date
    Aug 2008
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default 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:


    Qt Code:
    1. class MainWindow : public QMainWindow {
    2.  
    3. public:
    4. MainWindow(QWidget *parent=0,Qt::WFlags flags=0);
    5. void createDialog();
    6.  
    7. private:
    8. Ui::MainWindow *ui;
    9. };
    10.  
    11. MainWindow::MainWindow(QWidget *parent,Qt::WFlags flags){
    12. ui = new Ui::MainWindow;
    13. ui->setupUi(this);
    14. createDialog();
    15. }
    16.  
    17. void MainWindow::createDialog(){
    18. Dialog d;
    19. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by tilm; 13th August 2008 at 11:43. Reason: reformatted to look better

  7. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Creating a Dialog in a Slot of QMainWindow

    can you attach compilable sources?

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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.
    J-P Nurmi

  9. The following user says thank you to jpn for this useful post:

    tilm (13th August 2008)

  10. #9
    Join Date
    Aug 2008
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default 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?
    Last edited by tilm; 13th August 2008 at 17:41. Reason: spelling error

  11. #10
    Join Date
    Aug 2008
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

Similar Threads

  1. Replies: 4
    Last Post: 25th August 2014, 18:05
  2. Problem When Creating my own Slot
    By Fatla in forum Qt Programming
    Replies: 12
    Last Post: 6th June 2008, 14:44
  3. Replies: 3
    Last Post: 18th October 2007, 08:07

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.