Method and device for dynamically configuring window control and layout, equipment and medium

By using XML configuration files and reflection mechanisms to dynamically create objects and set properties and signals/slots in Qt applications, the cumbersome window control and layout adjustments in traditional Qt applications are solved, achieving efficient dynamic configuration and interface automation.

CN120929082AActive Publication Date: 2025-11-11CHANGSHA KELIANG TECH CO LTD

Patent Information

Application Number
CN202511460660.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-10-14
Publication Date
2025-11-11
Estimated Expiration
2045-10-14

AI Technical Summary

Technical Problem

In traditional Qt application development, adjusting window controls and layouts requires re-editing the code and recompiling the project, resulting in low development efficiency and difficulty in adapting to the needs of software iteration updates and personalized customization.

Method used

By obtaining the XML configuration file, Qt reflection is used to dynamically create objects and set properties and signals/slots, establishing a parent-child relationship tree between controls and layouts, thus enabling dynamic configuration and rendering of the interface tree.

Benefits of technology

It eliminates the need for hard-coding interface elements, improves the dynamism and scalability of interface configuration, reduces code redundancy and maintenance costs, and enables efficient dynamic configuration of window controls and layouts.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120929082A_ABST
    Figure CN120929082A_ABST
Patent Text Reader

Abstract

The invention relates to a method, a device, equipment and a medium for dynamically configuring a window control and layout, and the method comprises the following steps: firstly, obtaining and analyzing an XML configuration file to generate a data model tree of a structured window interface in a memory; then, objects are dynamically created based on a Qt reflection mechanism, attributes and signal slots are set, Qt reflection characteristics are fully utilized, object creation and attribute processing are flexible and efficient, hard coding interface elements in the compiling stage are not needed, needed controls and layouts can be dynamically generated according to a data model tree, and the dynamic nature and expandability of interface configuration are greatly enhanced; establishing a set membership tree between the controls and the layouts in the recursive traversal process to obtain a complete interface tree; and finally, performing layout calculation and rendering to generate a display interface, so that automation of interface layout is realized, interface element layout can be automatically adjusted according to a data model tree structure, and efficient dynamic configuration of window controls and layout is realized.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer technology, and in particular to a method, apparatus, computer device, storage medium, and computer program product for dynamically configuring window controls and layouts. Background Technology

[0002] In traditional Qt application development, window controls and layouts are primarily set up during the design phase using a visual interface designer. Developers determine key elements such as the position, size, type, and layout of controls during the design phase and then embed these elements in the code.

[0003] However, when software enters the iterative update, personalization, or needs to adapt to the diverse needs of different users, this fixed pattern exposes serious drawbacks. Once adjustments are needed to control-related properties or layout methods, developers must re-edit the code, meticulously tweaking the modified parts to ensure the new design is accurately implemented. After the code modifications are complete, the entire project must be recompiled to generate a new executable file. This process is tedious and complex, consuming a significant amount of time and effort, and greatly reducing development efficiency.

[0004] Therefore, there is an urgent need for an efficient dynamic configuration window control and layout scheme. Summary of the Invention

[0005] Therefore, it is necessary to provide an efficient method, apparatus, computer device, storage medium, and computer program product for dynamically configuring window controls and layouts to address the aforementioned technical problems.

[0006] Firstly, this application provides a method for dynamically configuring window controls and layouts. The method includes: Obtain and parse the XML configuration file to obtain the data model tree of the structured window interface in memory; The data model tree is recursively traversed, and objects are dynamically created and their properties and signals / slots are set based on the Qt reflection mechanism. During the recursive traversal, a parent-child relationship tree between controls and layouts is established to obtain a complete interface tree; The layout of the complete interface tree is calculated, and the calculated drawing interface is rendered to generate the display interface.

[0007] In one embodiment, the recursive traversal of the data model tree, dynamically creating objects and setting attributes and signal slots based on the Qt reflection mechanism, includes: The data model tree is recursively traversed to access each node in the data model tree layer by layer. Identify the control type of the node being traversed, where the control type includes control nodes and layout nodes; Based on the control type, objects are dynamically created and their properties and signals / slots are set using Qt reflection.

[0008] In one embodiment, the step of dynamically creating an object based on the control type and using Qt reflection mechanism, and setting its properties and signal slots, includes: For a control node, read the control type class name from the control node's attribute set; based on the read control type class name and using Qt reflection mechanism, dynamically create a control instance, set control properties, and connect signals and slots; For a layout node, read the layout type class name from the attribute set of the layout node; based on the layout type class name and the Qt reflection mechanism, dynamically create a layout manager instance and set the layout attributes.

[0009] In one embodiment, for a control node, reading the control type class name from the attribute set of the control node; dynamically creating a control instance based on the read control type class name and using Qt reflection mechanism, setting control attributes, and connecting signals and slots includes: For each control node, the control type class name is read from the control node's attribute set; Based on the read control type class name, and dynamically create control instances based on Qt reflection mechanism; Iterate through the property child nodes of the current control node, and for each property child node, set the corresponding property for the newly created control instance based on the Qt reflection mechanism; Iterate through the signal child nodes of the current control node, and for each signal child node, dynamically connect the preset target signal with the existing slot function based on the Qt reflection mechanism.

[0010] In one embodiment, for a layout node, reading the layout type class name from the attribute set of the layout node; dynamically creating a layout manager instance and setting layout attributes based on the layout type class name and using Qt reflection mechanism includes: For each layout node, the layout type class name is read from the attribute set of the layout node; Based on the layout type class name, the corresponding layout manager instance is dynamically created using Qt reflection mechanism; Set common properties for the created layout manager instance, including the spacing property, the margin property, and the stretch factor.

[0011] In one embodiment, establishing a parent-child relationship tree between controls and layouts during the recursive traversal to obtain a complete interface tree includes: During the recursive traversal, a parent-child relationship tree is simultaneously established between controls and layouts, between layouts, and between controls / layouts and windows, resulting in a complete interface tree.

