Results 1 to 4 of 4

Thread: Qt 4.6.0: Opening a dialog from a main window

  1. #1
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Qt 4.6.0: Opening a dialog from a main window

    Hi all!

    I recently started using Qt 4.6.0 to create a GUI for a computer vision program I previously coded in C...
    Everything is going well - I've created my main window and that works perfectly!

    All I want to do now is be able to open a dialog (created in Qt Designer) from this main window.

    I've used the multiple inheritance approach (as described here) for my main window:

    MainWindow.h (form has ObjectName=MainWindow in Qt Designer)
    Qt Code:
    1. #include "ui_MainWindow.h"
    2. class MainWin : public QMainWindow, private Ui::MainWindow
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. MainWin(QWidget *parent = 0);
    8.  
    9. // etc.
    10. }
    To copy to clipboard, switch view to plain text mode 

    MainWindow.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include "MainWindow.h"
    3. #include "CameraConnectDialog.h"
    4. MainWin::MainWin(QWidget *parent) : QMainWindow(parent)
    5. {
    6. setupUi(this);
    7.  
    8. // etc.
    9. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "MainWindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7. MainWin mainwindow;
    8. mainwindow.show();
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    CameraConnectDialog.h
    (form has ObjectName=CameraConnectDialog in Qt Designer)
    Qt Code:
    1. #include "ui_CameraConnectDialog.h"
    2.  
    3. class CameraConnectDialog : public QDialog
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. CameraConnectDialog(QWidget *parent = 0);
    9. };
    To copy to clipboard, switch view to plain text mode 

    CameraConnectDialog.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include "CameraConnectDialog.h"
    3.  
    4. CameraConnectDialog::CameraConnectDialog(QWidget *parent) : QDialog(parent)
    5.  
    6. {
    7. // code to go here
    8. }
    To copy to clipboard, switch view to plain text mode 

    I then attempt to open this dialog using the following in one of the member functions of class MainWin (in MainWindow.cpp):
    Qt Code:
    1. CameraConnectDialog dialog(this);
    2. dialog.exec();
    To copy to clipboard, switch view to plain text mode 

    I get no compilation errors with this but when the above dialog.exec() is invoked, a blank dialog appears as opposed to the one I actually created...

    My .pro file also contains all the appropriate forms and headers.

    Are there any mistakes in the way I've incorporated my dialog into my main window?
    I'm pulling my hair out over this and would really appreciate if someone could point my in the right direction!

    Thanks!!!

  2. #2
    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: Qt 4.6.0: Opening a dialog from a main window

    Did you forget to inherit the Ui class and call setupUi() in CameraConnectDialog?
    J-P Nurmi

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

    dmginc (14th January 2010)

  4. #3
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt 4.6.0: Opening a dialog from a main window

    I think that may be the problem!

    So I need:

    CameraConnectDialog.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include "CameraConnectDialog.h"
    3.  
    4. CameraConnectDialog::CameraConnectDialog(QWidget *parent) : QDialog(parent)
    5. {
    6. setupUi();
    7. }
    To copy to clipboard, switch view to plain text mode 

    How would I go about inheriting the Ui class though? (I'm kinda new to OOP...)

    Thanks!

  5. #4
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt 4.6.0: Opening a dialog from a main window

    Thanks for the tip! Got it working with the following:

    CameraConnectDialog.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include "CameraConnectDialog.h"
    3.  
    4. CameraConnectDialog::CameraConnectDialog(QWidget *parent) : QDialog(parent)
    5.  
    6. {
    7. setupUi(this);
    8. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "ui_CameraConnectDialog.h"
    2.  
    3. [B]
    4. CameraConnectDialog.h[/B]
    5. class CameraConnectDialog : public QDialog, private Ui::CameraConnectDialog
    6. {
    7. Q_OBJECT
    8. public:
    9. CameraConnectDialog(QWidget *parent = 0);
    10. };
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QInput dialog is always opening twice
    By phillip_Qt in forum Qt Programming
    Replies: 3
    Last Post: 13th November 2009, 12:46
  2. Replies: 11
    Last Post: 11th August 2008, 09:14
  3. Opening a Dialog from a MainWindow FileMenu
    By nbkhwjm in forum Newbie
    Replies: 4
    Last Post: 17th April 2007, 12:24
  4. Replies: 15
    Last Post: 23rd March 2007, 16:16
  5. Crash during sending list from dialog to main window
    By Djony in forum Qt Programming
    Replies: 5
    Last Post: 23rd November 2006, 19:43

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.