Quantcast
Channel: Qt DevNet forums: Qt Quick 1283365070**
Viewing all articles
Browse latest Browse all 4972

Metaobject Reflect child classes from base classes in C++

$
0
0
We want a solution in C++ that must be able to do the following: Given a string of particular type, lets say ‘A’, we want to find all the types that derives from ‘A’. Instantiate new objects out of the types that are derived from ‘A’. E.g. Lets say we have a class, VehicleEntity. VehicleEntityhas child classes, PassangerCarEntity, TruckEntity, TrainEntity, BoatEntity. We are unsure what vehicle entities there may be as the a library could be added containing more VehicleEntities. E.g. an AirplaneEntity thaterives from VehicleEntity could be added after deployment. In the application, when a user wants to select a VehicleEntity, the user should be able to pick any of the entities deriving from VehicleEntity. This includes the PassangerCarEntity, TruckEntity, TrainEntity, BoatEntity and AirplaneEntity. The user selects an Entity, lets say AirplaneEntity, A new object of type AirplaneEntity must be instantiated. The following is an concept example in C# of what we want to achieve in C++. In C# the items for the dropdown list can be retrieved as follows: Type vehicleEntityType = typeof(VehicleEntity); List<Type> types = new List<Type>(); foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())     types.AddRange(assembly.GetTypes().Where(x => vehicleEntityType.IsAssignableFrom(x) && !x.IsGenericType && !x.IsAbstract)); dropDownBox.ItemList = types;   //To instantiate the object: List<VehicleEntity> vehicleList = new List<VehicleEntity>(); Type newVehicleType = (Type)dropDownBox.SelectedItem; object newVehicle = Activator.CreateInstance(newVehicleType ); // defrault constructor used, parameterless constructors for vehicles. vehicleList.Add(newVehicle); Is this possible in QT? From the documentation it seems that all this is possible with the possible exception of finding a list of all the classes deriving from a base class. Finding all of the types in the application would be usefull, but this does not seem possible. Is this correct?

Viewing all articles
Browse latest Browse all 4972

Trending Articles