[0012] In one embodiment, after performing layout calculations on the complete interface tree and rendering the calculated drawing interface to generate the display interface, the method further includes: When the update of the XML configuration file is detected by the file system monitor, the updated XML configuration file is parsed, and the data model tree of the structured window interface in memory is regenerated to obtain a new data model tree; The new data model tree and the existing data model tree are compared using a tree difference comparison algorithm to identify newly added nodes, deleted nodes, and attribute-changed nodes. The parent-child relationship tree between controls and layouts is reconstructed for the newly added nodes, and the newly added nodes are dynamically added to the complete interface tree; for the deleted nodes, the corresponding control object or layout object is found in the complete interface tree, and the found control object or layout object is deleted; for the attribute change nodes, the corresponding control object is found in the complete interface tree, and the attribute information corresponding to the found control object is updated. The layout of the updated complete interface tree is calculated, and the calculated drawing interface is rendered to generate a new display interface.

[0013] In one embodiment, the method for dynamically configuring window controls and layouts further includes: When an exception occurs during XML configuration file parsing, or during dynamic object creation, property setting, and signal / slot setting, the exception is captured and a predefined error handling strategy is executed. The error handling strategy includes logging, replacing invalid nodes with default controls, or ignoring invalid nodes. Before dynamically creating objects, the whitelist validation is performed on the control type class name and layout type class name specified in the XML configuration file. The whitelist validation is used to prevent the instantiation of unauthorized classes.

[0014] Secondly, this application also provides an apparatus for dynamically configuring window controls and layouts. The apparatus includes: The parsing module is used to obtain and parse the XML configuration file to obtain the data model tree of the structured window interface in memory; The layout module is used to recursively traverse the data model tree, dynamically create objects and set their properties and signals / slots based on Qt reflection mechanism; The parent-child relationship analysis module is used to establish a parent-child relationship tree between controls and layouts during recursive traversal, thus obtaining a complete interface tree; The rendering and display module is used to perform layout calculations on the complete interface tree and render the calculated drawing interface to generate the display interface.

[0015] In one embodiment, the above-described apparatus for dynamically configuring window controls and layouts further includes: The error handling module is used to capture exceptions and execute predefined error handling strategies when exceptions occur during parsing, creating objects, setting properties, or connecting signal slots. The error handling strategies include logging, replacing invalid nodes with default controls, or ignoring invalid nodes. The security verification module is used to perform whitelist verification on the control type class name and layout type class name specified in the XML configuration file before dynamically creating objects. The whitelist verification is used to prevent the instantiation of unauthorized classes.

[0016] Thirdly, this application also provides a computer device. The computer device includes a memory and a processor, the memory storing a computer program, and the processor executing the computer program to perform the following steps: Obtain and parse the XML configuration file to obtain the data model tree of the structured window interface in memory; The data model tree is recursively traversed, and objects are dynamically created and their properties and signals / slots are set based on the Qt reflection mechanism. During the recursive traversal, a parent-child relationship tree between controls and layouts is established to obtain a complete interface tree; The layout of the complete interface tree is calculated, and the calculated drawing interface is rendered to generate the display interface.

[0017] Fourthly, this application also provides a computer-readable storage medium. The computer-readable storage medium stores a computer program thereon, which, when executed by a processor, performs the following steps: Obtain and parse the XML configuration file to obtain the data model tree of the structured window interface in memory; The data model tree is recursively traversed, and objects are dynamically created and their properties and signals / slots are set based on the Qt reflection mechanism. During the recursive traversal, a parent-child relationship tree between controls and layouts is established to obtain a complete interface tree; The layout of the complete interface tree is calculated, and the calculated drawing interface is rendered to generate the display interface.

[0018] Fifthly, this application also provides a computer program product. The computer program product includes a computer program that, when executed by a processor, performs the following steps: Obtain and parse the XML configuration file to obtain the data model tree of the structured window interface in memory; The data model tree is recursively traversed, and objects are dynamically created and their properties and signals / slots are set based on the Qt reflection mechanism. During the recursive traversal, a parent-child relationship tree between controls and layouts is established to obtain a complete interface tree; The layout of the complete interface tree is calculated, and the calculated drawing interface is rendered to generate the display interface.

[0019] The aforementioned method, apparatus, computer device, storage medium, and computer program product for dynamically configuring window controls and layouts first acquires and parses an XML configuration file to generate a data model tree of a structured window interface in memory. Then, based on the Qt reflection mechanism, objects are dynamically created and their properties and signals / slots are set to fully utilize Qt reflection features, making object creation and property handling flexible and efficient. This eliminates the need for hard-coding interface elements during the compilation stage and allows for dynamic generation of required controls and layouts based on the data model tree, greatly enhancing the dynamism and scalability of the interface configuration and reducing code redundancy and maintenance costs. Furthermore, a parent-child relationship tree between controls and layouts is established during recursive traversal to obtain a complete interface tree. Finally, layout calculations and rendering are performed to generate the display interface, automating the interface layout. The layout of interface elements can be automatically adjusted according to the data model tree structure, significantly improving interface adaptability and achieving efficient dynamic configuration of window controls and layouts. Attached Figure Description

[0020] Figure 1 This is a diagram illustrating the application environment of a method for dynamically configuring window controls and layouts in one embodiment. Figure 2 This is a flowchart illustrating a method for dynamically configuring window controls and layouts in one embodiment. Figure 3 This is a flowchart illustrating a method for dynamically configuring window controls and layouts in another embodiment; Figure 4 This is a structural block diagram of a device for dynamically configuring window controls and layouts in one embodiment; Figure 5 This is an internal structural diagram of a computer device in one embodiment. Detailed Implementation

[0021] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.

[0022] To explain in detail the technical principles and effects of the method for dynamically configuring window controls and layouts in this application, we will first provide a more detailed explanation of traditional related technologies.

[0023] In traditional Qt application development, window controls and layouts are typically fixed during the design phase using a visual interface designer. Once finalized, modifications are cumbersome. Adjusting the position, size, type, or layout of controls often requires re-editing the code and recompiling the project. This is extremely inconvenient during software iterations, customizations, and adaptations to different user needs, severely reducing development efficiency and limiting software maintainability and scalability.

