Results 1 to 4 of 4

Thread: Inheritance problem {SOLVED}

  1. #1
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Angry Inheritance problem {SOLVED}

    Hi everyone, I have the following methods in my base class:

    Qt Code:
    1. #ifndef WIDGETFACTURACION_H
    2. #define WIDGETFACTURACION_H
    3.  
    4. #include <QWidget>
    5.  
    6. namespace Ui {
    7. class widgetFacturacion;
    8. }
    9.  
    10. class widgetFacturacion : public QWidget
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit widgetFacturacion(QWidget *parent = 0, int handler = 0, int tipo_factura = 0, int tipo_ticket = 0, int nro_tique = 0);
    16. ~widgetFacturacion();
    17.  
    18. protected:
    19. Ui::widgetFacturacion *ui;
    20. .
    21. .
    22. .
    23. void conectar ();
    24. virtual void ingresar_valores_tabla (QString precio);
    25. virtual void inicializar (int tipo_factura);
    26.  
    27.  
    28. protected slots:
    29. virtual void on_cancelar_clicked();
    30.  
    31. virtual void on_realizar_venta_clicked();
    32.  
    33. signals:
    34. void widgetClosed (QString );
    35. };
    36.  
    37. #endif // WIDGETFACTURACION_H
    To copy to clipboard, switch view to plain text mode 

    the derived one implements the virtual methods:
    Qt Code:
    1. #ifndef WIDGETFACTBLACK_H
    2. #define WIDGETFACTBLACK_H
    3.  
    4. #include "widgetfacturacion.h"
    5.  
    6. namespace Ui {
    7. class WidgetFactBlack;
    8. }
    9.  
    10. class WidgetFactBlack : public widgetFacturacion
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit WidgetFactBlack(QWidget *parent, int handler, int tipo_factura, int tipo_ticket=0, int nro_tique=0);
    16. ~WidgetFactBlack();
    17.  
    18. protected:
    19. void ingresar_valores_tabla (QString precio);
    20. void inicializar (int tipo_factura);
    21.  
    22. protected slots:
    23. void on_cancelar_clicked();
    24. void on_realizar_venta_clicked();
    25. };
    26.  
    27. #endif // WIDGETFACTBLACK_H
    To copy to clipboard, switch view to plain text mode 

    And the .cpp of my derived class:

    Qt Code:
    1. #include "widgetfactblack.h"
    2. #include "ui_widgetfacturacion.h"
    3. #include "global_variables.h"
    4.  
    5. WidgetFactBlack::WidgetFactBlack(QWidget *parent, int handler, int tipo_factura, int tipo_ticket, int nro_tique) :
    6. widgetFacturacion(parent, handler, tipo_factura, tipo_ticket, nro_tique)
    7. {
    8. }
    9.  
    10. WidgetFactBlack::~WidgetFactBlack()
    11. {
    12. }
    13.  
    14. //---------------------------Inicializamos todo luego de seleccionado el tipo de factura
    15. void WidgetFactBlack::inicializar (int tipo_factura){
    16. qDebug()<< "Entre al metodo en black inicializar";
    17. ui->setupUi(this);
    18. ui->label_descripcion->clear();
    19. .
    20. .
    21. .
    22. }
    23. .
    24. .
    25. .
    To copy to clipboard, switch view to plain text mode 

    The problem comes when from the constructor of the base class i want to call the method of the derived class (method inicializar) posted before. Because program is not calling the derived method, instead it executes the one of the base class, even though it is defined by the derived class. I dont know if this is because it is called from within the constructor of the base class...Wish I knew.

    base class .cpp

    Qt Code:
    1. widgetFacturacion::widgetFacturacion(QWidget *parent, int handler, int tipo_factura, int tipo_ticket, int nro_tique) :
    2. QWidget(parent),
    3. ui(new Ui::widgetFacturacion)
    4. {
    5. this->conectar ();
    6. controlador = new ComControlador (handler);
    7. if (tipo_factura >0){
    8. SeleccionarCliente * cliente = new SeleccionarCliente (0);
    9. cliente->setWindowTitle ("Seleccionar Cliente");
    10. connect (cliente, SIGNAL(cliente_seleccionado(QStringList)), this, SLOT(cargar_cliente(QStringList)));
    11. cliente->show ();
    12. }else inicializar (tipo_factura); //PROBLEM, callls the base class method not looking into the derived one
    13. }
    14.  
    15. void widgetFacturacion::inicializar (int tipo_factura)
    16. {
    17. qDebug()<<"hi";
    18. ui->setupUi(this);
    19. ui->label_descripcion->clear();
    20. }
    To copy to clipboard, switch view to plain text mode 

    All the other virtual methods defined in the derived class are called correctly. Only the one called from the constructor gives me problems.

    Sorry if this is too basic.

    Thank you all in advance.


    My bad, I understood now:
    Even though it's a virtual function, the base's version will get called since the derived class isn't fully constructed yet. The base class constructor is called before the derived class constructor, so if the derived virtual function were to get called, it would be with an incompletely-initialized instance - a possible (probably) recipe for disaster.
    Last edited by KillGabio; 24th July 2013 at 07:12.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Inheritance problem {SOLVED}

    At the time of construction of the base class, the derived class is not yet created thus the virtual method table is not overwritten with methods redefined in the derived class thus calling virtual methods from the constructor of the base class cannot access any code from the derived class. Trying to do that results in the code from the base class being invoked and is considered a programming error as far as the standards go.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    KillGabio (24th July 2013)

  4. #3
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Inheritance problem {SOLVED}

    Thank you, I already editted my answer sorry for wasting your time. I was reading but not finding any answer till I camed across with a FAQ of C++ lol.


    Added after 22 minutes:


    I have another question that is bugging me, regarding this two classes. When I hit a button to close the derived Widget. The function called by the click of the button is executed twice, any ideas why this can be happening??


    In fact every button that closes the widget is called twice :/


    Added after 19 minutes:


    Got it, I was declaring the slots again in the derived class.
    protected slots:
    That line is wrong in derived.h
    Last edited by KillGabio; 24th July 2013 at 08:21.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Inheritance problem {SOLVED}

    You should name your slots differently (declaring what they do and not when they are invoked) and use connect() statements to connect them to signals.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Problem of inheritance
    By giorgik in forum Qt Programming
    Replies: 9
    Last Post: 22nd February 2013, 21:50
  2. problem with Inheritance and QTreeView
    By Kicer in forum Qt Programming
    Replies: 0
    Last Post: 3rd May 2010, 15:10
  3. Inheritance problem
    By brevleq in forum Qt Programming
    Replies: 6
    Last Post: 23rd December 2008, 06:27
  4. problem in widget inheritance
    By wagmare in forum Qt Programming
    Replies: 2
    Last Post: 10th December 2008, 07:35
  5. subclassing/inheritance QObject problem
    By cbeall1 in forum Qt Programming
    Replies: 12
    Last Post: 13th February 2006, 17:49

Tags for this Thread

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.