Results 1 to 6 of 6

Thread: How can we inherit ui?

  1. #1
    Join Date
    May 2011
    Posts
    120
    Thanks
    33
    Qt products
    Qt4
    Platforms
    Windows

    Default How can we inherit ui?

    how to inherit ui of the base class in derived class, now i can access memeber fuctions and member variables of base class. what can i do to inherit the ui of base class?

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How can we inherit ui?

    If ui form is public or protected member of base class, then you can access it directly in derived class (as any other base class member).
    In derived class constructor, you just call base class constructor in ctor initializer list:
    Qt Code:
    1. Derived::Derived(QWidget * parent) : Base(parent){ ... }
    To copy to clipboard, switch view to plain text mode 
    If you initialize ui in Base class constructor, it will be initialized here as well.
    If this does not helps you, provide more description for your problem.

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

    vinayaka (30th May 2011)

  4. #3
    Join Date
    May 2011
    Posts
    120
    Thanks
    33
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can we inherit ui?

    i don't get it. it shows errors. can you plz describe more.

  5. #4
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can we inherit ui?

    Quote Originally Posted by vinayaka View Post
    i don't get it. it shows errors. can you plz describe more.
    describe more? no. *you* have to describe more. it is not clear what you have and what exactly you want to achieve. maybe you could post a small piece of code...

  6. #5
    Join Date
    May 2011
    Posts
    120
    Thanks
    33
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can we inherit ui?

    Qt Code:
    1. //baseclass.h
    2. #ifndef BASECLASS_H
    3. #define BASECLASS_H
    4.  
    5. #include <QMainWindow>
    6.  
    7. namespace Ui {
    8. class baseclass;
    9. }
    10.  
    11. class baseclass : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit baseclass(QWidget *parent = 0);
    17. ~baseclass();
    18. // baseclass(int i = 10) : member(i){}
    19. // int getMember() {return member;}
    20. int i;
    21. Ui::baseclass *ui;
    22.  
    23. public slots:
    24. void on_pushButton_clicked();
    25. void test();
    26.  
    27.  
    28.  
    29. private:
    30.  
    31.  
    32. private slots:
    33.  
    34.  
    35.  
    36. private slots:
    37. void on_lineEdit_cursorPositionChanged(int , int );
    38. };
    39.  
    40. #endif // BASECLASS_H
    41.  
    42.  
    43. //derived class
    44. #ifndef BASECLASS_H
    45. #define BASECLASS_H
    46.  
    47. #include <QMainWindow>
    48.  
    49. namespace Ui {
    50. class baseclass;
    51. }
    52.  
    53. class baseclass : public QMainWindow
    54. {
    55. Q_OBJECT
    56.  
    57. public:
    58. explicit baseclass(QWidget *parent = 0);
    59. ~baseclass();
    60. // baseclass(int i = 10) : member(i){}
    61. // int getMember() {return member;}
    62. int i;
    63. Ui::baseclass *ui;
    64.  
    65. public slots:
    66. void on_pushButton_clicked();
    67. void test();
    68.  
    69.  
    70.  
    71. private:
    72.  
    73.  
    74. private slots:
    75.  
    76.  
    77.  
    78. private slots:
    79. void on_lineEdit_cursorPositionChanged(int , int );
    80. };
    81.  
    82. #endif // BASECLASS_H
    83.  
    84.  
    85. //baseclass.cpp
    86. #ifndef BASECLASS_H
    87. #define BASECLASS_H
    88.  
    89. #include <QMainWindow>
    90.  
    91. namespace Ui {
    92. class baseclass;
    93. }
    94.  
    95. class baseclass : public QMainWindow
    96. {
    97. Q_OBJECT
    98.  
    99. public:
    100. explicit baseclass(QWidget *parent = 0);
    101. ~baseclass();
    102. // baseclass(int i = 10) : member(i){}
    103. // int getMember() {return member;}
    104. int i;
    105. Ui::baseclass *ui;
    106.  
    107. public slots:
    108. void on_pushButton_clicked();
    109. void test();
    110.  
    111.  
    112.  
    113. private:
    114.  
    115.  
    116. private slots:
    117.  
    118.  
    119.  
    120. private slots:
    121. void on_lineEdit_cursorPositionChanged(int , int );
    122. };
    123.  
    124. #endif // BASECLASS_H
    125.  
    126. //mai.cpp
    127. #ifndef BASECLASS_H
    128. #define BASECLASS_H
    129.  
    130. #include <QMainWindow>
    131.  
    132. namespace Ui {
    133. class baseclass;
    134. }
    135.  
    136. class baseclass : public QMainWindow
    137. {
    138. Q_OBJECT
    139.  
    140. public:
    141. explicit baseclass(QWidget *parent = 0);
    142. ~baseclass();
    143. // baseclass(int i = 10) : member(i){}
    144. // int getMember() {return member;}
    145. int i;
    146. Ui::baseclass *ui;
    147.  
    148. public slots:
    149. void on_pushButton_clicked();
    150. void test();
    151.  
    152.  
    153.  
    154. private:
    155.  
    156.  
    157. private slots:
    158.  
    159.  
    160.  
    161. private slots:
    162. void on_lineEdit_cursorPositionChanged(int , int );
    163. };
    164.  
    165. #endif // BASECLASS_H
    166.  
    167.  
    168. //derivedclass.cpp
    169. #include "derivedclass.h"
    170. #include "ui_derivedclass.h"
    171. #include "baseclass.h"
    172.  
    173. derivedclass::derivedclass(QWidget *parent) :
    174. baseclass(parent),
    175. ui(new Ui::derivedclass)
    176. {
    177. ui->setupUi(this);
    178. test();
    179.  
    180. QString v=QString::number(i);
    181.  
    182. ui->lineEdit->setText(v);
    183.  
    184.  
    185. }
    186.  
    187. derivedclass::~derivedclass()
    188. {
    189.  
    190. delete ui;
    191. }
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How can we inherit ui?

    The same source is pasted few times.
    If you want the base and derived class to use the same ui form, you don't have to initialize it in both classes constructors, just in base class:
    Qt Code:
    1. baseclass::baseclass(QWidget * parent) : ui(new ui::baseclass)
    2. {
    3. ...
    4. }
    5.  
    6. ...
    7.  
    8. derivedclass::derivedclass(QWidget *parent) :
    9. baseclass(parent) // ui(new Ui::baseclass) and ui->setupUi() will be called here, no need to call it again
    10. {
    11. test();
    12.  
    13. QString v=QString::number(i);
    14.  
    15. ui->lineEdit->setText(v);
    16. }
    To copy to clipboard, switch view to plain text mode 
    Anyway, it's not clear to me what do you want to achieve.

Similar Threads

  1. About inherit
    By nesson in forum Qt Programming
    Replies: 1
    Last Post: 8th February 2011, 13:07
  2. Inherit from QTabWidget
    By Suncell in forum Newbie
    Replies: 2
    Last Post: 27th June 2010, 21:06
  3. How to use the ui_*.h,inherit it or as a member?
    By 75543255 in forum Qt Programming
    Replies: 1
    Last Post: 30th August 2009, 10:45
  4. Inherit the QAxWidget
    By MrShahi in forum Qt Programming
    Replies: 3
    Last Post: 29th April 2008, 08:06
  5. Object Inherit from QTreeWidgetItem
    By xgoan in forum Qt Programming
    Replies: 6
    Last Post: 18th August 2006, 12:20

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.