[0024] To address the aforementioned shortcomings of traditional technologies, this application proposes to read the XML configuration file when a Qt application starts, and based on the definitions in the file, use Qt's reflection mechanism and dynamic object creation capabilities to generate corresponding control objects in memory. These objects are then arranged and combined according to the layout types set in the XML file to construct a complete window interface, without the need for hard-coding the control classes, thus enhancing the system's flexibility and maintainability.

[0025] Specifically, the method for dynamically configuring window controls and layouts provided in this application embodiment can be applied to, for example... Figure 1 In the application environment shown, terminal 102 communicates with server 104 via a network. A data storage system can store the data that server 104 needs to process. The data storage system can be integrated onto server 104 or placed on a cloud or other network server. Terminal 102 responds to user operations by sending a dynamic configuration window control and layout request to server 104. The server responds to the request, obtains and parses the XML configuration file, and obtains the data model tree of the structured window interface in memory. It recursively traverses the data model tree, dynamically creates objects based on Qt reflection, and sets attributes and signal slots. During the recursive traversal, it establishes a parent-child relationship tree between controls and layouts to obtain a complete interface tree. It performs layout calculations on the complete interface tree and renders the calculated drawing interface to generate the display interface. Terminal 102 can be, but is not limited to, various personal computers, laptops, smartphones, tablets, IoT devices, and portable wearable devices. IoT devices can be smart speakers, smart TVs, smart air conditioners, smart in-vehicle devices, etc. Portable wearable devices can be smartwatches, smart bracelets, head-mounted devices, etc. Server 104 can be implemented using a standalone server or a server cluster composed of multiple servers. Furthermore, the method for dynamically configuring window controls and layouts in this application can also be directly applied to the terminal, where the terminal responds to user operations to directly execute the above processing and generate the display interface. The specific process is similar to that described above and will not be repeated here.

[0026] In one embodiment, such as Figure 2 As shown, a method for dynamically configuring window controls and layouts is provided, which can be applied to... Figure 1Taking server 104 as an example, the following steps are included: S200: Obtain and parse the XML configuration file to obtain the data model tree of the structured window interface in memory.

[0027] XML configuration files are pre-configured files that can be stored in the execution location. When the system (server) starts or receives a reload signal, the configuration file parsing module reads the specified XML configuration file. Qt's XML modules (such as QXmlStreamReader) or the DOM parser are used to parse the file content node by node. An in-memory structured window interface data model tree is a collection of data organized in memory in a tree structure, used to accurately map the hierarchical structure of the window interface. The root node of the tree represents the entire window; it is at the top level of the entire data model tree and is the starting point of the entire interface. Multiple child nodes extend downwards from the root node; these child nodes can be layout nodes or control nodes.

[0028] In simple terms, a structured data model that fully represents the entire window interface is generated in memory at this point. This model is typically a tree structure, with the root node representing the window and child nodes representing controls and nested layouts. This process occurs entirely at runtime, parsing a plain, modifiable text file rather than processing a fixed .ui file at compile time. This improves subsequent flexibility and, consequently, the efficiency of dynamically configuring window controls and layouts.

[0029] S400: Recursively traverses the data model tree, dynamically creating objects and setting properties and signals / slots based on Qt reflection.

[0030] A recursive algorithm is used, starting from the root node of the data model tree and sequentially visiting each child node. During the recursion, different processing is performed based on the node type (control node or layout node). For example, when a control node is encountered, control-related creation and property setting operations are performed; when a layout node is encountered, layout manager-related creation and property setting operations are performed. For each control node or layout node, its type class name is read from the node's property information. Before dynamically creating the object, the class name string read from the node is first security-validated by comparing it with a predefined whitelist of trusted class names to prevent instantiation of potentially dangerous or unsupported classes. If the class name is not in the whitelist, an error log is logged and the node is skipped, or a default placeholder control (such as QLabel) is created to maintain the integrity of the interface structure. Utilizing Qt's Meta-ObjectSystem, the corresponding QMetaObject object is found using the QMetaType::fromName() method. If found, the QMetaObject::newInstance() method is called to dynamically create the corresponding control instance or layout manager instance. This process should be placed within a try-catch block to prevent program crashes due to creation failure. If creation fails, detailed error information should be logged and a default object should be attempted. After object creation, the node's property collection is iterated again. For each property, the object's meta-object is obtained using `QMetaObject`, and then the property object is obtained using `QMetaProperty` methods (such as `QMetaProperty::fromName()`, and then `QMetaProperty::write()`) to write the values ​​from the property collection to the corresponding property of the object. When setting properties, it is necessary to check whether the property exists and whether the value types are compatible to avoid runtime errors caused by setting invalid properties. Additionally, if signal and slot connections are defined in the node, when using the `QObject::connect()` method to connect the object's signals to the corresponding slot functions, it supports connecting to existing slot functions and also supports defining anonymous slot functions using lambda expressions to increase processing flexibility.

[0031] Here, UI elements are dynamically generated based on the data model tree, and their expected attributes and interactive functions are assigned. Recursive traversal allows processing of all nodes in the data model tree, ensuring that each control and layout manager is correctly created and configured. The dynamic creation method based on Qt reflection improves code flexibility and scalability, allowing UI elements to be changed by modifying XML configuration files without altering the code itself.

[0032] S600: During the recursive traversal, a parent-child relationship tree between controls and layouts is established to obtain a complete interface tree.

[0033] During the recursive traversal of the data model tree, based on the hierarchical relationships between nodes, the objects (controls or layout managers) corresponding to child nodes are set as children of the objects corresponding to their parent nodes. For layout nodes, the `add child` method of the layout manager is used to add the child node objects to the parent layout manager. For control nodes, if it is a top-level control (i.e., a control directly belonging to the window), it is set as a child of the window. By recursively traversing the entire data model tree and continuously establishing parent-child relationships between controls and layouts, and between layouts themselves, a complete interface tree is ultimately formed.

[0034] Establishing a parent-child relationship tree is a crucial step in constructing a complete interface structure. A complete interface tree provides clear hierarchical information for subsequent layout calculations, enabling the layout manager to accurately calculate the position and size of each interface element based on parent-child relationships, thereby achieving a reasonable interface layout.

