Results 1 to 3 of 3

Thread: How to cast QObject to custom QWidget in abstract factory pattern?

  1. #1
    Join Date
    Jan 2015
    Location
    Barquisimeto, Venezuela
    Posts
    24
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Android

    Default How to cast QObject to custom QWidget in abstract factory pattern?

    Hi all, Have a nice day.

    I am new to the forum and is my first post, I reviewed most of this forum and the web and not get anything definitive to help me understand the problem I am waiting here can help me as I often they have done in other post for other people.

    I am trying to apply the abstract factory pattern to create a series of modules that require to an application I'm developing.

    I have taken an example from the book An Introduction to Design Patterns in C ++ With Qt (Prentice Hall-2011-2nd Edition) page 483, paragraph 16.1.1 Abstract Factory
    Chapter 16, as well as, I read on the website of some other examples and
    I have the following code to do what I need.

    Qt Code:
    1. typedef QHash<QString, QMetaObject> T_Modules;
    2.  
    3. T_Modules modulesTypesList () {
    4.  
    5. T_Modules hash;
    6.  
    7. hash [ "LibraryGui" ] = LibraryGui::staticMetaObject;
    8. hash [ "ConvertGui" ] = ConvertGui::staticMetaObject;
    9. hash [ "BurnGui" ] = BurnGui::staticMetaObject;
    10. hash [ "PlaylistGui" ] = PlaylistGui::staticMetaObject;
    11. hash [ "PlayingGui" ] = PlayingGui::staticMetaObject;
    12.  
    13. return hash;
    14. }
    15.  
    16. Module *ModulesFactory::createModule ( QString className, QWidget *parent ) {
    17.  
    18. T_Modules modulesType = modulesTypesList ();
    19.  
    20. Module *moduleCreated = 0;
    21.  
    22. if ( modulesType.contains ( className ) ) {
    23.  
    24. const QMetaObject& metaObjectClass = modulesType [ className ];
    25. moduleCreated = qobject_cast<Module *> ( metaObjectClass.newInstance () );
    26.  
    27. if ( moduleCreated == 0 ) {
    28.  
    29. qDebug () << "Error creating " << className;
    30. abort ();
    31.  
    32. } else if ( parent != 0 ) {
    33.  
    34. moduleCreated->setParent ( parent );
    35. }
    36. } else {
    37.  
    38. qDebug () << "Error creating " << className;
    39. abort ();
    40. }
    41. return moduleCreated;
    42. }
    To copy to clipboard, switch view to plain text mode 

    In theory everything is fine (compiles complete), but when running, it generates the
    following error:

    Qt Code:
    1. Starting /home/remizero/Proyectos/Qt/rockola/build-rockola-Desktop-Debug/rockola...
    2. Error creating "LibraryGui"
    3. The program has unexpectedly finished.
    4. /home/remizero/Proyectos/Qt/rockola/build-rockola-Desktop-Debug/rockola crashed
    To copy to clipboard, switch view to plain text mode 

    Of course, not a bug properly, the detail is that I did not create the object (send to abort to avoid having to close the application and to debug faster; p)

    My question is, What am I doing wrong trying to create object of type Module? on line 25

    Qt Code:
    1. moduleCreated = qobject_cast<Module *> ( metaObjectClass.newInstance () );
    To copy to clipboard, switch view to plain text mode 

    I'm working with QtCreator 3.2.1, Qt 5.3.2, gcc 4.9.1 32bit

    If you need a compilable example, I'll have to pass on the project with a minimum of 20 files and tell the file in question to go directly.

    Sorry for my bad English.

    Thanks in advance.

  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: How to cast QObject to custom QWidget in abstract factory pattern?

    Does your class inherit Module? Does Module have a Q_OBJECT macro? Does newInstance() return a valid pointer?
    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.


  3. #3
    Join Date
    Jan 2015
    Location
    Barquisimeto, Venezuela
    Posts
    24
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Android

    Default Re: How to cast QObject to custom QWidget in abstract factory pattern?

    Goodnight wysota. Thanks for responding so quickly.

    Yes, my class inherits from Module and also has the Q_OBJECT macro. newInstance() returned a pointer to 0, which was driving me crazy lol.

    Reviewing some structure in my class LibraryGui, the error had it in the constructor of the class, because I was missing the macro Q_INVOKABLE, which did not recognize the constructor, but still was still presenting another error is that the constructor required parameters and that I solved using the macro Q_ARG (MyObject * myArg), leaving my code section as follows:

    Qt Code:
    1. typedef QHash<QString, QMetaObject> T_Modules;
    2.  
    3. T_Modules modulesTypesList () {
    4.  
    5. T_Modules hash;
    6.  
    7. hash [ "LibraryGui" ] = LibraryGui::staticMetaObject;
    8. hash [ "ConvertGui" ] = ConvertGui::staticMetaObject;
    9. hash [ "BurnGui" ] = BurnGui::staticMetaObject;
    10. hash [ "PlaylistGui" ] = PlaylistGui::staticMetaObject;
    11. hash [ "PlayingGui" ] = PlayingGui::staticMetaObject;
    12.  
    13. return hash;
    14. }
    15.  
    16. Module *ModulesFactory::createModule ( QString className, QWidget *parent ) {
    17.  
    18. T_Modules modulesType = modulesTypesList ();
    19.  
    20. Module *moduleCreated = 0;
    21.  
    22. if ( modulesType.contains ( className ) ) {
    23.  
    24. const QMetaObject& metaObjectClass = modulesType [ className ];
    25. [B][I][U]moduleCreated = qobject_cast<QAction *> ( metaObjectClass.newInstance ( Q_ARG ( QWidget*, parent ) ) );[/U][/I][/B]
    26.  
    27. if ( moduleCreated == 0 ) {
    28.  
    29. qDebug () << "Error creating " << className;
    30. abort ();
    31.  
    32. } else if ( parent != 0 ) {
    33.  
    34. moduleCreated->setParent ( parent );
    35. }
    36. } else {
    37.  
    38. qDebug () << "Error creating " << className;
    39. abort ();
    40. }
    41. return moduleCreated;
    42. }
    To copy to clipboard, switch view to plain text mode 

    And in my class LibraryGui, which inherits from Module, is as follows:

    Qt Code:
    1. class LibraryGui : public Module {
    2.  
    3. Q_OBJECT
    4.  
    5. public:
    6. Q_INVOKABLE explicit LibraryGui ( QWidget *parent = 0 );
    7. ...
    8. }
    To copy to clipboard, switch view to plain text mode 

    Thank you very much for your questions because I did check with my code more carefully.

    So I close this thread.

    Thank you very much again and hope that this error is a reminder to other programmers colleagues.

    PS: I do not get as placing "solved" in the title of the post
    Last edited by remizero; 21st February 2015 at 04:21.

Similar Threads

  1. Factory design pattern issue
    By ayanda83 in forum Newbie
    Replies: 10
    Last Post: 27th August 2014, 11:45
  2. Replies: 0
    Last Post: 12th September 2011, 15:18
  3. abstract custome QWidget class
    By migel in forum Newbie
    Replies: 1
    Last Post: 12th July 2011, 10:41
  4. Replies: 1
    Last Post: 30th October 2010, 13:28
  5. QObject factory (singleton pattern)
    By area51 in forum Qt Programming
    Replies: 8
    Last Post: 24th June 2010, 15:55

Tags for this Thread

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.