Results 1 to 4 of 4

Thread: hello! i have a simple child widgets question

  1. #1
    Join Date
    Nov 2007
    Posts
    47
    Thanks
    34
    Qt products
    Qt4
    Platforms
    Windows

    Default hello! i have a simple child widgets question

    Hello. I'm trying to make a widget and a child widget. I'd like Widget2 to be a child of Widget. Would anybody be able to check if I'm doing it right. Thanx

    main.cpp:
    Qt Code:
    1. .......
    2. Widget2::Widget2(Widget* parent): Widget(parent)
    3. {
    4. }
    5.  
    6. Widget::Widget(QWidget* parent): QWidget(parent)
    7. {
    8. }
    9. ......
    To copy to clipboard, switch view to plain text mode 

    hfile.h
    Qt Code:
    1. #ifndef HFILE_H
    2. #define HFILE_H
    3.  
    4. #include <QtGui>
    5.  
    6. class Widget : public QWidget
    7. {
    8. Q_OBJECT
    9. public:
    10. Widget (QWidget* parent = 0);
    11. protected:
    12. private:
    13. };
    14.  
    15. class Widget2 : public Widget
    16. {
    17. Q_OBJECT
    18. public:
    19. Widget2 (Widget* QWidget);
    20. protected:
    21. private:
    22. };
    23. #endif
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Alingsås, Sweden
    Posts
    437
    Thanks
    3
    Thanked 39 Times in 39 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: hello! i have a simple child widgets question

    Your code makes Widget2 inherit Widget (which, in turn inherits QWidget). This gives you an is-a relationship, i.e. Widget2 is-a Widget. This is usually not what you call a child widget.

    Child widgets are widgets that have a parent. This is called a has-a relationship. The parent has-a child. For example:

    Qt Code:
    1. QWidget *w1, *w2;
    2.  
    3. w1 = new QWidget();
    4. w2 = new QWidget( w1 );
    To copy to clipboard, switch view to plain text mode 

    Here w1 has-a w2, i.e. w2 is a child widget to w1.

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

    ht1 (17th November 2007)

  4. #3
    Join Date
    Nov 2007
    Posts
    47
    Thanks
    34
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: hello! i have a simple child widgets question

    Thanx for your clarification.
    So, in your example w2 is a child of w1, right?

    Can any QWidget be a child of another?
    For example: can QWidget be a child of QLabel, or only the other way around is allowed(because of the resaons how they inherit from one another)?

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: hello! i have a simple child widgets question

    Quote Originally Posted by ht1 View Post
    Can any QWidget be a child of another?
    Yes, but sometimes it doesn't make much sense (like making a scrollbar a child of a pushbutton).

Similar Threads

  1. QTextEdit simple question
    By Marcopolo in forum Qt Tools
    Replies: 4
    Last Post: 11th October 2007, 00:01
  2. Designer Question About Sizing Widgets
    By pmabie in forum Qt Tools
    Replies: 1
    Last Post: 23rd September 2007, 11:05
  3. setClipPath on child widgets.
    By bunjee in forum Qt Programming
    Replies: 9
    Last Post: 27th May 2007, 19:12
  4. initialize child widgets within parent?
    By ucomesdag in forum Newbie
    Replies: 6
    Last Post: 6th June 2006, 08:11
  5. Replies: 2
    Last Post: 22nd February 2006, 14:58

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.