[0035] S800: Performs layout calculations on the complete interface tree, renders the calculated drawing interface, and generates the display interface.

[0036] Once the entire UI tree is built, the layout management module begins its work. First, the top-level layout is set on the main window or container control using the `QWidget::setLayout` method. This step associates the UI tree with the actual display container, preparing for subsequent layout calculations and UI rendering. The `activate()` function of the top-level layout is called, or the layout calculation process is automatically triggered by Qt. Qt's layout system automatically and recursively calculates the size and position of all child controls and child layouts. During the calculation, the layout system fully considers the size policy (`sizePolicy`), minimum / maximum size (`minimumSize` / `maximumSize`), and stretch factors set in the layout, determining the specific position and size of each control and layout within the window. For example, for multiple buttons in a horizontal layout, the layout system calculates the display position of each button based on the button size and layout spacing, ensuring they are arranged horizontally within the layout. Qt's rendering engine then draws the final UI onto the screen based on the layout calculation results. To improve performance, for complex interfaces or frequently updated scenarios, the following optimization strategies can be combined: 1) Caching mechanism: The parsed data model tree or the created object tree is cached and reused directly when the configuration file has not changed, reducing the overhead of parsing and creation; 2) Asynchronous loading and calculation: The XML parsing, object creation and layout calculation process is executed in a background thread to avoid blocking the main thread and causing the interface to lag. Rendering is then performed on the UI thread after preparation is complete. 3) Incremental update: When a change in the configuration file is detected, only the changed parts are differentially updated to avoid rebuilding the entire interface tree.

[0037] The rendering engine selects the appropriate drawing method based on the type and properties of the control. For example, for a button control, the rendering engine will draw the button's background, border, text, and other elements to present an appearance on the screen that meets design requirements. The rendering process is the crucial step in ultimately converting interface data into an actual visual image. Through efficient rendering algorithms, the display quality and performance of the interface can be guaranteed.

[0038] Here, by performing layout calculations and rendering on the complete interface tree, the interface data in memory is converted into an actual displayable interface. Layout calculations ensure the reasonable arrangement of various controls and layouts in the window, resulting in a good visual effect and user experience; interface rendering accurately draws the calculated interface information onto the screen, allowing the user to see the final window interface.

[0039] The above-described method for dynamically configuring window controls and layouts first obtains and parses an XML configuration file to generate a data model tree for a structured window interface in memory. Then, it dynamically creates objects and sets properties and signals / slots based on Qt reflection, fully utilizing Qt reflection features to make object creation and property handling flexible and efficient. This eliminates the need for hard-coding interface elements during the compilation phase and allows for dynamic generation of required controls and layouts based on the data model tree, greatly enhancing the dynamism and scalability of the interface configuration and reducing code redundancy and maintenance costs. Furthermore, during the recursive traversal, a parent-child relationship tree between controls and layouts is established to obtain a complete interface tree. Finally, layout calculations and rendering are performed to generate the display interface, automating the interface layout. The layout of interface elements can be automatically adjusted according to the data model tree structure, significantly improving interface adaptability and achieving efficient dynamic configuration of window controls and layouts.

[0040] In one embodiment, such as Figure 3 As shown, S400 includes: S420: Perform a recursive traversal of the data model tree to access each node in the data model tree level by level.

[0041] Recursion is a programming technique where a function calls itself from within its own function. When traversing a data model tree, starting from the root node, the function first visits the root node, and then recursively calls the traversal function for each of the root node's child nodes. For example, if the root node has two child nodes A and B, after visiting the root node, the traversal function for child node A will be called first. Within child node A's traversal function, its child nodes (if any) will be visited, and so on, until the child node has no more child nodes. Then, the process backtracks to child node A, and after traversing child node A, the same recursive traversal operation is performed on child node B. During recursion, the depth and order of recursion are controlled to achieve layer-by-layer access. Each recursive call passes the current node as a parameter to the next call, while simultaneously recording the current level. This way, when visiting each node, its level is clearly defined, allowing for the sequential access of nodes in the data model tree from the root node to the bottom-most node.

[0042] S440: Identifies the control type of the node being traversed. Control types include control nodes and layout nodes.

[0043] In a data model tree, each node contains type identification information. This identification information can be represented through node attributes or specific tags. Control nodes and layout nodes have different characteristics. Control nodes typically contain attributes related to the specific control, such as the text of a button or the default value of a text box; while layout nodes mainly contain layout-related attributes, such as the layout direction (horizontal, vertical) and spacing. Analyzing the attribute set of a node can also help determine the type of the node.

[0044] S460: Dynamically creates objects and sets properties and signals / slots based on the control type and using Qt reflection.

[0045] Qt provides a Meta-ObjectSystem, which provides runtime type information and dynamic functionality for objects. The QMetaObject class allows you to obtain class meta-information, including the class name, properties, methods, signals, and slots. Dynamically creating objects based on Qt reflection primarily utilizes the QMetaObject::newInstance() method. This method creates a new object instance based on the class's meta-object information. It retrieves the corresponding class name based on the identified control type and then uses QMetaObject::newInstance() to create the object instance. During creation, if the class constructor requires parameters, these can be passed through other methods of QMetaObject to meet the creation needs of different classes. If dynamic object creation fails during this process, an exception is thrown or a null pointer is returned. The error handling module that called the process catches the exception and handles it according to a preset strategy (such as logging errors, creating a default control, or skipping the node) to ensure the program continues running. After object creation, the object's properties need to be set based on the property information stored in the nodes of the data model tree. Qt provides the QMetaProperty class to manipulate the object's properties. By retrieving the object's metaobject, iterating through its property list, and finding the property corresponding to the node property in the data model tree, the `QMetaProperty::write()` method is used to write the node property value into the object's property.

[0046] Signals and slots are a crucial mechanism for inter-object communication in Qt. When constructing the control and layout object tree, it's necessary to connect the object's signals to their corresponding slot functions based on the signal-slot relationships defined in the data model tree. Qt provides the `QObject::connect()` method to implement this connection. It associates the signal by specifying the object sending the signal, the signal's signature, the object receiving the signal, and the slot function's signature. After dynamically creating objects and handling their properties and signals / slots, these objects are organized according to the hierarchical relationships in the data model tree to construct the control and layout object tree. Based on the parent-child relationships in the data model tree, child objects are set as children of their parent objects.

