Results 1 to 2 of 2

Thread: Instantiate objects only having their class name

  1. #1
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Instantiate objects only having their class name

    Hi! I have a QThread where I perform SQL queries. Now that my application has grown, I want to many it modular by creating modules that live in that thread, i.e. classes that are instantiated and perform their work in that thread.

    However, I would like to do it in a way the QThread doesn't need to know of each of the classes it has. It would just get the list of class names and instantiate them without needing to know which are their constructors.

    I thought of registering every module as a metatype, then passing the class name to the QThread and using QMetaType::construct() in it to be able to instantiate them. It would look like this:

    Qt Code:
    1. class DbThread : public QThread
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. DbThread(QObject *parent = 0);
    7. void constructExtensions();
    8. void queryExtensions();
    9. void registerExtension(const QString& type);
    10.  
    11. private:
    12. QStringList m_extensionNames;
    13. QList<Extension*> m_extensions;
    14. };
    15.  
    16.  
    17. void DbThread::constructExtensions()
    18. {
    19. foreach(const QString& className, m_extensionNames) {
    20. QByteArray classNameBA = className.toLocal8Bit();
    21. int id = QMetaType::type(classNameBA.constData());
    22. if(id != -1) {
    23. void *myClassPtr = QMetaType::construct(id);
    24. if(myClassPtr == 0) {
    25. qDebug() << "Failed instantiating the module:" << className;
    26. continue;
    27. }
    28. Extension *extension = (Extension*) myClassPtr;
    29. extension->setParent(this);
    30. m_extensions.append(extension);
    31. }
    32. }
    33. }
    34.  
    35.  
    36. void DbThread::queryExtensions()
    37. {
    38. foreach(Extension *ext, m_extensions)
    39. ext->doSomething();
    40. }
    41.  
    42.  
    43. void DbThread::registerExtension(const QString& type)
    44. {
    45. m_extensionNames << type;
    46. }
    To copy to clipboard, switch view to plain text mode 

    I use the class Extension which is in fact an interface. Other modules are subclasses of it. For example:

    Qt Code:
    1. #include <QMetaType>
    2. #include <QObject>
    3. #include <QString>
    4. #include "extension.h"
    5.  
    6. class CoolExtension : public Extension
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. CoolExtension(QObject *parent = 0);
    12. CoolExtension(const CoolExtension& other);
    13. void doSomething();
    14. };
    15.  
    16. Q_DECLARE_METATYPE(CoolExtension)
    To copy to clipboard, switch view to plain text mode 

    Then I would call DbThread::registerExtension("CoolExtension") so that constructExtensions() would instantiate one object of the CoolExtension class.

    That is the idea. However the application segfaults after I try to use the pointer QMetaType::construct() gave me. The pointer is not null but it seems QMetaType::construct() is not doing what I expect or I can't convert it properly. What might be wrong?

    Do you have a better idea on how to create modules that must be instantiated by a QThread since they live in it? Perhaps should I instantiate them somewhere else and use QThread::moveToThread()? But I don't like the idea of instantiating in one thread and moving to another one.

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Instantiate objects only having their class name

    You have to register "pointers to classes" as the metatype, not classes themselves. Although I'm not sure construction will work properly with pointers. It might be much easier if you just provided factories for the classes using the plugin infrastructure.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QWizard creating objects dynamically
    By afflictedd2 in forum Qt Programming
    Replies: 5
    Last Post: 24th March 2009, 15:43
  2. Replies: 1
    Last Post: 25th July 2008, 23:44
  3. class QHBoxLayout
    By csvivek in forum Installation and Deployment
    Replies: 2
    Last Post: 10th April 2008, 07:57
  4. using class objects globally??????
    By pratik in forum Qt Programming
    Replies: 2
    Last Post: 9th July 2007, 13:51
  5. Replies: 2
    Last Post: 4th May 2006, 19:17

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.