A Language Switching Method and System for Applications Developed Based on QT

By using data structures based on attribute interfaces and nested container structures in QT applications, dynamic language switching is implemented, solving the problem of high complexity of restarting the application and code in the prior art language switching, and improving flexibility and readability.

CN119917079BActive Publication Date: 2025-07-01CHENGDU PAIZ INTERCONNECT ELECTRONIC TECHNOLOGY CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202510406691.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-04-02
Publication Date
2025-07-01
Estimated Expiration
2045-04-02

AI Technical Summary

Technical Problem

The existing QT-based application language switching method has the problem of static switching requiring restart of the application, low code readability and insufficient flexibility.

Method used

By creating a data structure based on the QT attribute interface, record the objects, attribute names and source language text that need to be translated, and combine the nested structure of the associated container and hash table container to achieve dynamic language switching without restarting the application.

Benefits of technology

It realizes the ability to switch languages ​​dynamically without restarting the application, reduces code complexity and design difficulty, and improves code readability and flexibility.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119917079B_ABST
    Figure CN119917079B_ABST
Patent Text Reader

Abstract

The present invention discloses a language switching method and system for QT-based applications, which relates to the field of computers and solves the problems of the existing QT language switching method that requires restarting the application program and has complex software design. The key points of its technical solution are as follows: It includes creating a data structure based on the QT property interface to record the objects that need to be translated, the property names of the objects that need to be translated, and the source language text of the property values, and entering the data members in the form of a combination of the object, the property, and the source language text; in response to the language switching instruction, unloading the source language file and loading the new language file according to the language to be translated, traversing each object and property in the data members to obtain the source language text, calling the QT translation method to translate the source language text into the translated text, and setting the property value of the object according to the translated text; implementing a simple, readable, and easy-to-maintain dynamic language switching method through the QT property interface.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computers, and more specifically, to a method and system for language switching in applications developed based on QT. Background Art

[0002] For existing applications developed based on QT, their language switching can be roughly divided into two categories: One is to load the language translation file at startup: This method is to load the corresponding language translation file (.qm file) according to the language selected by the user when the application starts. Through the translation mechanism of QT, find the translation corresponding to each string in the language translation file, and set the translated string to each object to achieve language switching. The other is to set the language for each object individually during runtime: This method requires developers to set each object that needs to be translated on the entire interface as a data member during design. After loading the translation file, call the language switching method, generate the default translation of the interface design code through the ui file, and reset the text content of the interface elements according to the current language environment through the retranslateUi method.

[0003] The problems existing in the above two language switching methods are as follows: The first method can only achieve static language switching, and the application must be restarted after each switch to take effect, which interferes with the ongoing work. The second method itself will add a large number of data members, with low code readability, and will still be called after the object that needs to be translated is destroyed, and cannot meet the requirements of complex scenarios such as dynamic pages, with low flexibility.

[0004] Therefore, the present application provides a method and system for language switching in applications developed based on QT to solve the above problems. Summary of the Invention

[0005] The purpose of the present application is to provide a method and system for language switching in applications developed based on QT, to solve the problems that the existing QT language switching method requires restarting the application and maintaining a large number of data members; the present application creates a new data structure and cooperates with the property interface of QT to implement a simple, readable, and easy-to-maintain dynamic language switching method.

[0006] The present application first provides a method for language switching in applications developed based on QT, including: creating a data structure based on the QT property interface to record the objects that need to be translated, the names of the properties that the objects need to be translated, and the source language texts of the property values, and entering the data members in the form of a combination of the object, property, and source language text; in response to a language switching instruction, unload the source language file and load a new language file according to the language that needs to be translated, traverse each object and property in the data members to obtain the source language text, call the QT translation method to translate the source language text into the translated text, and set the property value of the object according to the translated text.

[0007] In a possible implementation, the data structure is a nested structure composed of an associative container and a hash table container. The associative container is used to record objects, and the hash table container is used to record the attributes of the objects and the source language text.

[0008] In a possible implementation, the associative container uses QPointer <qobject>Type record object, where QPointer is a weak pointer pointing to the QObject object and is automatically set to nullptr when the object is destroyed.

[0009] In a possible implementation, the method further includes: when traversing each object and property in the data member, if it is found that the QPointer is set to nullptr, the corresponding combination of object, property, and source language text is removed from the data member.

[0010] This application also provides a language switching system for developing applications based on QT, including: an object recording unit for creating a data structure based on the QT property interface to record the objects to be translated, the property names of the objects to be translated, and the source language text of the property values, and entering them into the data member in the form of a combination of object, property, and source language text; a switching response unit for responding to the language switching instruction, unloading the source language file and loading a new language file according to the language to be translated, traversing each object and property in the data member to obtain the source language text, calling the QT translation method to translate the source language text into the translated text, and setting the property value of the object according to the translated text.