[0047] In one embodiment, dynamically creating objects and setting properties and signals / slots based on the control type and using Qt reflection includes: Step 1: For a control node, read the control type class name from the control node's property set; based on the read control type class name, dynamically create a control instance using Qt reflection, set control properties, and connect signals and slots.

[0048] In the data model tree, each control node contains a set of attributes, stored in a specific data structure, such as key-value pairs. By traversing the attribute set of the control node, the attribute key associated with the control type class name is found, and the corresponding attribute value is obtained; this attribute value is the control type class name. Qt provides a meta-object system, where the QMetaObject class contains class metadata. First, the QMetaType::fromName() method is used to find the corresponding QMetaObject object based on the read control type class name. If the corresponding QMetaObject object is found, the QMetaObject::newInstance() method is called, which dynamically creates a new control instance at runtime based on the class metadata. After creating the control instance, its attributes need to be set to match the values ​​defined in the control node's attribute set. The attribute set of the control node is traversed again; for each attribute, the QMetaObject is used to obtain the meta-object of the control instance, and then the relevant methods of QMetaObject are used. When iterating through the property child nodes of the currently described control node, the `QObject::setProperty(const char* name, const QVariant&value)` method is called to set each property. Internally, this method utilizes Qt's property system to automatically perform type conversion and safety checks. The value from the property set is then written to the corresponding property of the control instance. Signals and slots are an important mechanism for inter-object communication in Qt. The property set of a control node may also define signal-slot connections. By parsing this connection information, the `QObject::connect()` method is used to connect the control instance's signals to the corresponding slot functions.

[0049] Step 2: For the layout node, read the layout type class name from the attribute set of the layout node; based on the layout type class name, dynamically create a layout manager instance and set the layout attributes using Qt reflection mechanism.

[0050] Similar to reading the control type class name, layout nodes also have their own property set. Within the layout node's property set, search for the property key related to the layout type class name and retrieve the corresponding property value; this value is the layout type class name. Utilizing Qt's meta-object system, first use the `QMetaType::fromName()` method to find the corresponding `QMetaObject` object based on the layout type class name. If found, then call the `QMetaObject::newInstance()` method to dynamically create a layout manager instance. The layout node's property set also contains layout-related properties, such as layout direction and spacing. By iterating through the layout node's property set, using `QMetaObject` to obtain the meta-object of the layout manager instance, and then using `QMetaProperty` methods to set the values ​​from the property set to the corresponding properties of the layout manager instance.

[0051] In one embodiment, for a control node, the control type class name is read from the control node's property set; based on the read control type class name, and using Qt reflection to dynamically create a control instance, set control properties, and connect signals and slots, the process includes: Step 1: For a control node, read the control type class name from the control node's property set.

[0052] Within the data model tree obtained by parsing the XML configuration file, the currently processed control node is located. Each control node contains a set of attributes that stores various information about the control. Using a specific parsing method (such as utilizing the interface provided by the XML parser or custom parsing logic), the attribute named "type" is searched from the control node's attribute set. The value of this attribute is the control type class name, represented as a string, such as "QPushButton" or "QLineEdit".

[0053] Step 2: Based on the read control type class name, dynamically create control instances using Qt reflection mechanism.

[0054] This section describes how to dynamically create control instances using Qt's Meta-Object System. First, the string-type control type class name is converted to a format suitable for Qt's Meta-Object System. Then, by calling `QMetaType` or using methods like `QMetaObject::newInstance()`, an instance of the control is dynamically created based on the string-type class name. Specifically, it can be done as follows: QObject*widget=QMetaType::create<QObject*> (QMetaType::type(typeName.toUtf8())).

[0055] Step 3: Traverse the property child nodes of the current control node, and for each property child node, set the corresponding property for the newly created control instance based on Qt reflection mechanism.

[0056] Under a control node, there may be multiple property child nodes, each representing a property of the control. By traversing these property child nodes, the name and value of each property can be obtained. The property name and value are also stored as strings in the XML configuration file. Specifically, by traversing the property child nodes of the control node, the QObject::setProperty() method is used to dynamically set the properties (such as geometry, text, font, etc.) of the newly created control instance using the property name (string).

[0057] Step 4: Traverse the signal child nodes of the current control node. For each signal child node, dynamically connect the preset target signal with the existing slot function based on the Qt reflection mechanism.

[0058] The `signal` child nodes of the control node store the signal information that needs to be connected. These `signal` child nodes are traversed to obtain the name (string) of each signal. Simultaneously, corresponding slot functions are defined in the application to handle events triggered by the signal. Specifically, the `QObject::connect()` function is used to dynamically connect the signals (strings) specified in the configuration file with the existing slot functions (string matching) in the application.

[0059] In one embodiment, for a layout node, the layout type class name is read from the attribute set of the layout node; based on the layout type class name, and using Qt reflection mechanism, a layout manager instance is dynamically created and layout attributes are set, including: Step 1: For the layout node, read the layout type class name from the attribute set of the layout node.

[0060] Within the data structure constructed by parsing the XML configuration file, the currently processed layout node is located. The layout node contains a set of attributes that stores various layout-related information. Using specific parsing logic, such as leveraging interfaces provided by the XML parser or custom parsing functions, the attribute named "type" is retrieved from the layout node's attribute set.

[0061] Step 2: Based on the layout type class name, dynamically create the corresponding layout manager instance using Qt reflection mechanism.

[0062] This code demonstrates how to dynamically create layout manager instances using Qt's Meta-Object System. First, the read string-type layout type class name is converted to a format suitable for Qt's Meta-Object System. Then, based on the obtained type identifier, QMetaType::create is called.<QLayout*> The () method dynamically creates an instance of the layout manager.

[0063] Step 3: Set common properties for the created layout manager instance. Common properties include the spacing property, margin property, and stretch factor.

