Results 1 to 2 of 2

Thread: How to use the ui_*.h,inherit it or as a member?

  1. #1
    Join Date
    Aug 2009
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question How to use the ui_*.h,inherit it or as a member?

    If you use QtCreator the UI file will be generate a UI_*.h file.
    In the QtCreator they use it as a member . I think which I make is a window,not has a window.
    For example:
    Qt Code:
    1. //Use it as a member
    2. class MainWin : public QMainWindow
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. MainWin(QWidget *parent = 0);
    8. ~MainWin();
    9.  
    10. private:
    11. Ui::MainWin *ui;//a member value
    12. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //Inherit as a base class
    2. class MainWin : public QMainWindow,public UI::MainWin//Multiple inheritance
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. MainWin(QWidget *parent = 0);
    8. ~MainWin();
    9.  
    10. };
    To copy to clipboard, switch view to plain text mode 

    "is a" means to inherit it as base class,
    "has a"means use it as member value,
    Athough two ways all can work,but I want to know which is better,what's difference between them.
    Why the QtCreator use it as a member?is the way better?
    Last edited by 75543255; 30th August 2009 at 12:27.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to use the ui_*.h,inherit it or as a member?

    Quoting docs:
    The Single Inheritance Approach
    ...
    The advantages of this approach are its simple use of inheritance to provide a QWidget-based interface, and its encapsulation of the user interface widget variables within the ui data member. We can use this method to define a number of user interfaces within the same widget, each of which is contained within its own namespace, and overlay (or compose) them. This approach can be used to create individual tabs from existing forms, for example.

    The Multiple Inheritance Approach
    ...
    In this case, the widgets used in the user interface can be accessed in the same say as a widget created in code by hand. We no longer require the ui prefix to access them.

    Subclassing using multiple inheritance gives us more direct access to the contents of the form, is slightly cleaner than the single inheritance approach, but does not conveniently support composition of multiple user interfaces.
    Some people prefer the multiple inheritance approach because they say it's cleaner. Other people prefer the single inheritance approach because it makes the code more readable. I'd say it's a matter of taste. Feel free to use the approach that makes you feel more comfortable.
    J-P Nurmi

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

    75543255 (30th August 2009)

Similar Threads

  1. Replies: 22
    Last Post: 8th October 2008, 13:54
  2. Replies: 4
    Last Post: 19th March 2008, 17:47
  3. problem with forward declaration
    By MarkoSan in forum General Programming
    Replies: 14
    Last Post: 6th January 2008, 21:45
  4. Pointers to Member Functions
    By Doug Broadwell in forum General Programming
    Replies: 8
    Last Post: 15th May 2007, 23:08
  5. error:invalid use of member
    By quickNitin in forum General Programming
    Replies: 4
    Last Post: 19th June 2006, 15:21

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.