PDA

View Full Version : Qt 4.6.0: Opening a dialog from a main window



dmginc
14th January 2010, 11:44
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 (http://doc.trolltech.com/4.3/uitools-multipleinheritance.htmlitools-multipleinheritance.html)) for my main window:

MainWindow.h (form has ObjectName=MainWindow in Qt Designer)


#include "ui_MainWindow.h"
class MainWin : public QMainWindow, private Ui::MainWindow
{
Q_OBJECT

public:
MainWin(QWidget *parent = 0);

// etc.
}


MainWindow.cpp


#include <QtGui>
#include "MainWindow.h"
#include "CameraConnectDialog.h"
MainWin::MainWin(QWidget *parent) : QMainWindow(parent)
{
setupUi(this);

// etc.
}

main.cpp


#include <QApplication>
#include "MainWindow.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWin mainwindow;
mainwindow.show();
return app.exec();
}

CameraConnectDialog.h (form has ObjectName=CameraConnectDialog in Qt Designer)

#include "ui_CameraConnectDialog.h"

class CameraConnectDialog : public QDialog
{
Q_OBJECT

public:
CameraConnectDialog(QWidget *parent = 0);
};


CameraConnectDialog.cpp

#include <QtGui>
#include "CameraConnectDialog.h"

CameraConnectDialog::CameraConnectDialog(QWidget *parent) : QDialog(parent)

{
// code to go here
}

I then attempt to open this dialog using the following in one of the member functions of class MainWin (in MainWindow.cpp):

CameraConnectDialog dialog(this);
dialog.exec();

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!!!:D

jpn
14th January 2010, 12:21
Did you forget to inherit the Ui class and call setupUi() in CameraConnectDialog?

dmginc
14th January 2010, 12:48
I think that may be the problem!

So I need:

CameraConnectDialog.cpp


#include <QtGui>
#include "CameraConnectDialog.h"

CameraConnectDialog::CameraConnectDialog(QWidget *parent) : QDialog(parent)
{
setupUi();
}

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

Thanks!

dmginc
14th January 2010, 13:16
Thanks for the tip! :D Got it working with the following:

CameraConnectDialog.cpp

#include <QtGui>
#include "CameraConnectDialog.h"

CameraConnectDialog::CameraConnectDialog(QWidget *parent) : QDialog(parent)

{
setupUi(this);
}



#include "ui_CameraConnectDialog.h"


CameraConnectDialog.h
class CameraConnectDialog : public QDialog, private Ui::CameraConnectDialog
{
Q_OBJECT
public:
CameraConnectDialog(QWidget *parent = 0);
};