PDA

View Full Version : hello! i have a simple child widgets question



ht1
17th November 2007, 22:45
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:


.......
Widget2::Widget2(Widget* parent): Widget(parent)
{
}

Widget::Widget(QWidget* parent): QWidget(parent)
{
}
......


hfile.h



#ifndef HFILE_H
#define HFILE_H

#include <QtGui>

class Widget : public QWidget
{
Q_OBJECT
public:
Widget (QWidget* parent = 0);
protected:
private:
};

class Widget2 : public Widget
{
Q_OBJECT
public:
Widget2 (Widget* QWidget);
protected:
private:
};
#endif

e8johan
17th November 2007, 23:40
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:



QWidget *w1, *w2;

w1 = new QWidget();
w2 = new QWidget( w1 );


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

ht1
17th November 2007, 23:52
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)?

jacek
18th November 2007, 00:49
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).