Results 1 to 11 of 11

Thread: Heritage

  1. #1
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Heritage

    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
    Qt Code:
    1. #ifndef DEF_HERITAGE
    2. #define DEF_HERITAGE
    3.  
    4.  
    5. #include <QtGui>
    6.  
    7.  
    8. class Heritage: public QMainWindow
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. Heritage();
    14. ~Heritage();
    15.  
    16. public slots:
    17. void openChildWindow();
    18.  
    19. protected:
    20. QMainWindow *m_centralZone;
    21. QPushButton *m_childWindow, *m_quick;
    22.  
    23.  
    24. };
    25.  
    26. #endif // HERITAGE_H
    To copy to clipboard, switch view to plain text mode 

    MainWindow.cpp
    Qt Code:
    1. #include "Heritage.h"
    2.  
    3. Heritage::Heritage()
    4. {
    5. m_centralZone = new QMainWindow;
    6. m_centralZone->setFixedSize(400, 200);
    7.  
    8. m_childWindow = new QPushButton("Open childwindow", m_centralZone);
    9. m_childWindow->move(50, 150);
    10.  
    11. m_quick = new QPushButton("Exit", m_centralZone);
    12. m_quick->move(200, 150);
    13.  
    14.  
    15. connect(m_childWindow, SIGNAL(clicked()), this, SLOT(openChildWindow()));
    16. connect(m_quick, SIGNAL(clicked()), qApp, SLOT(quit()));
    17.  
    18. setWindowTitle("Heritage");
    19.  
    20. setCentralWidget(m_centralZone);
    21. }
    22.  
    23. void Heritage::openChildWindow()
    24. {
    25. m_childWindow->show();
    26. }
    27.  
    28. Heritage::~Heritage()
    29. {}
    To copy to clipboard, switch view to plain text mode 
    Childwindow.h
    Qt Code:
    1. #ifndef DEF_CHILDWINDOW
    2. #define DEF_CHILDWINDOW
    3.  
    4. #include "Heritage.h"
    5.  
    6. #include<QtGui>
    7.  
    8. class ChildWindow: public Heritage
    9. {
    10. Q_OBJECT
    11.  
    12. ChildWindow(QWidget *parent);
    13. ~ChildWindow();
    14.  
    15. public:
    16.  
    17. private slots:
    18.  
    19. private:
    20. QWidget *m_childWndow;
    21.  
    22. };
    23.  
    24. #endif // CHILDWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    ChildWindow.cpp
    Qt Code:
    1. #include "ChildWindow.h"
    2.  
    3. ChildWindow::ChildWindow(QWidget *parent):QMainWindow(parent)
    4. {
    5. m_childWindow = new QWidget;
    6. m_childWindow->setFixedSize(350, 150);
    7.  
    8.  
    9. }
    10.  
    11. ChildWindow::~ChildWindow()
    12. {
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 

    Main
    Qt Code:
    1. #include <QApplication>
    2. #include "Heritage.h"
    3. #include <QtGui>
    4.  
    5.  
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QApplication app(argc, argv);
    10. Heritage wind;
    11.  
    12. wind.show();
    13.  
    14. return app.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 

    I would really appreciate your help.
    Many thanks in advance

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Heritage

    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.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Heritage

    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.

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Heritage

    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..
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Heritage

    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.

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Heritage

    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.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  7. #7
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Heritage

    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.

  8. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Heritage

    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:penChildWindow() 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
    Qt Code:
    1. #include <QApplication>
    2. #include "Heritage.h"
    3. #include <QtGui>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8. Heritage wind;
    9.  
    10. wind.show();
    11.  
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    Heritage.h
    Qt Code:
    1. #ifndef DEF_HERITAGE
    2. #define DEF_HERITAGE
    3.  
    4. #include <QtGui>
    5.  
    6. class ChildWindow;
    7.  
    8. class Heritage: public QMainWindow
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. explicit Heritage(QWidget *parent = 0);
    14. virtual ~Heritage();
    15.  
    16. public slots:
    17. void openChildWindow();
    18.  
    19. protected:
    20. ChildWindow *m_childWindow;
    21. QPushButton *m_childButton;
    22. QPushButton *m_quitButton;
    23. };
    24.  
    25. #endif // HERITAGE_H
    To copy to clipboard, switch view to plain text mode 

    Heritage.cpp
    Qt Code:
    1. #include "Heritage.h"
    2. #include "ChildWindow.h"
    3.  
    4. Heritage::Heritage(QWidget *parent)
    5. : QMainWindow(parent)
    6. , m_childWindow(new ChildWindow(0))
    7. {
    8. m_childWindow->setWindowTitle("ChildWindow");
    9. m_childWindow->hide();
    10.  
    11. QWidget *centralWidget = new QWidget(this);
    12. centralWidget->setFixedSize(400, 200);
    13.  
    14. m_childButton = new QPushButton("Open childwindow", centralWidget);
    15. m_childButton->move(50, 150);
    16.  
    17. m_quitButton = new QPushButton("Exit", centralWidget);
    18. m_quitButton->move(200, 150);
    19.  
    20. connect(m_childButton, SIGNAL(clicked()), this, SLOT(openChildWindow()));
    21. connect(m_quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));
    22.  
    23. setWindowTitle("Heritage");
    24. setCentralWidget(centralWidget);
    25. }
    26.  
    27. void Heritage::openChildWindow()
    28. {
    29. m_childWindow->show();
    30. }
    31.  
    32. Heritage::~Heritage()
    33. {
    34. ;
    35. }
    To copy to clipboard, switch view to plain text mode 

    ChildWindow.h
    Qt Code:
    1. #ifndef DEF_CHILDWINDOW
    2. #define DEF_CHILDWINDOW
    3.  
    4. #include<QtGui>
    5.  
    6. class ChildWindow: public QWidget
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. explicit ChildWindow(QWidget *parent = 0);
    12. virtual ~ChildWindow();
    13.  
    14. private:
    15. QPushButton *m_childButton;
    16. };
    17.  
    18. #endif // CHILDWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    ChildWindow.cpp
    Qt Code:
    1. #include "ChildWindow.h"
    2.  
    3. ChildWindow::ChildWindow(QWidget *parent)
    4. : QWidget(parent)
    5. {
    6. m_childButton = new QPushButton("ChildWindow Button", this);
    7. m_childButton->setFixedSize(350, 150);
    8. }
    9.  
    10. ChildWindow::~ChildWindow()
    11. {
    12. ;
    13. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Santosh Reddy; 25th January 2013 at 10:29.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  9. #9
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Heritage

    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?

  10. #10
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Heritage

    Try copying the code again, and then build. It should compile fine.
    Last edited by Santosh Reddy; 25th January 2013 at 11:45.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  11. #11
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Heritage

    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!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.