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. How to dynamically declare structure from text in runtime
Forum Updated to NodeBB v4.3 + New Features

How to dynamically declare structure from text in runtime

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 295 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.
  • B Offline
    B Offline
    brsmnc
    wrote on last edited by
    #1

    Hello, I want to build an simple application that it takes data from UDP and parses it with given struct. Application will not know about the structure at the beginning. It will read structure from the text file selected by the user and create it dynamically.

    I found that once a type declared in compile time, we can create an instance of it at run time using QMetaType class methods. But I couldn't find any way how can I declare a structure type and register it in run time. For below example MyStruct will be declared in file.

    Do you have any suggestions?

    //Example
    int id = qRegisterMetaType<MyStruct>();
    QMetaType type = QMetaType::fromName("MyStruct");
    if (type.isValid()) {
    void *ptr= type.create();
    ptr-> ....;
    ...
    }

    JonBJ Pl45m4P 2 Replies Last reply
    0
    • B brsmnc

      Hello, I want to build an simple application that it takes data from UDP and parses it with given struct. Application will not know about the structure at the beginning. It will read structure from the text file selected by the user and create it dynamically.

      I found that once a type declared in compile time, we can create an instance of it at run time using QMetaType class methods. But I couldn't find any way how can I declare a structure type and register it in run time. For below example MyStruct will be declared in file.

      Do you have any suggestions?

      //Example
      int id = qRegisterMetaType<MyStruct>();
      QMetaType type = QMetaType::fromName("MyStruct");
      if (type.isValid()) {
      void *ptr= type.create();
      ptr-> ....;
      ...
      }

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @brsmnc
      In C++ you cannot declare struct or class at runtime, only at compile time. It's a strongly-typed language.

      You could do something like this more from Python if you wanted to change to that.

      It is unclear why exactly you want to produce a "type" or struct dynamically. Even if you could create the type at runtime, how would that be useful given that you would not have any code compiled which used it?? You can manage without in C++, depends what you're really trying to achieve which makes you think you need some dynamically-created type.

      1 Reply Last reply
      0
      • B brsmnc

        Hello, I want to build an simple application that it takes data from UDP and parses it with given struct. Application will not know about the structure at the beginning. It will read structure from the text file selected by the user and create it dynamically.

        I found that once a type declared in compile time, we can create an instance of it at run time using QMetaType class methods. But I couldn't find any way how can I declare a structure type and register it in run time. For below example MyStruct will be declared in file.

        Do you have any suggestions?

        //Example
        int id = qRegisterMetaType<MyStruct>();
        QMetaType type = QMetaType::fromName("MyStruct");
        if (type.isValid()) {
        void *ptr= type.create();
        ptr-> ....;
        ...
        }

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by
        #3

        @brsmnc said in How to dynamically declare structure from text in runtime:

        I want to build an simple application that it takes data from UDP and parses it with given struct

        Why not create (declare) the struct(s) and read your data and save it in your struct(s)?
        You could use QByteArray, QVariant etc... or pre-declare one struct/class for every "type" you might have (if there are not too many).


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        1 Reply Last reply
        1

        • Login

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