PDA

View Full Version : Heritage



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

Santosh Reddy
25th January 2013, 08:33
I can send you a corrected code, but I am sure it will only cause more confusion to you.

Instead I recommend to take a step back and take a smaller code and make it work.

1. Are you sure what you want and what you are doing?
2. Why are you connecting a QPushButton clicked() signel to show() ? (this will not be of any use)
3. ChildWindow is inheroted form Heritage, which is inherited from QMainWindow. Clearly QMainWindow is not directly inhertied so the ctor syntax is in-correct. It should be Heritage instead of QMainWindow;


I some how feel you are confused between QPushButton & ChildWidget, or the posted is not what you wanted to post.

Stanfillirenfro
25th January 2013, 08:48
Hello Reddy and many thanks for your reply.
By sending me the correct code, it would not cause any confusion in me please.
Yes, I am really sure what I want and what I am doing. The intension is to get to the childwindow from the main one. Behind this idea, I just wanted to use some functions of the main window in the childwindow without having to rewrite them. But I am blocked at this step of getting to the childwindow already, so that I can not continue.
For your third remark, Heritage should replace QMainWindow, but even with this change my code fails to compile.

I would really appreciate your help.
Thanks in advance.

Santosh Reddy
25th January 2013, 08:59
The intension is to get to the childwindow from the main one.
There no part in code which does this.

Moreover I suggest searching this the forum, as this is very commonly asked question/problem, and has been asnwered multiple times on this forum. You should also be able get to a working code in some thread.

I got some work now, will see later..

Stanfillirenfro
25th January 2013, 09:10
Thanks!

The intension is to get to the childwindow from the main one.

To get to the childwindow, I have to create it. I did it (Childwindow)
To get to the childwindow, I have to connect a signal from the main window using the button "m_childWindow" to a slot (openChildWindow()).
The exit slot (to close the application) is working properly, but not that to get to the childwindow. It is there my problem.

Will appreciate any help.

Santosh Reddy
25th January 2013, 09:22
This is what I feared.


To get to the childwindow, I have to create it. I did it (Childwindow)
This is not being done in the posted code


To get to the childwindow, I have to connect a signal from the main window using the button "m_childWindow" to a slot (openChildWindow()).
This slot will show pushbutton not childwindow. this is the reason I quoted earlier that

I some how feel you are confused between QPushButton & ChildWidget, or the posted is not what you wanted to post.


The exit slot (to close the application) is working properly, but not that to get to the childwindow. It is there my problem.
Yes, this is correct, it will never get to childwindow, as you never created it. :confused:

Stanfillirenfro
25th January 2013, 09:42
Hi Reddy!
I have created teh childwindow with ChildWindow.h and ChildWindow.cpp. Isn't it the way to create a window?

This slot will show pushbutton not childwindow. this is the reason I quoted earlier that
According to my knowledge, by clicking on this button, a signal should be emitted to the main window to display the childwindow.

I some how feel you are confused between QPushButton & ChildWidget, or the posted is not what you wanted to post.
No, a button is a widget on wich a click can be done. A childwidget can be a window with buttons.

Santosh Reddy
25th January 2013, 10:29
I have created teh childwindow with ChildWindow.h and ChildWindow.cpp. Isn't it the way to create a window?
Can you show where is this ?. Post specific line of code where this is beig done.


According to my knowledge, by clicking on this button, a signal should be emitted to the main window to display the childwindow.
When clicked a signal will be emitted and Haritage::openChildWindow() slot will be executed. In this slot there is no code to create/show ChildWindoe. All this slot does is to show the m_childWindow (QPushButton). m_childWindow is QPushButton (NOT ChildWindow).




I some how feel you are confused between QPushButton & ChildWidget, or the posted is not what you wanted to post.
No, a button is a widget on wich a click can be done. A childwidget can be a window with buttons.
This seems ok. Then what is that makes you think a ChildWindow is created?

Try answering these...

1. Point me to the code where ChildWindow is created?
2. Point me to the code where ChildWindow is showed?
3. Why are you creating a QWidget in ChildWindow ctor?
4. Why are you connecting a QPushButton::clicked() signel to QPushButton::show()?
5. Why are you setting QMainWindow (m_centralZone) as central widget of Heritage?

I hope by the time you answer these questions, you should understand that these are the things you did worng in the code.

If it is still not clear, please refresh your C++ basics.

Added after 24 minutes:

Ok here is modified to work code
main.cpp


#include <QApplication>
#include "Heritage.h"
#include <QtGui>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Heritage wind;

wind.show();

return app.exec();
}


Heritage.h


#ifndef DEF_HERITAGE
#define DEF_HERITAGE

#include <QtGui>

class ChildWindow;

class Heritage: public QMainWindow
{
Q_OBJECT

public:
explicit Heritage(QWidget *parent = 0);
virtual ~Heritage();

public slots:
void openChildWindow();

protected:
ChildWindow *m_childWindow;
QPushButton *m_childButton;
QPushButton *m_quitButton;
};

#endif // HERITAGE_H


Heritage.cpp


#include "Heritage.h"
#include "ChildWindow.h"

Heritage::Heritage(QWidget *parent)
: QMainWindow(parent)
, m_childWindow(new ChildWindow(0))
{
m_childWindow->setWindowTitle("ChildWindow");
m_childWindow->hide();

QWidget *centralWidget = new QWidget(this);
centralWidget->setFixedSize(400, 200);

m_childButton = new QPushButton("Open childwindow", centralWidget);
m_childButton->move(50, 150);

m_quitButton = new QPushButton("Exit", centralWidget);
m_quitButton->move(200, 150);

connect(m_childButton, SIGNAL(clicked()), this, SLOT(openChildWindow()));
connect(m_quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));

setWindowTitle("Heritage");
setCentralWidget(centralWidget);
}

void Heritage::openChildWindow()
{
m_childWindow->show();
}

Heritage::~Heritage()
{
;
}


ChildWindow.h


#ifndef DEF_CHILDWINDOW
#define DEF_CHILDWINDOW

#include<QtGui>

class ChildWindow: public QWidget
{
Q_OBJECT

public:
explicit ChildWindow(QWidget *parent = 0);
virtual ~ChildWindow();

private:
QPushButton *m_childButton;
};

#endif // CHILDWINDOW_H


ChildWindow.cpp


#include "ChildWindow.h"

ChildWindow::ChildWindow(QWidget *parent)
: QWidget(parent)
{
m_childButton = new QPushButton("ChildWindow Button", this);
m_childButton->setFixedSize(350, 150);
}

ChildWindow::~ChildWindow()
{
;
}

Stanfillirenfro
25th January 2013, 11:17
Many thanks Reddy for your help.
I have tried to compile your code, but I have the following error message:
"In constructor 'Heritage::Heritage(QWidget*)':
'ChildWindow::ChildWindow(QWidget*)' is private

Could you please have a look?

Santosh Reddy
25th January 2013, 11:27
Try copying the code again, and then build. It should compile fine.

Stanfillirenfro
25th January 2013, 11:40
Many thanks Reddy. It is working perfectly. The problem is solved.
I am really grateful to you for your time to help me. I have make not only one, but many steps forward because of you.
One more time MANY THANKS!