[0011] In a possible implementation, the data structure in the object recording unit is a nested structure composed of an associative container and a hash table container. The associative container is used to record objects, and the hash table container is used to record the properties of the objects and the source language text.

[0012] In a possible implementation, the associative container in the object recording unit uses QPointer <qobject>A type record object, where QPointer is a weak pointer pointing to the QObject object and is automatically set to nullptr when the object is destroyed.

[0013] In a possible implementation, the system further includes: an object update unit, which is configured to remove the corresponding object, attribute, and source language text combination from the data members when it is found that the QPointer is set to nullptr while traversing each object and attribute in the data members.

[0014] Compared with the prior art, the present application has the following beneficial effects: The QT property interface is used for language switching. In the native components of QT, almost all the values that can be displayed are registered with property interfaces. Therefore, knowing the object and property names can modify the corresponding property values, and language switching can be achieved by setting the object property values; The language switching method of the present application can achieve language switching without restarting the application program, and can achieve language switching for dynamic pages; The language switching method of the present application does not need to set each piece of text that needs to be translated, and language switching can be achieved through the property interface, which reduces the software design difficulty and improves the code readability. Brief Description of the Drawings

[0015] The drawings described herein are used to provide a further understanding of the embodiments of the present invention, form a part of the present application, and do not limit the embodiments of the present invention. In the drawings:

[0016] Figure 1 It is a flowchart of the language switching method for applications developed based on QT provided by the embodiments of the present application;

[0017] Figure 2 It is a timing diagram of the language switching method for applications developed based on QT provided by the embodiments of the present application;

[0018] Figure 3 It is a structural diagram of the language switching system for applications developed based on QT provided by the embodiments of the present application. Detailed Embodiments

[0019] Hereinafter, the term "comprising" or "may comprise" that can be used in various embodiments of the present application indicates the presence of the claimed functions, operations, or elements, and does not limit the addition of one or more functions, operations, or elements. Further, as used in various embodiments of the present application, the terms "comprising", "having" and their cognates are only intended to mean the presence of specific features, numbers, steps, operations, elements, components, or combinations of the foregoing items, and should not be construed as precluding the existence or addition of the possibility of one or more other features, numbers, steps, operations, elements, components, or combinations of the foregoing items.

[0020] The terms used in various embodiments of the present application are only for the purpose of describing specific embodiments and are not intended to limit the various embodiments of the present application. As used herein, the singular forms are intended to include the plural forms as well, unless the context clearly indicates otherwise. Unless otherwise defined, all terms (including technical and scientific terms) used herein have the same meaning as commonly understood by one of ordinary skill in the art to which the various embodiments of the present application belong. Terms (such as those defined in a general use dictionary) will be interpreted as having the same meaning as their context in the relevant technical field and will not be interpreted as having an idealized meaning or being overly formal, unless clearly defined in the various embodiments of the present application.

[0021] To make the objectives, technical solutions and advantages of the present application more clear and understandable, the present application will be further described in detail below with reference to embodiments and drawings. The illustrative embodiments and descriptions of the present application are only for explaining the present application and do not serve as a limitation to the present application.

[0022] Please refer to Figure 1 as shown Figure 1 The flowchart of the language switching method for an application developed based on QT provided by an embodiment of the present application. It includes: creating a data structure based on the QT property interface to record the object to be translated, the property name of the object to be translated, and the source language text of the property value, and entering the data members in the form of a combination of the object, property, and source language text; in response to a language switching instruction, unloading the source language file and loading a new language file according to the language to be translated, traversing each object and property in the data members to obtain the source language text, calling the QT translation method to translate the source language text into a translated text, and setting the property value of the object according to the translated text.

[0023] Specifically, for each object to be translated, a self-created data structure is used to record the object, its properties to be translated, and the source language text thereof, and the data members are entered in the form of a combination of "object-property-source language text". In response to a language switching instruction issued by the user, QT triggers a language switching event, switches the default locale to the locale of the target language to adapt to different languages and cultural habits, and notifies each top-level window to be updated; at this time, the source language file is unloaded and a new language file is loaded according to the switched target language, each window to be updated captures the language switching event, traverses the "object-property" in the data members to extract the source language text; the source language text is translated into a translated text through the QT translation method (such as the tr function); finally, the property value of the object is set to the translated text, so that the text after language switching can be displayed on the updated window, realizing language switching.

[0024] The following example illustrates the translation process of this application. For example, a QObject object has an attribute objectName, whose value is the source language text tool (the source language text is usually in English), and records "QObject-objectName-tool". When traversing this QObject object during translation, when the attribute objectName is used, the source language text "tool" is taken out. A Qt translation method is called, such as the tr function, and the translated text is "tool", then the attribute value of objectName is set to "tool" to complete the translation.