[0064] The `spacing` property controls the spacing between child controls in a layout manager. The `margin` property sets the margin between the layout manager and its parent control or surrounding environment. Setting common properties (spacing and margin) for a layout manager instance allows for further customization of the layout's appearance and effects. The `spacing` property adjusts the tightness between child controls, making the interface more aesthetically pleasing and readable; the `margin` property controls the distance between the layout manager and surrounding elements, preventing the layout from being too compact or crowded. By dynamically setting these properties, layout details can be flexibly adjusted according to different interface design needs, improving interface quality and user experience. During layout calculation, the layout manager comprehensively considers the `sizePolicy` (such as `Expanding`, `Fixed`, `Minimum`, etc.), stretch factor, and minimum / maximum size properties of all its internal controls to calculate a reasonable layout geometry.

[0065] In one embodiment, after performing layout calculations on the complete interface tree and rendering the calculated drawing interface to generate the display interface, the method further includes: Step 1: When the XML configuration file is updated by the file system monitor, the updated XML configuration file is parsed, and the data model tree of the structured window interface in memory is regenerated to obtain a new data model tree.

[0066] When an update to the XML configuration file is detected by the file system monitor, the server initiates the parsing process. The updated XML configuration file content is read using an XML parser. Information about each node (including control nodes and layout nodes), such as node type, attribute name, and attribute value, is extracted according to the hierarchical structure and tag definitions of the XML file. Based on this information, a structured window interface data model tree is reconstructed in memory. This new data model tree, denoted as `newTree`, reflects the updated interface structure of the XML configuration file.

[0067] Step 2: Use the tree difference comparison algorithm to compare the new data model tree and the data model tree to identify newly added nodes, deleted nodes, and attribute change nodes.

[0068] The newTree is compared with the old data model tree (oldTree) corresponding to the current interface. A tree difference comparison algorithm (based on depth-first search) is used to identify newly added nodes (controls / layouts), deleted nodes, and nodes whose attributes have changed.

[0069] Step 3: Reconstruct the parent-child relationship tree between controls and layouts for newly added nodes, and dynamically add the new nodes to the complete interface tree; for deleted nodes, find the corresponding control object or layout object in the complete interface tree and delete the found control object or layout object; for nodes with changed attributes, find the corresponding control object in the complete interface tree and update the attribute information of the found control object.

[0070] Based on the position and hierarchy of the newly added node within the `newTree`, analyze its parent-child relationships with surrounding controls and layouts. For example, if a newly added button control is located within a horizontal layout manager, then the parent object of that button control is the horizontal layout manager. Utilizing Qt's object model and layout management mechanism, create the corresponding control or layout object for the newly added node. Then, based on the constructed parent-child relationship tree, dynamically add the new object to its corresponding position in the complete UI tree. For example, use the `setLayout()` method of `QWidget` to set the layout manager for the parent control, and use the `addWidget()` method of the layout manager to add the control to the layout.

