PDA

View Full Version : how do you calling a function from MainWindow from a widget.



parsnips146
30th July 2010, 16:00
Hello,

I have only just started programming both C++ and Qt and have a question about accessing functions in classes.

I have a main function with a MDI and have a widget i created for the documents to open. I also have a Wizard called newWizard which currently opens shows some labels and is supposed to create a default child object in the Mdi.

the MainWindow code used to create the Mdi and the children is shown below,


#include <QtGui>

#include "mainwindow.h"
#include "newwizard.h"
#include "groupbox.h"

MainWindow::MainWindow()
{
mdiArea = new QMdiArea;
mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded );
mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
mdiArea->setViewMode(QMdiArea::TabbedView);
setCentralWidget(mdiArea);

createActions();
createMenu();
createToolbars();

readSetting();

setWindowIcon(QIcon(":images/images/cegu.ico"));
//setCurrentFile("");

createchild();
}

void MainWindow::createchild()
{
GroupBox *child = new GroupBox;
mdiArea->addSubWindow(child);
child->show();
}


void MainWindow::fileNew()
{
newWizard wizard;
wizard.exec();
}

the headerfile for mainwindow is shown below:


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class QAction;
class QLabel;
class QMdiArea;
class newWizard;

class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
void createchild();
...
private:
void createActions();
void createMenu();
void createToolbars();
void readSetting();
void writeSetting();
void loadFile(const QString &fileName);
bool saveFile(const QString &fileName);
void setCurrentFile(const QString &fileName);
QString strippedName(const QString &fullFileName);

QStringList recentFiles;
QMdiArea *mdiArea;

The newWizard code is shown below


#include <QtGui>
#include "newwizard.h"
#include "mainwindow.h"


newWizard::newWizard(QWidget *parent) :
QWizard(parent)
{
addPage(new introPage);
addPage(new fileTypePage);
addPage(new whichVariablesPage);
addPage(new conclusionPage);


setWindowTitle(tr("New File Wizard"));
}

void newWizard::accept(QWidget *parent) : QMainWindow(parent)
{
parent->
createchild();

}

introPage::introPage(QWidget *parent) : QWizardPage(parent)
{
setTitle(tr("Introduction"));

label = new QLabel(tr("This wizard will create a new"
"File either header or Footer"
"with the variable you choose"
));
label->setWordWrap(true);

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(label);
setLayout(layout);
}

fileTypePage::fileTypePage(QWidget *parent) : QWizardPage(parent)
{
setTitle(tr("File Type Selection"));
setSubTitle(tr("Specify which File type either"
"header or footer and the name."));

headerRadioButton = new QRadioButton(tr("&Header File: "));
headerNameLineEdit = new QLineEdit;
connect(headerRadioButton, SIGNAL(toggled(bool)), headerNameLineEdit, SLOT(setEnabled(bool)));
headerRadioButton->setChecked(true);

footerRadioButton = new QRadioButton(tr("&Footer File: "));
footerNameLineEdit = new QLineEdit;
connect(footerRadioButton, SIGNAL(toggled(bool)), footerNameLineEdit, SLOT(setEnabled(bool)));
footerRadioButton->setChecked(false);
footerNameLineEdit->setEnabled(false);

registerField("HeaderName", headerNameLineEdit);
registerField("headerSelect", headerRadioButton);
registerField("FooterName", footerNameLineEdit);
registerField("footerSelect", footerRadioButton);

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(headerRadioButton);
layout->addWidget(headerNameLineEdit);
layout->addWidget(footerRadioButton);
layout->addWidget(footerNameLineEdit);
setLayout(layout);
}

whichVariablesPage::whichVariablesPage(QWidget *parent) : QWizardPage(parent)
{
setTitle(tr("Variables Selection"));
setSubTitle(tr("Specify which Variables you would like to include."));


}

conclusionPage::conclusionPage(QWidget *parent) :QWizardPage(parent)
{

}

and the header file for that is


#ifndef NEWWIZARD_H
#define NEWWIZARD_H

#include <QWizard>

QT_BEGIN_NAMESPACE
class QCheckBox;
class QGroupBox;
class QLabel;
class QLineEdit;
class QRadioButton;
class QMdiArea;
QT_END_NAMESPACE


class newWizard : public QWizard
{
Q_OBJECT

public:
newWizard(QWidget *parent = 0);


void accept();

};

class introPage : public QWizardPage
{
Q_OBJECT

public:
introPage(QWidget *parent = 0);

private:
QLabel *label;
};

class fileTypePage : public QWizardPage
{
Q_OBJECT

public:
fileTypePage(QWidget *parent = 0);

private:
QLabel *headerNameLabel;
QLabel *footerNameLabel;
QLineEdit *headerNameLineEdit;
QLineEdit *footerNameLineEdit;
QRadioButton *headerRadioButton;
QRadioButton *footerRadioButton;

};

class whichVariablesPage : public QWizardPage
{
Q_OBJECT

public:
whichVariablesPage(QWidget *parent = 0);

private:
QLabel *label;
};


class conclusionPage : public QWizardPage
{
Q_OBJECT

public:
conclusionPage(QWidget *parent = 0);

private:
QLabel *label;
};

#endif // NEWWIZARD_H

This code is not the finished article and is still a work in progress. As can be seen in the newWizard code at lines 18 to 23 i am trying to access the createchild() function from the mainwindow in that function when the wizard closes.

Any help on this would be great.

Thanks

James Parsons

Lykurg
30th July 2010, 16:23
This question was asked several times, so please use the search function of this board.

Short answer: use signal and slots mechanism or pass a pointer of your main window to the dialog. Better use the first version.

parsnips146
30th July 2010, 16:59
Thank you for the quick reply.

The hint to use signal and slot mechanism was extreamly useful and enabled me to get this part of the code to work.