[0025] The improvement of this application is that it uses QT's attribute interface to switch languages. In QT's native components, the values ​​of displayable data are almost all registered with attribute interfaces. Therefore, if you know the object and attribute name, you can modify the corresponding attribute value, and achieve language switching by setting the object attribute value. Compared with the existing first language switching method, the language switching method of this application can achieve language switching without restarting the application, and can achieve dynamic language switching; compared with the existing second language switching method, the language switching method of this application does not need to set every object in the interface, which can effectively reduce the number of data members to be maintained, reduce the difficulty of software design, and improve code readability.

[0026] In a possible implementation, the data structure is a nested structure consisting of an associative container and a hash table container, the associative container is used to record objects, and the hash table container is used to record attributes of the objects and source language text.

[0027] Specifically, the data structure can be defined as: "QMap <QPointer <qobject>, QHash<const char*, const char *>> sourceTexts". The data structure sourceTexts uses an associative container QMap to record objects on the outer layer and a hash table container QHash to record the property names and source language texts of the objects on the inner layer. This nested data structure can efficiently record multiple "object-property-source language text" combinations, facilitating the traversal work when switching languages.

[0028] In a possible implementation, the associative container uses QPointer <qobject>Type record object, where QPointer is a weak pointer pointing to a QObject object and is automatically set to nullptr when the object is destroyed. Using QPointer <qobject>The object to be recorded can promptly detect whether the object has been destroyed.

[0029] Specifically, QPointer is a weak pointer provided by QT. QPointer <qobject>It is a weak pointer pointing to a QObject object. This application uses QPointer, which can detect in time when the QObject object is destroyed.

[0030] In a possible implementation, the method further includes: when traversing each object and property in the data member, if it is found that the QPointer is set to nullptr, the corresponding combination of object, property, and source language text is removed from the data member.

[0031] Specifically, considering the life cycle of the object, it is necessary to remove the already destroyed object from the data member in time. Therefore, when traversing the container during the next language switch, if the QPointer is found <qobject>If it is nullptr, remove it.

[0032] It can be understood that in this application, the object is recorded as QPointer <qobject>The type can ensure timely perception and response to destroyed objects and can meet the requirements of complex scenarios such as dynamic pages.

[0033] In a possible implementation, QT triggers a language switch event when loading and unloading language files. Therefore, when a language switch event is triggered, logic can be added in the code to filter out irrelevant events to ensure that the language switch event is only triggered when the language changes, and all windows are notified to update the interface. Specifically, logic can be set in the code to check whether the newly loaded language file is the same as the source language file. If they are the same, the language switch event is not triggered.

[0034] In a possible implementation, in addition to the tr function of the QT translation method provided by QT itself, a custom translation function can be added as a supplement to the QT translation method to provide automatic translation support for exceptional cases.

[0035] Please refer to Figure 2 as shown in Figure 2 is the timing diagram of the language switch method for applications developed based on QT provided by the embodiments of the present application. In the figure, the dashed arrows represent synchronous messages, the solid arrows represent asynchronous messages, and the return arrows represent return messages. The timing content of the application startup part and the language switch part is shown in the figure.

[0036] The application startup part includes: the user interface receives a startup instruction issued by the user (1.1 Start the application), the language switch module responds to the startup instruction and records the object-attribute-source language text combination to be translated (1.2 Record the translation object-attribute-source language text combination), the language switch module calls the translation method of the QT translation framework (1.3 Call the translation method), the QT translation framework performs translation to obtain the translation text (1.4 Translation text), the QT translation framework returns the translation text to the language switch module (1.5 Return the translation text), the language switch module sets the translation text as the attribute value of the object (1.6 Set the object-attribute value), the language switch module sends the object attribute value to the user interface for display (1.7 Render), and the user interface presents it to the user (1.8 Present).

[0037] The language switching part includes: the user interface receives a language switching instruction issued by the user (2.1 Switch language), the language switching module responds to the language switching instruction to switch the default locale (2.2 Switch locale), the language switching module unloads the source language file and loads the new language file (2.3 Switch translation file), the language switching module traverses all the objects and properties that need to be translated in the data members (2.4 Traverse object - property), the language switching module obtains the source language text according to the traversal result (2.5 Obtain source language text), the language switching module calls the QT translation method of the QT translation framework (2.6 Call translation method), the QT translation framework executes the QT translation method to obtain the translated text (2.7 Translate text), the QT translation framework returns the translated text to the language switching module (2.8 Return translated text), the language switching module sets the property value of the object according to the translated text (2.9 Set object - property value), the language switching module sends the object property value to the user interface for display (2.10 Render), and the user interface presents it to the user (2.11 Present).

