Results 1 to 7 of 7

Thread: templates / Q_OBJECT question

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: templates / Q_OBJECT question

    I admit I haven't read the whole post, but it looks like templates are not for you - you can't decide which class to create during runtime, the template argument has to be specified during compilation. But if you think templates are for you, then make some object that is not derived from QObject and make it a member of your class - QObjects can't be templates, but they can have a member pointer that is a base class of a template class.

    Qt Code:
    1. class ... {
    2. Base *x;
    3. };
    4.  
    5. class Base{};
    6. template .... class X : public Base {...};
    To copy to clipboard, switch view to plain text mode 

  2. The following user says thank you to wysota for this useful post:

    _borker_ (19th December 2007)

  3. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 541 Times in 521 Posts

    Default Re: templates / Q_OBJECT question

    I think you can avoid templates. Why not using only a factory class + an abstract(basically for preventing instantiating this meaningless class) node class + node implementations?
    The factory class will return Node(abstract) objects which you could identify later by adding a getType pure virtual method which subclasses actually implement.

    For example:
    Qt Code:
    1. AbstractNode *node = NodeFactory.CreateNode...;
    2. switch(node->type())
    3. {
    4. ...
    5. case AbstractNode::eTypeANode:
    6. {
    7. dynamic_cast<TypeANode*>(node)->SomeTypeANodeSpecializedMethod();
    8. break;
    9. }
    10. ...
    11. }
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to marcel for this useful post:

    _borker_ (19th December 2007)

  5. #3
    Join Date
    Dec 2007
    Posts
    4
    Thanks
    2

    Default Re: templates / Q_OBJECT question

    thanks Marcel, that looks like a good idea. From what you and wysota have said, steering clear of templates might well be the best idea here.

    cheers
    borker

  6. #4
    Join Date
    Dec 2007
    Posts
    4
    Thanks
    2

    Default Re: templates / Q_OBJECT question

    thanks for the reply

    When the query is created the return type will be known so it's possible to know the desired return type at compile time that will be what is used as the template class.

    I'm mainly searching for a way to implement a behavior pattern that is more or less generic without having to create subclasses that don't do much more than just handle different return types.

    I'll have a play around this evening with what you mentioned in your reply and see if that solves my problems

    cheers
    borker

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

    Default Re: templates / Q_OBJECT question

    What "return types" are we talking about here? Maybe QVariant is what you need?

  8. #6
    Join Date
    Dec 2007
    Posts
    4
    Thanks
    2

    Default Re: templates / Q_OBJECT question

    hmm, never really played around with QVariants, might be an option there also... thanks jacek

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
  •  
Qt is a trademark of The Qt Company.