Stanfillirenfro
25th January 2013, 08:12
Greetings to all of you!
I am facing a problem of opening a childwindow from a principal one. In this project, the childwindow should inherit from the principal one.
While compiling, the following error message appears: "QMainWindow' is not a direct base of 'Childwindow" (in ChildWindow.cpp, line 3).
According to my knowledge, the ChildWindow is a QWidget (see ChildWindow.h, line 12), inheriting from a QMainWindow, and it is the reason for the ChildWindow.cpp, line 3.
When the line 12 of ChildWIndow.h is as followed:
ChildWindow(),
and the line 3 of ChildWindow.cpp as followed:
ChildWindow::ChildWindow(),
the compilation works properly, but the slot (line 15 of Heritage.cpp) does not fuction, that mean I can not get to the childwindow. A click on the exit button lead as expected to the end of the application and the main window gets closed.
My questions:
1-Can I use the heritage in such case?
2- If yes, what is wrong with the constructor of the childWindow?
3- Finally, how can I solve the entire problem?
This is my code.
MainWindow.h
#ifndef DEF_HERITAGE
#define DEF_HERITAGE
#include <QtGui>
class Heritage: public QMainWindow
{
Q_OBJECT
public:
Heritage();
~Heritage();
public slots:
void openChildWindow();
protected:
QMainWindow *m_centralZone;
QPushButton *m_childWindow, *m_quick;
};
#endif // HERITAGE_H
MainWindow.cpp
#include "Heritage.h"
Heritage::Heritage()
{
m_centralZone = new QMainWindow;
m_centralZone->setFixedSize(400, 200);
m_childWindow = new QPushButton("Open childwindow", m_centralZone);
m_childWindow->move(50, 150);
m_quick = new QPushButton("Exit", m_centralZone);
m_quick->move(200, 150);
connect(m_childWindow, SIGNAL(clicked()), this, SLOT(openChildWindow()));
connect(m_quick, SIGNAL(clicked()), qApp, SLOT(quit()));
setWindowTitle("Heritage");
setCentralWidget(m_centralZone);
}
void Heritage::openChildWindow()
{
m_childWindow->show();
}
Heritage::~Heritage()
{}
Childwindow.h
#ifndef DEF_CHILDWINDOW
#define DEF_CHILDWINDOW
#include "Heritage.h"
#include<QtGui>
class ChildWindow: public Heritage
{
Q_OBJECT
ChildWindow(QWidget *parent);
~ChildWindow();
public:
private slots:
private:
QWidget *m_childWndow;
};
#endif // CHILDWINDOW_H
ChildWindow.cpp
#include "ChildWindow.h"
ChildWindow::ChildWindow(QWidget *parent):QMainWindow(parent)
{
m_childWindow = new QWidget;
m_childWindow->setFixedSize(350, 150);
}
ChildWindow::~ChildWindow()
{
}
Main
#include <QApplication>
#include "Heritage.h"
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Heritage wind;
wind.show();
return app.exec();
}
I would really appreciate your help.
Many thanks in advance
I am facing a problem of opening a childwindow from a principal one. In this project, the childwindow should inherit from the principal one.
While compiling, the following error message appears: "QMainWindow' is not a direct base of 'Childwindow" (in ChildWindow.cpp, line 3).
According to my knowledge, the ChildWindow is a QWidget (see ChildWindow.h, line 12), inheriting from a QMainWindow, and it is the reason for the ChildWindow.cpp, line 3.
When the line 12 of ChildWIndow.h is as followed:
ChildWindow(),
and the line 3 of ChildWindow.cpp as followed:
ChildWindow::ChildWindow(),
the compilation works properly, but the slot (line 15 of Heritage.cpp) does not fuction, that mean I can not get to the childwindow. A click on the exit button lead as expected to the end of the application and the main window gets closed.
My questions:
1-Can I use the heritage in such case?
2- If yes, what is wrong with the constructor of the childWindow?
3- Finally, how can I solve the entire problem?
This is my code.
MainWindow.h
#ifndef DEF_HERITAGE
#define DEF_HERITAGE
#include <QtGui>
class Heritage: public QMainWindow
{
Q_OBJECT
public:
Heritage();
~Heritage();
public slots:
void openChildWindow();
protected:
QMainWindow *m_centralZone;
QPushButton *m_childWindow, *m_quick;
};
#endif // HERITAGE_H
MainWindow.cpp
#include "Heritage.h"
Heritage::Heritage()
{
m_centralZone = new QMainWindow;
m_centralZone->setFixedSize(400, 200);
m_childWindow = new QPushButton("Open childwindow", m_centralZone);
m_childWindow->move(50, 150);
m_quick = new QPushButton("Exit", m_centralZone);
m_quick->move(200, 150);
connect(m_childWindow, SIGNAL(clicked()), this, SLOT(openChildWindow()));
connect(m_quick, SIGNAL(clicked()), qApp, SLOT(quit()));
setWindowTitle("Heritage");
setCentralWidget(m_centralZone);
}
void Heritage::openChildWindow()
{
m_childWindow->show();
}
Heritage::~Heritage()
{}
Childwindow.h
#ifndef DEF_CHILDWINDOW
#define DEF_CHILDWINDOW
#include "Heritage.h"
#include<QtGui>
class ChildWindow: public Heritage
{
Q_OBJECT
ChildWindow(QWidget *parent);
~ChildWindow();
public:
private slots:
private:
QWidget *m_childWndow;
};
#endif // CHILDWINDOW_H
ChildWindow.cpp
#include "ChildWindow.h"
ChildWindow::ChildWindow(QWidget *parent):QMainWindow(parent)
{
m_childWindow = new QWidget;
m_childWindow->setFixedSize(350, 150);
}
ChildWindow::~ChildWindow()
{
}
Main
#include <QApplication>
#include "Heritage.h"
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Heritage wind;
wind.show();
return app.exec();
}
I would really appreciate your help.
Many thanks in advance