[0038] It can be understood that the language switching method for QT - based applications provided in this application can greatly reduce the complexity of program design, improve readability and maintainability when there are a large number of window interface objects that need to be translated; this application realizes dynamic language switching, enabling QT applications to display different language texts according to the user's selection. This application utilizes the property system and event mechanism of QT, making the language switching process more flexible and efficient.

[0039] Please refer to Figure 3 as shown Figure 3 This is the structural diagram of the language switching system for QT - based applications provided by the embodiments of this application. It includes: an object recording unit, which is used to create a data structure based on the QT property interface to record the objects that need to be translated, the names of the properties that the objects need to be translated, and the source language text of the property values, and enter them into the data members in the form of a combination of objects, properties, and source language text; a switching response unit, which is used to respond to the language switching instruction, unload the source language file and load the new language file according to the language that needs to be translated, traverse each object and property in the data members to obtain the source language text, call the QT translation method to translate the source language text into the translated text, and set the property value of the object according to the translated text.

[0040] In a possible implementation manner, the data structure in the object recording unit is a nested structure composed of an associative container and a hash table container. The associative container is used to record objects, and the hash table container is used to record the properties of the objects and the source language text.

[0041] In a possible implementation manner, the associative container in the object recording unit uses QPointer <qobject>Type record object, where QPointer is a weak pointer pointing to the QObject object and is automatically set to nullptr when the object is destroyed.

[0042] In a possible implementation, the system further includes: an object update unit, which is used to remove the corresponding object, attribute, and source language text combination from the data member when it is found that the QPointer is set to nullptr while traversing each object and attribute in the data member.

[0043] It can be understood that the language switching system for developing applications based on QT provided by this application is used to implement the above method, corresponding one by one to the above method, and has corresponding technical effects, so it will not be elaborated here.

[0044] The above specific implementation manners further elaborate on the purpose, technical solution, and beneficial effects of the present invention. It should be understood that the above are only the specific implementation manners of the present invention and are not used to limit the protection scope of the present invention. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.< / qobject> < / qobject> < / qobject> < / qobject> < / qobject> < / qobject> < / qobject> < / qobject> < / qobject>

Claims

1. A language switching method for developing applications based on QT, characterized in that: include: Use QT's attribute interface to switch languages. Create a data structure based on the QT attribute interface to record the objects to be translated, the attribute names of the objects to be translated, and the source language text of the attribute values. Enter data members in the form of a combination of objects, attributes, and source language text. In response to the language switching instruction, the source language file is unloaded and a new language file is loaded according to the language to be translated. The various objects and attributes in the data member are traversed to obtain the source language text. The QT translation method is called to translate the source language text into the translation text. The attribute value of the object is set according to the translation text. The language switching is realized by setting the object attribute value. The QPointer is used. <qobject> Record objects to promptly detect whether they have been destroyed;< / qobject> The data structure is a nested structure composed of an associative container and a hash table container. The associative container is used to record objects, and the hash table container is used to record the attributes and source language text of the objects.

2. A language switching method for developing applications based on QT according to claim 1, characterized in that: The associative container uses QPointer <qobject> Type record object, where QPointer is a weak pointer to the object QObject, which is automatically set to nullptr when the object is destroyed.< / qobject> 3. A language switching method for developing applications based on QT according to claim 2, characterized in that: Also includes: When traversing the objects and attributes in the data member, if the QPointer is found to be set to nullptr, the corresponding object, attribute, and source language text combination will be removed from the data member.

4. A language switching system based on QT development application, characterized in that: include: The object recording unit is used to switch the language using the QT attribute interface, create a data structure based on the QT attribute interface to record the object to be translated, the attribute name of the object to be translated and the source language text of the attribute value, and enter the data member in the form of a combination of object, attribute and source language text; The switching response unit is used to respond to the language switching instruction, unload the source language file and load the new language file according to the language to be translated, traverse the various objects and attributes in the data member to obtain the source language text, call the QT translation method to translate the source language text into the translation text, set the attribute value of the object according to the translation text, and realize language switching by setting the object attribute value. QPointer is used <qobject> Record objects to promptly detect whether they have been destroyed;< / qobject> The data structure in the object recording unit is a nested structure composed of an associative container and a hash table container. The associative container is used to record objects, and the hash table container is used to record attributes and source language text of the object.

5. A language switching system based on QT development application according to claim 4, characterized in that: The associative container in the object record unit is represented by a QPointer <qobject> Type record object, where QPointer is a weak pointer to the object QObject, which is automatically set to nullptr when the object is destroyed.< / qobject> 6. A language switching system based on QT development application according to claim 4, characterized in that: Also includes: The object update unit is used to remove the corresponding object, attribute, and source language text combination from the data member when traversing each object and attribute in the data member. If the QPointer is found to be set to nullptr, the object update unit is used to remove the corresponding object, attribute, and source language text combination from the data member.

Citation Information

Patent Citations

  • International translation system for front-end development

    CN118966242A