Results 1 to 6 of 6

Thread: Does Qt provide a mechanism to instantiate an object by name (string)?

  1. #1
    Join Date
    Oct 2011
    Posts
    35
    Thanked 9 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Does Qt provide a mechanism to instantiate an object by name (string)?

    I'm wondering if Qt (perhaps through the meta-object system) provides a mechanism to instantiate a QObject-derived object simply by specifying the type's name. The use case here would be to read a class' name from a configuration file and instantiate the appropriate type (without having messy if...else if...else logic). The QMetaObject documentation shows there is a newInstance() method, but that doesn't quite do what I'm looking for (i.e., creating an object from a string name).

  2. #2
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Does Qt provide a mechanism to instantiate an object by name (string)?

    Yes it does, read manual about QMetaType::details. First example shows how to do it.

  3. #3
    Join Date
    Oct 2011
    Posts
    35
    Thanked 9 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Does Qt provide a mechanism to instantiate an object by name (string)?

    Excellent, thank you very much!

  4. #4
    Join Date
    Jun 2011
    Posts
    69
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Does Qt provide a mechanism to instantiate an object by name (string)?

    What about template programming ?
    ...یه مرد هیچوقت زمین نمیخوره

  5. #5
    Join Date
    Oct 2011
    Posts
    35
    Thanked 9 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Does Qt provide a mechanism to instantiate an object by name (string)?

    Alir3z4, could you share your thoughts on how you would use template programming to accomplish this? If one is reading information (strings) from a configuration file, I'm not sure how template programming can help to get around having if...else if...else logic to instantiate the appropriate templated-type. How were you thinking of using templates for this?

  6. #6
    Join Date
    Jun 2011
    Posts
    69
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Does Qt provide a mechanism to instantiate an object by name (string)?

    Oh, i misunderstood by it, i tought you mean by the type, but as you mentioned creating an object from a string name
    ...یه مرد هیچوقت زمین نمیخوره

Similar Threads

  1. Getting std::string object from QString object ( qt3)
    By joseph in forum Qt Programming
    Replies: 11
    Last Post: 28th March 2013, 20:09
  2. Cannot instantiate Plugin with UI
    By trallallero in forum Qt Programming
    Replies: 1
    Last Post: 30th October 2010, 09:34
  3. object to string
    By babu198649 in forum General Programming
    Replies: 5
    Last Post: 31st January 2008, 08:01
  4. Please instantiate the QApplication object first
    By zorro68 in forum Qt Programming
    Replies: 7
    Last Post: 25th October 2007, 12:20
  5. QAxContainer: can't instantiate control
    By fabien in forum Qt Programming
    Replies: 0
    Last Post: 24th July 2007, 11:22

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.