[Qt-interest] Multiple form classes in a main window with Qt creator

Stan Warford Stan.Warford at pepperdine.edu
Mon Apr 20 02:06:22 CEST 2009


Hello list,

I want to use Qt creator to

1. create a main window with Qt creator with placeholders for each  
pane in the main window,

2. create each pane with Qt creator as a "Qt Designer Form Class"

3. programatically integrate each form class into the main window, so  
I can

4. organize my app with signals and slots to communicate between the  
classes associated with the panes.

To test out the idea, I created a main window with Qt creator /  
designer as follows:

MainWindowClass (QMainWindow)
     centralWidget (QWidget)
         splitter (QSplitter)
             topQWidget (QWidget)
             bottomQWidget (QWidget)

and a form class with Qt creator / designer as follows:

Form (QWidget)
     label (QLabel)
     plainTextEdit (QPlainTextEdit)

Creator produces topform.h as:

=====================
#ifndef TOPFORM_H
#define TOPFORM_H
#include <QtGui/QWidget>
namespace Ui {
     class TopForm;
}
class TopForm : public QWidget {
     Q_OBJECT
     Q_DISABLE_COPY(TopForm)
public:
     explicit TopForm(QWidget *parent = 0);
     virtual ~TopForm();
private:
     Ui::TopForm *m_ui;
};
#endif // TOPFORM_H
=====================

and topform.cpp as:

=====================
#include "topform.h"
#include "ui_topform.h"
TopForm::TopForm(QWidget *parent) :
     QWidget(parent),
     m_ui(new Ui::TopForm)
{
     m_ui->setupUi(this);
}
TopForm::~TopForm()
{
     delete m_ui;
}
=====================

To programmatically link the two, I added a few lines to Qt creator's  
mainwindow.h as follows:

=====================
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
#include "topform.h"
namespace Ui
{
     class MainWindowClass;
}
     class MainWindow : public QMainWindow
{
     Q_OBJECT
public:
     MainWindow(QWidget *parent = 0);
     ~MainWindow();
private:
     Ui::MainWindowClass *ui;
     TopForm *topForm;
};
#endif // MAINWINDOW_H
=====================

and a few lines to Qt creator's mainwindow.cpp as follows:

=====================
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
     : QMainWindow(parent), ui(new Ui::MainWindowClass)
{
     ui->setupUi(this);
     topForm = new TopForm(ui->splitter);
}
MainWindow::~MainWindow()
{
     delete ui;
}
=====================

Before I add "label" and "plainTextEdit" the project compiles and  
behaves as expected. But, once I put those widgets in TopForm I get  
the following compile errors:

topform.cpp: In constructor 'TopForm::TopForm(QWidget*)':
topform.cpp:6: error: invalid use of undefined type 'struct Ui::TopForm'
topform.h:7: error: forward declaration of 'struct Ui::TopForm'
topform.cpp:8: error: invalid use of undefined type 'struct Ui::TopForm'
topform.h:7: error: forward declaration of 'struct Ui::TopForm'
topform.cpp: In destructor 'virtual TopForm::~TopForm()':
topform.cpp:13: warning: possible problem detected in invocation of  
delete operator:
topform.cpp:13: warning: invalid use of undefined type 'struct  
Ui::TopForm'
topform.h:7: warning: forward declaration of 'struct Ui::TopForm'
topform.cpp:13: note: neither the destructor nor the class-specific  
operator delete will be called, even if they are declared when the  
class is defined.
make[1]: *** [debug/topform.o] Error 1
make[1]: Leaving directory `/Users/warford/Experiment1'
make: *** [debug] Error 2
make: Leaving directory `/Users/warford/Experiment1'
Exited with code 2.
Error while building project Experiment1
When executing build step 'Make'

I have two questions:

1. Why do I get the errors when I have widgets in my form? I have  
tried various includes and forward references, but cannot find the  
magic combination.

2. Is this approach a good way to go about implementing a modular  
design? I am refactoring a complex app that is super difficult to  
maintain. The main window has various nested splitters and tabs. I  
want a clean interface between the panes with each pane having its  
object communicate with the other objects in the other panes with  
signals and slots. Is this something that should be done using (a) Qt  
designer custom widgets, or perhaps (b) Qt designer plugins? Am I  
trying to reinvent the wheel here?

I am using Qt Creator 1.0.0, Based on Qt 4.5.0 on Mac OS X.

Thanks for any help,

Stan Warford




More information about the Qt-interest-old mailing list