Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] How to create an instance of a widget from a string?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 3.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sam
    wrote on last edited by
    #1

    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 :)

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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.

      (Z(:^

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Sam
          wrote on last edited by
          #4

          #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.

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

            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.

            (Z(:^

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #7

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

                (Z(:^

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Sam
                  wrote on last edited by
                  #8

                  @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 :)

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved