Results 1 to 9 of 9

Thread: To subclass or not to subclass - that is the question !

  1. #1
    Join Date
    Apr 2009
    Posts
    20
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Angry To subclass or not to subclass - that is the question !

    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.

  2. #2
    Join Date
    Apr 2009
    Posts
    20
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Angry Re: To subclass or not to subclass - that is the question !

    I would really appreciate some input - really really struggling here !!!!!!!!

  3. #3
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: To subclass or not to subclass - that is the question !

    Maybe you can post an image of how bad it looks?

  4. #4
    Join Date
    Apr 2009
    Posts
    20
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Angry Re: To subclass or not to subclass - that is the question !

    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 !

    PLEASE PLEASE HELP AS THIS IS INFURIATING ME !!!!!!!!!!!
    Attached Files Attached Files

  5. #5
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: To subclass or not to subclass - that is the question !

    Use QWidget with a QHBoxLayout.

    You know, your last post isn't really helping you -- You should probably read this.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  6. #6
    Join Date
    Apr 2009
    Posts
    20
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: To subclass or not to subclass - that is the question !

    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.

  7. #7
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: To subclass or not to subclass - that is the question !

    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?
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  8. #8
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: To subclass or not to subclass - that is the question !

    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.

  9. #9
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: To subclass or not to subclass - that is the question !

    Quote Originally Posted by fassage View Post
    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.
    Last edited by Coises; 12th February 2010 at 03:23.

Similar Threads

  1. How to subclass QAbstractProxyModel
    By skycrestway in forum Qt Programming
    Replies: 2
    Last Post: 22nd October 2010, 23:35
  2. How to subclass QTableWidgetItem
    By codemonkey in forum Qt Programming
    Replies: 4
    Last Post: 4th October 2009, 11:11
  3. subclass of QFileDialog
    By afail in forum Qt Programming
    Replies: 1
    Last Post: 18th May 2009, 17:09
  4. Subclass and Connections
    By merry in forum Newbie
    Replies: 10
    Last Post: 6th March 2007, 13:19
  5. Subclass
    By merry in forum General Programming
    Replies: 2
    Last Post: 1st March 2007, 10:34

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.