Results 1 to 4 of 4

Thread: How to use Qt inheritance not only for the ui , but for the whole Project

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2011
    Posts
    16
    Thanks
    3
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    Windows

    Default How to use Qt inheritance not only for the ui , but for the whole Project

    Iam using an Integer Value i in Base class and created another class Inherited from the base class . In the constructor of base class , i is assigned with value =2510,
    that value can be displayed in the derived class .

    But the problem is that Integer i cannot be assigned value in the pushbutton click function of the baseclass, instead the old value is showing ........



    the header files and source files are given below



    Qt Code:
    1. //baseclass.h
    2.  
    3.  
    4. #ifndef BASECLASS_H
    5. #define BASECLASS_H
    6.  
    7. #include <QMainWindow>
    8.  
    9. namespace Ui {
    10. class baseclass;
    11. }
    12.  
    13. class baseclass : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit baseclass(QWidget *parent = 0);
    19. ~baseclass();
    20.  
    21.  
    22. protected:
    23. int i;
    24.  
    25. protected slots:
    26. void on_pushButton_clicked();
    27.  
    28.  
    29.  
    30. private:
    31. Ui::baseclass *ui;
    32.  
    33. private slots:
    34.  
    35.  
    36.  
    37. private slots:
    38. void on_lineEdit_cursorPositionChanged(int , int );
    39. };
    40.  
    41. #endif // BASECLASS_H
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. //derived.h
    2. #ifndef DERIVEDCLASS_H
    3. #define DERIVEDCLASS_H
    4. #include <baseclass.h>
    5. #include <QMainWindow>
    6.  
    7. namespace Ui {
    8. class derivedclass;
    9. }
    10.  
    11. class derivedclass : public baseclass
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit derivedclass(QWidget *parent = 0);
    17. ~derivedclass();
    18.  
    19.  
    20. private:
    21. Ui::derivedclass *ui;
    22. };
    23.  
    24. #endif // DERIVEDCLASS_H
    To copy to clipboard, switch view to plain text mode 



    baseclass.cpp
    Qt Code:
    1. #include "baseclass.h"
    2. #include "ui_baseclass.h"
    3. #include "inherited.h"
    4. #include "derivedclass.h"
    5. baseclass::baseclass(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::baseclass)
    8. {
    9. ui->setupUi(this);
    10. i=2510;
    11.  
    12.  
    13. }
    14.  
    15. baseclass::~baseclass()
    16. {
    17. delete ui;
    18. }
    19.  
    20. void baseclass::on_pushButton_clicked()
    21. {
    22.  
    23. QString Value=ui->lineEdit->text();
    24. i=Value.toInt();
    25.  
    26.  
    27. derivedclass *in=new derivedclass;
    28. in->show();
    29. this->hide();
    30.  
    31. }
    32.  
    33. void baseclass::on_lineEdit_cursorPositionChanged(int , int )
    34. {
    35.  
    36. QString Value=ui->lineEdit->text();
    37. ui->label_2->setText(Value);
    38.  
    39. //i= Value.toInt();
    40.  
    41. }
    To copy to clipboard, switch view to plain text mode 


    DERIVED.CPP

    Qt Code:
    1. #include "derivedclass.h"
    2. #include "ui_derivedclass.h"
    3. #include "baseclass.h"
    4.  
    5. derivedclass::derivedclass(QWidget *parent) :
    6. baseclass(parent),
    7. ui(new Ui::derivedclass)
    8. {
    9. ui->setupUi(this);
    10. QString v=QString::number(i);
    11. ui->lineEdit->setText(v);
    12. }
    13.  
    14. derivedclass::~derivedclass()
    15. {
    16. delete ui;
    17. }
    To copy to clipboard, switch view to plain text mode 


    i JUST wANTED TO HAVE VALUE IN BASE CLASS ACCESSIBLE AND ALTERABLE TO DERIVED CLASS
    Last edited by wysota; 27th May 2011 at 14:36. Reason: Threads merged

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: How to use Qt inheritance not only for the ui , but for the whole Project

    The first thing that I would check is that your on_pushButton_clicked() click handle fires. I gues people often spend hours in finding problems in code that never gets executed.

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to use Qt inheritance not only for the ui , but for the whole Project

    To OP:
    In your slot, you create a new instance of your derived class, that new instance of course has your hard coded 'i' initialization in it, which is what you see.
    It looks to me, you are confused as to what inheritance really does.
    My suggestion to you:
    Leave Qt for now, and practice and learn C++ until you are fully proficient with it.
    Only then come back to Qt, otherwise you will just get frustrated.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    Default Re: How to use Qt inheritance not only for the ui , but for the whole Project

    Your problem has nothing to do with Qt. Actually you don't have a problem at all. "i" can be modified from within methods defined in both classes. Your real problem is that you forgot to learn object oriented programming.
    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. Replies: 1
    Last Post: 3rd December 2009, 23:34
  2. inheritance
    By steiner in forum Qt Programming
    Replies: 4
    Last Post: 30th October 2007, 20:17
  3. inheritance
    By mickey in forum General Programming
    Replies: 11
    Last Post: 28th September 2007, 21:54
  4. How much inheritance do you use?
    By Michiel in forum General Programming
    Replies: 8
    Last Post: 1st August 2006, 22:29
  5. QSA and inheritance
    By jwintz in forum Qt Programming
    Replies: 1
    Last Post: 13th June 2006, 14:05

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
  •  
Qt is a trademark of The Qt Company.