PDA

View Full Version : To subclass or not to subclass - that is the question !



fassage
9th February 2010, 11:20
Dear All,

In my gui application, i have noticed that i am using many times a "qlabel + qslider + qlineedit" combination.
As such, i was thinking to sublass these and their assosicated functionality - which i assume is good practice.
I am struggling however, with the following issues and would really appreciate any help you could provide.

1. Should i subclass a QWidget or a QHBox ? What are the reasons for the choice ?

2. The main problem is the layout manager at the parent level. I am using a QGridLayout to manage many other widgets together with several instances of this new subclass. When i have the "qlabel + qslider + qlineedit" flat in the parent the layout is perfect, but once i subclass with either QWidget or QHBox, the layout of the parent widget is messed up ? I want it to be as if it were flat in the parent ? Is this possible ?

I know you are going to ask me to post some code, and i would really like to but theres so much of it.

I am in the process of making a testcase for you guys in the meantime.

Many thanks in advance.
:confused:

fassage
9th February 2010, 14:51
I would really appreciate some input - really really struggling here !!!!!!!! :(

boudie
9th February 2010, 15:06
Maybe you can post an image of how bad it looks?

fassage
9th February 2010, 18:16
Ok Guys !

Since no one replied to my post with a testcase - then here it is attached as xxx.zip !!!!

Heres a summary of the files:
window.h - qmainwindow that only has a tab widget
gtab4 - widget subclass of a tab inserted into window->tabwidget
gedit.h is the attempted implementation of subclassing

Can i draw your attention to the file gtab4.h. Here i have created two scenarios. you will see the lines commented out are the flat implementation of and the ones in there now are the sublass gedit z1 and z2 objects.

I have also tried to implement gedit.h by subclassing qwidget and having a qhboxlayout rather than the qhbox attached but this didnt make a difference !

:mad::mad: PLEASE PLEASE HELP AS THIS IS INFURIATING ME !!!!!!!!!!! :mad::mad:

franz
9th February 2010, 21:42
Use QWidget with a QHBoxLayout.

You know, your last post isn't really helping you -- You should probably read this (http://www.catb.org/~esr/faqs/smart-questions.html).

fassage
10th February 2010, 07:44
Thanks for the 1 reply.

I read your link and it is a useful guide. I will try to take your feedback constructively but it would have been more useful if your pointer was more specific. Do u mean i should have chosen my forum more carefully, or another issue ?

In terms of your answer, well as i said in my previous 2 posts, it didnt work. Subclassing qwidget with a qhboxlayout still suffers the same layout issues one level up !

I will try again then : The main goal is to subclass a few widgets in order to reuse them, but maintain the way they are handled by their parent and its layout manager as if they were placed flat in the parent widget.

It doesnt sound too hard, i have tried many docs and attepts and failed.

Your help would be immense.

franz
10th February 2010, 22:04
Good. The forum is probably the good one. The attitude is what needed improvement.

Do I understand correctly that you want to synchronize the contents of the layouts, more specifically have the columns aligned?

pitonyak
11th February 2010, 21:42
I assume that you will create a widget (as in you will create a class that is derived from QWidget). You can then encapsulate all of this into a single widget that you can easily reuse. I assume this, because I believe that you want to have three other widgets contained in a single widget. If I misunderstood, and your desire is to have all of the functionalities, then you can just derive from the widget of your choice.

If you desire that your widget will be available in the designer, then you should choose that from which you derive your new widget carefully, because this will impact which properties are available in the designer.

Coises
12th February 2010, 02:41
The main problem is the layout manager at the parent level. I am using a QGridLayout to manage many other widgets together with several instances of this new subclass. When i have the "qlabel + qslider + qlineedit" flat in the parent the layout is perfect, but once i subclass with either QWidget or QHBox, the layout of the parent widget is messed up ? I want it to be as if it were flat in the parent ? Is this possible ?

If I understand what you want, I’m fairly sure it isn’t possible, unless possibly by writing your own layout manager. If you add something (whether a QWidget or a QLayout) as a single item to another layout, the internals of the child layout are opaque to the container. It will never behave in the same way as if you added the three widgets as three different layout items, because the containing layout can’t see three items, only one.

How about creating a new class that contains QPointers to the three widgets you want as members? In the constructor, create the three widgets and store their addresses. In the destructor, check that each QPointer::isNull returns false before deleting the associated widget.

Then write member functions for the operations you will need, including an operation to add the widgets to a QGridLayout. For example, if they are always added in the same order to adjoining columns in the same row and the alignment of each is always the same, you would only need to pass this function a QGridLayout pointer, a row number and the first column number.

If you will want your class to manage the signals and slots of the three widgets, then you’ll need to derive it from QObject, include a Q_OBJECT macro, and make any necessary connections between signals from the widgets and private slots in your class in the constructor. You shouldn’t need to connect any signals to the widgets’ slots, as you can call those functions directly from your own public slots.