[Solved] How to create an instance of a widget from a string?



  • I am storing objects of a class in a QList, The class contains information about the widget and its positioning inside a layout eg. GridLayout. In the class I have a getter and setter functions that returns the Widget name like "QLabel","QLineEdit" in string format. How can i instantiate these widgets based on the string name?

    Thanks :)


  • Moderators

    Not much info, but this looks like a very crazy setup. Layouts usually manage themselves, you should use that.

    As for instantiation:
    @
    if (myString == "QLabel")
    label = new QLabel(this);
    @

    But I'm almost certain that is not what you have meant.



  • Well... perhaps you should look at [[doc:QUiLoader]]? It also works based on a string, although that string is in XML format. But, indeed, it is not a normal setup to use this.



  • #sierdzio

    Yes as of now i am using the same approch that you have provided, But the problem is that if i have 10 different widgets then foreach widget i have to write

    @if(myString =="QLabel")
    label = new QLabel(this);
    label->setObjectName(/obj->getObjectName/);
    label->setText(/obj->getText/);
    layout->addWidget(label,/obj->getRow/,/obj->getColumn/,/obj->getRowSpan/,/obj->getColumnSpan/);

    ......
    ......
    if(myString == "QLineEdit")
    lineEdit = new QLineEdit(this)@

    and so on.
    Is there any other way to do the same. As i need to add the same properties for different widgets.


  • Moderators

    use only line 2 in your ifs. Move all the rest outside ifs and use QObject::setProperty() to change the properties.

    You will have to do a qobject_cast<> to "downgrade" the pointer, though. Example:
    @
    QObject *currentObject;
    if(myString =="QLabel")
    {
    label = new QLabel(this);
    currentObject = qobject_cast<QObject>(label);
    }
    // all other ifs here

    currentObject->setProperty("property name", obj->getText);
    // etc...
    @

    // brain to terminal, not tested :) I might have used wrong method names, and so on.



  • There is no need for the casting. Using properties works directly on any QObject derived class.
    @
    QObject *currentObject;
    if(myString =="QLabel")
    {
    currentObject = new QLabel(this);
    }
    // all other ifs here

    currentObject->setProperty("property name", obj->getText);
    // etc...
    

    @

    I still wonder if this is really the way to go. Are you not looking for a property editor type thing? I successfully used "YAPE":http://qt-apps.org/content/show.php?content=100064 in the past for this, but there are plenty of alternatives around.


  • Moderators

    (casting) Ah yes, that's what I suspected, but was too lazy to check or think through :)



  • @sierdzio
    Its works perfect :) Thanks for the help.

    @Andre

    Thanks for the help. The property editor thing is next on the list. Here i have a requirement where i need to load a form/frame from an xml file. The xml file contains the details of the type of layout and the positioning of each components.

    Thanks for your time :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.