Results 1 to 3 of 3

Thread: doubt in parent and child class

  1. #1
    Join Date
    Nov 2014
    Location
    Chennai
    Posts
    160
    Thanks
    65
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default doubt in parent and child class

    need to call parent method or variable in child class.
    i created parent class (QWidget)
    Widget.h
    Qt Code:
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include <QWidget>
    4. #include<QtGui>
    5. #include"secdialog.h" //child class
    6. namespace Ui {
    7. class Widget;
    8. }
    9.  
    10. class Widget : public QWidget
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. QPushButton *button1;
    16. QString list;
    17. private:
    18. Ui::Widget *ui;
    19. secDialog *sec; //creating object to child class
    20. public slots:
    21. void display();
    22. }
    To copy to clipboard, switch view to plain text mode 
    secDialog.h
    child class (QDialog
    Qt Code:
    1. #ifndef SECDIALOG_H
    2. #define SECDIALOG_H
    3. #include <QDialog>
    4. #include<QWidget>
    5. #include<QtGui>
    6. #include"widget.h"//parent class
    7. namespace Ui {
    8. class secDialog;
    9. }
    10.  
    11. class secDialog : public QDialog
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. Private :
    17. Widget *widget;//create object for parent class
    18. public slots:
    19. void newmethod();
    20. }
    To copy to clipboard, switch view to plain text mode 
    Widget.cpp
    Qt Code:
    1. #include "widget.h"
    2. #include "ui_widget.h"
    3.  
    4. Widget::Widget(QWidget *parent) :
    5. QWidget(parent),
    6. ui(new Ui::Widget)
    7. {
    8. button1=new QPushButton("CLICK",this);
    9. button1->show();
    10. connect(button1,SIGNAL(clicked()),this,SLOT(display());
    11. ui->setupUi(this);
    12. }
    13. void Widget::display()
    14. {
    15. sec=new secDialog(this);
    16. sec->show();
    17. }
    To copy to clipboard, switch view to plain text mode 
    secDialog.cpp
    Qt Code:
    1. #include "secdialog.h"
    2. #include "ui_secdialog.h"
    3. secDialog::secDialog(QWidget *parent) :
    4. QDialog(parent),
    5. ui(new Ui::secDialog)
    6. {
    7. newmethod();
    8. }
    9. void secDialog::newmethod()
    10. {
    11. widget=new QWidget(this);
    12. widget->list="Hello";
    13. qDebug<<widget->list;
    14. }
    To copy to clipboard, switch view to plain text mode 
    When i execute this ,i getting Error,
    Qt Code:
    1. "widget does not name a type"
    To copy to clipboard, switch view to plain text mode 
    i need to use both class variables and button,i know somewhere am doing wrong can any one help to solve this problem,
    Thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: doubt in parent and child class

    You have recursive includes, better use forward declarations in the headers to make each other type known and only include the other class' header in the source

    widget.h
    Qt Code:
    1. class secDialog; // instead of #include "secDialog.h"
    To copy to clipboard, switch view to plain text mode 

    widget.cpp
    Qt Code:
    1. #include"secDialog.h" // include moved to the cpp file
    To copy to clipboard, switch view to plain text mode 

    secDialog.h
    Qt Code:
    1. class Widget; // instead of #include "widget.h"
    To copy to clipboard, switch view to plain text mode 

    secDialog.cpp
    Qt Code:
    1. #include"widget.h" // include moved to the cpp file
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    iswaryasenthilkumar (26th November 2015)

  4. #3
    Join Date
    Nov 2014
    Location
    Chennai
    Posts
    160
    Thanks
    65
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: doubt in parent and child class

    Thanks it works anda_skoa
    Quote Originally Posted by anda_skoa View Post
    You have recursive includes, better use forward declarations in the headers to make each other type known and only include the other class' header in the source

    widget.h
    Qt Code:
    1. class secDialog; // instead of #include "secDialog.h"
    To copy to clipboard, switch view to plain text mode 

    widget.cpp
    Qt Code:
    1. #include"secDialog.h" // include moved to the cpp file
    To copy to clipboard, switch view to plain text mode 

    secDialog.h
    Qt Code:
    1. class Widget; // instead of #include "widget.h"
    To copy to clipboard, switch view to plain text mode 

    secDialog.cpp
    Qt Code:
    1. #include"widget.h" // include moved to the cpp file
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. Replies: 2
    Last Post: 25th February 2015, 18:18
  2. Best way for a parent class to know value chosen on child class
    By scarecr0w132 in forum Qt Programming
    Replies: 2
    Last Post: 16th October 2014, 02:40
  3. Replies: 3
    Last Post: 13th February 2012, 08:22
  4. Replies: 8
    Last Post: 5th August 2010, 05:51
  5. Child-Parent Widget??
    By anupamgee in forum Qt Programming
    Replies: 1
    Last Post: 11th May 2009, 13:17

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.