[0071] Within the complete UI tree, the control or layout object corresponding to the node to be deleted is located through traversal and search algorithms (such as recursive traversal). Then, the corresponding deletion method is called, such as removing the control object from the parent layout (using the layout manager's removeWidget() method), and the object's memory space is released to ensure that resources are properly reclaimed.

[0072] Locate the control object corresponding to the property change node in the complete interface tree. Set the new property value to the control object by calling its `setProperty()` method. Then, call the control object's `update()` method to request the server to redraw the control to reflect the changed appearance.

[0073] Step 4: Perform layout calculations on the updated complete interface tree, and render the calculated drawing interface to generate a new display interface.

[0074] The system invokes Qt's layout system to perform layout calculations on the updated complete UI tree. After the layout calculation is complete, the system renders the UI based on the calculated drawing information. Qt's rendering engine draws the appearance of the controls onto the screen, generating a new display interface.

[0075] In one embodiment, the method for dynamically configuring window controls and layouts further includes: Step 1: When an exception occurs during XML configuration file parsing, or during dynamic object creation, or during the setting of properties and slots, the exception is captured and a predefined error handling strategy is executed. The error handling strategy includes logging, replacing invalid nodes with default controls, or ignoring invalid nodes.

[0076] An exception handling mechanism is implemented in key operations such as XML configuration file parsing, dynamic object creation, and setting properties and signals / slots. When exceptions occur during these operations, the exception information is captured promptly. Predefined error handling strategies include logging, replacing invalid nodes with default controls, or ignoring invalid nodes. The logging function records the specific time, location, exception type, and relevant context information of the exception for subsequent troubleshooting. The default control replacement strategy uses pre-defined default controls to replace the specified controls when an exception prevents their creation, ensuring the interface still displays correctly. The invalid node ignoring strategy skips invalid nodes in the XML configuration file that do not affect the overall layout and functionality, preventing the entire configuration process from failing due to a single invalid node. Here, the exception handling mechanism significantly improves the stability of parsing and processing.

[0077] Step 2: Before dynamically creating objects, perform whitelist verification on the control type class name and layout type class name specified in the XML configuration file. The whitelist verification is used to prevent the instantiation of unauthorized classes.

[0078] Before dynamically creating objects, the control type class names and layout type class names specified in the XML configuration file are whitelisted. The whitelist is a predefined list of legal class names that includes all authorized control and layout classes allowed to be instantiated in the system. Whitelisting is a crucial means of ensuring system security. In scenarios where window controls and layouts are dynamically configured, allowing the instantiation of controls and layouts with arbitrary class names could pose serious security risks. Malicious users could potentially tamper with the XML configuration file to introduce malicious classes, thereby launching attacks on the system, leaking data, and other malicious activities. Whitelisting effectively prevents the instantiation of unauthorized classes, ensuring that only strictly reviewed and trusted classes can run in the system, significantly improving the security and reliability of the program (software).

[0079] It should be understood that although the steps in the flowcharts of the above embodiments are shown sequentially according to the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some steps in the flowcharts of the above embodiments may include multiple steps or multiple stages. These steps or stages are not necessarily completed at the same time, but can be executed at different times. The execution order of these steps or stages is not necessarily sequential, but can be performed alternately or in turn with other steps or at least some of the steps or stages of other steps.

[0080] Based on the same inventive concept, this application also provides a device for dynamically configuring window controls and layouts to implement the above-described method for dynamically configuring window controls and layouts. The solution provided by this device is similar to the solution described in the above-described method. Therefore, the specific limitations of one or more device embodiments for dynamically configuring window controls and layouts provided below can be found in the limitations of the method for dynamically configuring window controls and layouts described above, and will not be repeated here.

[0081] In one embodiment, such as Figure 4 As shown, a device for dynamically configuring window controls and layouts is provided, including: Parsing module 200 is used to obtain and parse the XML configuration file to obtain the data model tree of the structured window interface in memory; Layout module 400 is used to recursively traverse the data model tree, dynamically creating objects and setting properties and signals / slots based on Qt reflection mechanism; The parent-child relationship analysis module 600 is used to establish a parent-child relationship tree between controls and layouts during recursive traversal, thereby obtaining a complete interface tree; The rendering and display module 800 is used to perform layout calculations on the complete interface tree and render the calculated drawing interface to generate the display interface.

[0082] In one embodiment, the layout module 400 is further configured to perform a recursive traversal operation on the data model tree to access each node in the data model tree layer by layer; identify the control type of the traversed node, including control nodes and layout nodes; and dynamically create objects and set properties and signals / slots based on the control type and Qt reflection mechanism.

[0083] In one embodiment, the layout module 400 is further configured to, for a control node, read the control type class name from the attribute set of the control node; dynamically create a control instance based on the read control type class name and based on the Qt reflection mechanism, set control attributes, and connect signals and slots; for a layout node, read the layout type class name from the attribute set of the layout node; dynamically create a layout manager instance based on the layout type class name and based on the Qt reflection mechanism, and set layout attributes.

[0084] In one embodiment, the layout module 400 is further configured to: read the control type class name from the property set of the control node; dynamically create a control instance based on the read control type class name and based on the Qt reflection mechanism; traverse the property child nodes of the current control node, and for each property child node, set the corresponding property for the newly created control instance based on the Qt reflection mechanism; traverse the signal child nodes of the current control node, and for each signal child node, dynamically connect the preset target signal with the existing slot function based on the Qt reflection mechanism.

[0085] In one embodiment, the layout module 400 is further configured to read the layout type class name from the attribute set of the layout node; dynamically create a corresponding layout manager instance based on the layout type class name using Qt reflection mechanism; and set common properties for the created layout manager instance, including the spacing property, the margin property, and the stretch factor.

[0086] In one embodiment, the parent-child relationship analysis module 600 is also used to synchronously establish a parent-child relationship tree between controls and layouts, between layouts, and between controls / layouts and windows during the recursive traversal process, so as to obtain a complete interface tree.

[0087] In one embodiment, the above-described apparatus for dynamically configuring window controls and layouts further includes: The update module is used to parse the updated XML configuration file when an update is detected by the file system monitor, regenerate the data model tree of the structured window interface in memory, and obtain a new data model tree. It then uses a tree difference comparison algorithm to compare the new data model tree with the original data model tree, identifying newly added nodes, deleted nodes, and nodes with changed attributes. For newly added nodes, it reconstructs the parent-child relationship tree between controls and layouts, dynamically adding the new node to the complete interface tree. For deleted nodes, it searches for the corresponding control or layout object in the complete interface tree and deletes the found control or layout object. For nodes with changed attributes, it searches for the corresponding control object in the complete interface tree and updates the attribute information of the found control object. Finally, it performs layout calculations on the updated complete interface tree and renders the calculated drawing interface to generate a new display interface.

[0088] In one embodiment, the above-described apparatus for dynamically configuring window controls and layouts further includes: The error handling module is used to capture exceptions and execute predefined error handling strategies when exceptions occur during parsing, creating objects, setting properties, or connecting signal slots. The error handling strategies include logging, replacing invalid nodes with default controls, or ignoring invalid nodes. The security verification module is used to perform whitelist verification on the control type class name and layout type class name specified in the XML configuration file before dynamically creating objects. The whitelist verification is used to prevent the instantiation of unauthorized classes.

[0089] The modules in the aforementioned device for dynamically configuring window controls and layouts can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in or independent of the processor in a computer device, or stored in the memory of a computer device in software form, so that the processor can call and execute the operations corresponding to each module.

[0090] In one embodiment, a computer device is provided, which may be a server, and its internal structure diagram may be as follows: Figure 5 As shown, the computer device includes a processor, memory, and a network interface connected via a system bus. The processor provides computing and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system, computer programs, and a database. The internal memory provides an environment for the operation of the operating system and computer programs stored in the non-volatile storage media. The database stores preset data. The network interface communicates with external terminals via a network connection. When executed by the processor, the computer program implements a method for dynamically configuring window controls and layouts.

[0091] Those skilled in the art will understand that Figure 5 The structure shown is merely a block diagram of a portion of the structure related to the present application and does not constitute a limitation on the computer device to which the present application is applied. Specific computer devices may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.

[0092] In one embodiment, a computer device is provided, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the above-described method for dynamically configuring window controls and layouts.

[0093] In one embodiment, a computer-readable storage medium is provided having a computer program stored thereon, which, when executed by a processor, implements the above-described method for dynamically configuring window controls and layouts.

[0094] In one embodiment, a computer program product is provided, including a computer program that, when executed by a processor, implements the above-described method for dynamically configuring window controls and layouts.

[0095] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium. When executed, the computer program can include the processes of the embodiments of the above methods. Any references to memory, databases, or other media used in the embodiments provided in this application can include at least one of non-volatile and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetic random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM can take many forms, such as Static Random Access Memory (SRAM) or Dynamic Random Access Memory (DRAM). The databases involved in the embodiments provided in this application may include at least one type of relational database and non-relational database. Non-relational databases may include, but are not limited to, blockchain-based distributed databases. The processors involved in the embodiments provided in this application may be general-purpose processors, central processing units, graphics processing units, digital signal processors, programmable logic devices, quantum computing-based data processing logic devices, etc., and are not limited to these.

[0096] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.

[0097] The above embodiments are merely illustrative of several implementation methods of this application, and their descriptions are relatively specific and detailed. However, they should not be construed as limiting the scope of this application. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these all fall within the protection scope of this application. Therefore, the protection scope of this application should be determined by the appended claims.

Claims

1. A method for dynamically configuring window controls and layout, characterized in that, The method includes: Obtain and parse the XML configuration file to obtain the data model tree of the structured window interface in memory; The data model tree is recursively traversed, and objects are dynamically created and their properties and signals / slots are set based on the Qt reflection mechanism. During the recursive traversal, a parent-child relationship tree between controls and layouts is established to obtain a complete interface tree; The layout of the complete interface tree is calculated, and the calculated drawing interface is rendered to generate the display interface.

2. The method according to claim 1, characterized in that, The recursive traversal of the data model tree, dynamically creating objects and setting attributes and signal slots based on Qt reflection mechanism, includes: The data model tree is recursively traversed to access each node in the data model tree layer by layer. Identify the control type of the node being traversed, where the control type includes control nodes and layout nodes; Based on the control type, objects are dynamically created and their properties and signals / slots are set using Qt reflection.

3. The method according to claim 2, characterized in that, The step of dynamically creating an object and setting its properties and signal slots based on the control type and Qt reflection mechanism includes: For a control node, read the control type class name from the control node's attribute set; based on the read control type class name and using Qt reflection mechanism, dynamically create a control instance, set control properties, and connect signals and slots; For a layout node, read the layout type class name from the attribute set of the layout node; based on the layout type class name and the Qt reflection mechanism, dynamically create a layout manager instance and set the layout attributes.

4. The method according to claim 3, characterized in that, For a control node, the process of reading the control type class name from the control node's attribute set, and dynamically creating a control instance based on the read control type class name and using Qt reflection, setting control properties, and connecting signals and slots includes: For each control node, the control type class name is read from the control node's attribute set; Based on the read control type class name, and dynamically create control instances based on Qt reflection mechanism; Iterate through the property child nodes of the current control node, and for each property child node, set the corresponding property for the newly created control instance based on the Qt reflection mechanism; Iterate through the signal child nodes of the current control node, and for each signal child node, dynamically connect the preset target signal with the existing slot function based on the Qt reflection mechanism.

5. The method according to claim 3, characterized in that, For a layout node, the process of reading the layout type class name from the layout node's attribute set and dynamically creating a layout manager instance and setting layout attributes based on the layout type class name and Qt reflection mechanism includes: For each layout node, the layout type class name is read from the attribute set of the layout node; Based on the layout type class name, the corresponding layout manager instance is dynamically created using Qt reflection mechanism; Set common properties for the created layout manager instance, including the spacing property, the margin property, and the stretch factor.

6. The method according to claim 1, characterized in that, The process of establishing a parent-child relationship tree between controls and layouts during recursive traversal to obtain a complete interface tree includes: During the recursive traversal, a parent-child relationship tree is simultaneously established between controls and layouts, between layouts, and between controls / layouts and windows, resulting in a complete interface tree.

7. The method according to claim 1, characterized in that, After performing layout calculations on the complete interface tree and rendering the calculated drawing interface to generate the display interface, the process further includes: When the update of the XML configuration file is detected by the file system monitor, the updated XML configuration file is parsed, and the data model tree of the structured window interface in memory is regenerated to obtain a new data model tree; The new data model tree and the existing data model tree are compared using a tree difference comparison algorithm to identify newly added nodes, deleted nodes, and attribute-changed nodes. The parent-child relationship tree between controls and layouts is reconstructed for the newly added nodes, and the newly added nodes are dynamically added to the complete interface tree; for the deleted nodes, the corresponding control object or layout object is found in the complete interface tree, and the found control object or layout object is deleted; for the attribute change nodes, the corresponding control object is found in the complete interface tree, and the attribute information corresponding to the found control object is updated. The layout of the updated complete interface tree is calculated, and the calculated drawing interface is rendered to generate a new display interface.

8. The method according to claim 1, characterized in that, Also includes: When an exception occurs during XML configuration file parsing, or during dynamic object creation, property setting, and signal / slot setting, the exception is captured and a predefined error handling strategy is executed. The error handling strategy includes logging, replacing invalid nodes with default controls, or ignoring invalid nodes. Before dynamically creating objects, the whitelist validation is performed on the control type class name and layout type class name specified in the XML configuration file. The whitelist validation is used to prevent the instantiation of unauthorized classes.

9. A device for dynamically configuring window controls and layouts, characterized in that, The device includes: The parsing module is used to obtain and parse the XML configuration file to obtain the data model tree of the structured window interface in memory; The layout module is used to recursively traverse the data model tree, dynamically create objects and set their properties and signals / slots based on Qt reflection mechanism; The parent-child relationship analysis module is used to establish a parent-child relationship tree between controls and layouts during recursive traversal, thus obtaining a complete interface tree; The rendering and display module is used to perform layout calculations on the complete interface tree and render the calculated drawing interface to generate the display interface.

10. The apparatus according to claim 9, characterized in that, Also includes: The error handling module is used to capture exceptions and execute predefined error handling strategies when exceptions occur during XML configuration file parsing, or during dynamic object creation, setting of properties and slots. The error handling strategies include logging, replacing invalid nodes with default controls, or ignoring invalid nodes. The security verification module is used to perform whitelist verification on the control type class name and layout type class name specified in the XML configuration file before dynamically creating objects. The whitelist verification is used to prevent the instantiation of unauthorized classes.

Citation Information

Patent Citations

  • Dynamic generation method for graphical user interface

    CN104239044A

  • An operation method of a real-time GUI editor based on QtScript script

    CN109408054A

  • User interface mitigation techniques for modular energy systems

    CN117121382A

  • Qt interface and style automatic generation method and system based on HTML (Hypertext Markup Language) file format

    CN117311692A

  • Rendering method based on node tree

    CN118657870A

Cited By

  • Table cell style rendering method based on node attribute extension

    CN122065781A

  • A table cell style rendering method based on node attribute extension

    CN122065781B

  • Model performance data display method, device, equipment and medium

    CN122387927A