Results 1 to 8 of 8

Thread: Dynamic objects without identifier...

  1. #1
    Join Date
    Jul 2009
    Location
    Dundee, United Kingdom
    Posts
    18
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Dynamic objects without identifier...

    Hi,
    I'm facing a problem which I can't overcome... In a for() loop I'm creating objects of 3 different classes depending on an if() statement:

    Qt Code:
    1. if(type == "string")
    2. {
    3. QLineEdit *string = new QLineEdit(groupBox);
    4. groupBox->layout()->addWidget(string);
    5. }
    6. else if(type == "date")
    7. {
    8. QDateEdit *date = new QDateEdit(groupBox);
    9. groupBox->layout()->addWidget(date);
    10. }
    11. else if(type == "image")
    12. {
    13. QLabel *image = new QLabel("image",groupBox);
    14. groupBox->layout()->addWidget(image);
    15. }
    To copy to clipboard, switch view to plain text mode 

    Now it would be nice to have some sort of array of those objects with an ID key, so I can lookup a specific object depending on the ID and change it's contents. I've tried QHash, but I would end up with 3 or more class fields of type QHash<int, className*>.

    Do you have any better suggestion?
    Thanks in advance,
    Regards,

    Bill

  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: Dynamic objects without identifier...

    QHash<int, QWidget*> and qobject_cast to determine the type.
    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
    Jul 2009
    Location
    Dundee, United Kingdom
    Posts
    18
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Dynamic objects without identifier...

    Hi,
    Thanks for your reply. I've decided to use a QObjectList which I populate in the if statements, where additionally I set the proper object names used later to identify the object "type" (image, string etc.). Works pretty well.

    Regards,
    Bill

  4. #4
    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: Dynamic objects without identifier...

    It might be better to use QList<QWidget*> - it's better for you than QList<QObject*> because you can use the widget interface without any casting. If you want to stick with QObjectList, then you don't need it at all - at any time you can call QObject::children() on the parent widget (or the layout - you'd have to check that out) that will return you a QObjectList of its children.
    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.


  5. #5
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: Dynamic objects without identifier...

    QList<QString, QMetaObject>. Store the classes meta objects and then use QMetaObject::newInstance to create new instances.
    It's nice to be important but it's more important to be nice.

  6. #6
    Join Date
    Jul 2009
    Location
    Dundee, United Kingdom
    Posts
    18
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Dynamic objects without identifier...

    Hi,
    Thanks for the QMetaObject hint - I'll look at it in detail...
    Another problem I have is that I create an object and add it to a QScrollArea like this:
    Qt Code:
    1. scrollAreaWidgetContents->layout()->addWidget(date);
    To copy to clipboard, switch view to plain text mode 

    What would be the best way to remove objects from the QScrollArea, which have a pointer stored in a QList<QWidget*>?
    I've tried
    Qt Code:
    1. for(int i=0;i<formElements.length();++i) scrollAreaWidgetContents->layout()->removeWidget(formElements.at(i));
    To copy to clipboard, switch view to plain text mode 
    but end up with a segfault.

    Thanks for your help,
    Reagards,
    Bill

  7. #7
    Join Date
    Jul 2009
    Location
    Dundee, United Kingdom
    Posts
    18
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Dynamic objects without identifier...

    I think I found a solution.
    After a bit of RTFM,
    Qt Code:
    1. while(!formElements.isEmpty()) delete formElements.takeFirst();
    To copy to clipboard, switch view to plain text mode 

    works well.

    Regards,
    Bill

  8. #8
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: Dynamic objects without identifier...

    Does qDeleteAll works as well?
    It's nice to be important but it's more important to be nice.

Similar Threads

  1. Load objects from dynamic library
    By Trok in forum Qt Programming
    Replies: 10
    Last Post: 17th July 2009, 20:04
  2. Deleting objects in their event handler
    By drhex in forum Qt Programming
    Replies: 7
    Last Post: 6th May 2009, 16:08
  3. QWizard creating objects dynamically
    By afflictedd2 in forum Qt Programming
    Replies: 5
    Last Post: 24th March 2009, 15:43
  4. Lumina GLSL IDE based on QtScript and dynamic Objects
    By oc2k1 in forum Qt-based Software
    Replies: 0
    Last Post: 12th August 2008, 04:12
  5. lib("dnsapi")
    By CHeader in forum Newbie
    Replies: 10
    Last Post: 16th February 2008, 19:21

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.