Language switching method and system based on QT development application
By creating a data structure based on attribute interface in a QT application and recording the source language text of objects and attributes, the problem of restarting the application in the prior art language switching is solved, dynamic language switching is realized, and code readability and flexibility are improved.
Patent Information
- Application Number
- CN202510406691.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-02
- Publication Date
- 2025-05-02
- Estimated Expiration
- 2045-04-02
AI Technical Summary
Existing QT-based applications need to restart the application or add a large number of data members during language switching, resulting in static language switching, low code readability and insufficient flexibility.
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.
It enables dynamic language switching without restarting the application, reduces the difficulty of software design, and improves code readability and flexibility.
Smart Images

Figure CN119917079A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computers, and more specifically, to a language switching method and system based on QT development and application. Background Art
[0002] The language switching of existing applications developed based on QT 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, and use QT's translation mechanism to 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 separately at runtime: this method requires the developer to set each object that needs to be translated in the entire interface as a data member during design, call the language switching method after loading the translation file, 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 with the above two language switching methods are: 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, the code readability is low, and when the object to be translated is destroyed, it will still be called, which cannot adapt to complex scenarios such as dynamic pages and has low flexibility.
[0004] Therefore, the present application provides a language switching method and system based on QT development application to solve the above problems. Summary of the invention
[0005] The purpose of this application is to provide a language switching method and system based on QT development application, to solve the problem that the existing QT language switching method requires restarting the application and maintaining a large number of data members; this application creates a new data structure and cooperates with QT's attribute interface to realize a concise, easy-to-read and easy-to-maintain dynamic language switching method.
[0006] The present application first provides a language switching method for developing applications based on QT, including: creating 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, and entering data members in the form of a combination of objects, attributes, and source language texts; 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 attribute in the data member to obtain the source language text, calling the QT translation method to translate the source language text into a translation text, and setting the attribute value of the object according to the translation text.
[0007] 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.
[0008] In one possible implementation, the associative container is 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.
[0009] In a possible implementation, the method further includes: when traversing each object and attribute in the data member, if it is found that the QPointer is set to nullptr, removing the corresponding object, attribute, and source language text combination from the data member.
[0010] The present application also provides a language switching system for developing applications based on QT, including: an object recording unit, which is used to create a data structure based on the QT attribute interface to record the objects that need to be translated, the attribute names of the objects that need to be translated, and the source language text of the attribute values, and enter data members in the form of a combination of objects, attributes, and source language texts; a switching response unit, which is used to respond to a language switching instruction, unload the source language file and load a new language file according to the language to be translated, traverse each object and attribute in the data member to obtain the source language text, call the QT translation method to translate the source language text into a translation text, and set the attribute value of the object according to the translation text.
[0011] In a possible implementation, the data structure in the object recording unit 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 and source language text of the object.
[0012] In one possible implementation, 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.
[0013] 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 if it is found that the QPointer is set to nullptr when traversing each object and attribute in the data member.
[0014] Compared with the prior art, the present application has the following beneficial effects: it adopts QT's attribute interface for language switching. In QT's native components, the values of displayable data are almost all registered with attribute interfaces. Therefore, the corresponding attribute values can be modified by knowing the object and attribute names, and language switching is achieved by setting the object attribute values; the language switching method of the present application can achieve language switching without restarting the application, and can achieve language switching of dynamic pages; the language switching method of the present application does not need to set each text that needs to be translated, and language switching can be achieved through the attribute interface, which reduces the difficulty of software design and improves 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, constitute a part of this application, and do not constitute a limitation of the embodiments of the present invention. In the drawings: Figure 1 A flowchart of a language switching method for developing applications based on QT provided in an embodiment of the present application; Figure 2 A timing diagram of a language switching method for developing applications based on QT provided in an embodiment of the present application; Figure 3 This is a structural diagram of the language switching system based on QT development application provided in an embodiment of the present application. DETAILED DESCRIPTION
[0016] Hereinafter, the terms "include" or "may include" used in various embodiments of the present application indicate the presence of the claimed function, operation or element, and do not limit the addition of one or more functions, operations or elements. In addition, as used in various embodiments of the present application, the terms "include", "have" and their cognates are intended only to indicate specific features, numbers, steps, operations, elements, components or combinations of the foregoing, and should not be understood as first excluding the presence of one or more other features, numbers, steps, operations, elements, components or combinations of the foregoing or the possibility of adding one or more features, numbers, steps, operations, elements, components or combinations of the foregoing.
[0017] The terms used in the various embodiments of the present application are only used 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 form is intended to also include the plural form, unless the context clearly indicates otherwise. Unless otherwise defined, all terms (including technical terms and scientific terms) used herein have the same meaning as the meanings commonly understood by ordinary technicians in the field to which the various embodiments of the present application belong. Terms (such as terms defined in generally used dictionaries) will be interpreted as having the same meaning as the contextual meaning in the relevant technical field and will not be interpreted as having an idealized meaning or an overly formal meaning, unless clearly defined in the various embodiments of the present application.
[0018] In order to make the objectives, technical solutions and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with examples and drawings. The illustrative implementation scheme of the present application and its description are only used to explain the present application and are not intended to limit the present application.
[0019] See also Figure 1 As shown, Figure 1 A flowchart of a language switching method for developing applications based on QT is provided for an embodiment of the present application. It includes: creating a data structure based on the QT attribute interface to record the objects to be translated, the attribute names and attribute value source language texts of the objects to be translated, and entering data members in the form of a combination of objects, attributes, and source language texts; responding 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 attribute in the data member to obtain the source language text, calling the QT translation method to translate the source language text into the translation text, and setting the attribute value of the object according to the translation text.
[0020] Specifically, for each object that needs to be translated, a self-created data structure is used to record the object, its attributes that need to be translated, and its source language text, and the data member is entered in the form of a combination of "object-attribute-source language text". In response to the 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 update; at this time, the source language file is unloaded and the new language file is loaded according to the switched target language, and each window to be updated captures the language switching event, traverses the "object-attribute" in the data member to obtain the source language text; the source language text is translated through the QT translation method (such as the tr function) to obtain the translated text; finally, the attribute value of the object is set to the translated text, and the text after the language switch can be displayed on the updated window to achieve language switching.
[0021] 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.
[0022] 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.
[0023] 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.
[0024] 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 object's attribute name and source language text on the inner layer. This nested data structure can efficiently record multiple "object-attribute-source language text" combinations, making it easier to traverse when switching languages.
[0025] In one possible implementation, the associative container is a QPointer <qobject>Type record object, where QPointer is a weak pointer to the QObject object, which is automatically set to nullptr when the object is destroyed. <qobject>By recording objects, you can promptly detect whether the objects have been destroyed.
[0026] Specifically, QPointer is a weak pointer provided by QT. <qobject>It is a weak pointer to a QObject object. This application uses QPointer, which can sense in time when the QObject object is destroyed.
[0027] In a possible implementation, the method further includes: when traversing each object and attribute in the data member, if it is found that the QPointer is set to nullptr, removing the corresponding object, attribute, and source language text combination from the data member.
[0028] Specifically, considering the life cycle of an object, it is necessary to remove the destroyed object from the data member in a timely manner. Therefore, when the next language switch traverses this container, if a QPointer <qobject>If it is nullptr, it is removed.
[0029] It is understandable that this application records objects as QPointer <qobject>Type can ensure timely perception and response to destroyed objects, and can adapt to the needs of complex scenarios such as dynamic pages.
[0030] In a possible implementation, QT will trigger a language switch event when loading or unloading a language file. Therefore, when a language switch event is triggered, logic can be added to the code to filter out irrelevant events, ensuring that the language switch event is triggered only when the language changes, and notifying all windows to update the interface; specifically, logic can be set in the code to check whether the currently loaded new language file is the same as the source language file, and if they are the same, the language switch event will not be triggered.
[0031] In a possible implementation, in addition to the QT translation method tr function provided by QT, a custom translation function may be added as a supplement to the QT translation method to provide automatic translation support for exceptional situations.
[0032] See also Figure 2 As shown, Figure 2 A timing diagram of a language switching method for developing applications based on QT provided in an embodiment of the present application. In the figure, the dotted arrow represents a synchronous message, the solid arrow represents an asynchronous message, and the return arrow represents a return message. The figure shows the timing content of the application startup part and the language switching part.
[0033] The application startup part includes: the user interface receives a startup instruction issued by the user (1.1 startup application), the language switching module records the object-attribute-source language text combination that needs to be translated in response to the startup instruction (1.2 records the translation object-attribute-source language text combination), the language switching module calls the translation method of the QT translation framework (1.3 calls the translation method), the QT translation framework performs the translation to obtain the translated text (1.4 translates the text), the QT translation framework returns the translated text to the language switching module (1.5 returns the translated text), the language switching module sets the translated text as the attribute value of the object (1.6 sets the object-attribute value), the language switching module sends the object attribute value to the user interface for display (1.7 draws), and the user interface is presented to the user (1.8 presents).
[0034] The language switching part includes: the user interface receives a language switching instruction issued by the user (2.1 switching language), the language switching module switches the default regional setting in response to the language switching instruction (2.2 switching regional setting), the language switching module unloads the source language file and loads a new language file (2.3 switching translation file), the language switching module traverses all objects and attributes that need to be translated in the data member (2.4 traversing objects-attributes), the language switching module obtains the source language text according to the traversal result (2.5 obtaining the source language text), the language switching module calls the QT translation method of the QT translation framework (2.6 calling the translation method), the QT translation framework executes the QT translation method to obtain the translation text (2.7 translating the text), the QT translation framework returns the translation text to the language switching module (2.8 returning the translation text), the language switching module sets the attribute value of the object according to the translation text (2.9 setting the object-attribute value), the language switching module sends the object attribute value to the user interface display (2.10 drawing), and the user interface is presented to the user (2.11 presenting).
[0035] It can be understood that the language switching method for QT-based application development provided by this application can greatly reduce the complexity of program design and improve readability and maintainability when a large number of window interface objects need to be translated; this application implements dynamic language switching, allowing QT applications to display different language texts according to user selection. This application utilizes QT's attribute system and event mechanism to make the language switching process more flexible and efficient.
[0036] See also Figure 3 As shown, Figure 3 The structural diagram of the language switching system based on QT development application provided in the embodiment of the present application. It includes: an object recording unit, which is used to create a data structure based on the QT attribute interface to record the objects to be translated, the attribute names and attribute value source language texts of the objects to be translated, and enter the data members in the form of a combination of objects, attributes, and source language texts; a switching response unit, which is used to respond to the switching language instruction, unload the source language file and load a new language file according to the language to be translated, traverse each object and attribute 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, and set the attribute value of the object according to the translation text.
[0037] In a possible implementation, the data structure in the object recording unit 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 and source language text of the object.
[0038] In one possible implementation, 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.
[0039] 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 if it is found that the QPointer is set to nullptr when traversing each object and attribute in the data member.
[0040] It can be understood that the language switching system based on QT development application provided in this application is used to implement the above method, corresponds to the above method one by one, and has corresponding technical effects, so it is not elaborated here.
[0041] The above specific implementation methods further illustrate the objectives, technical solutions and beneficial effects of the present invention in detail. It should be understood that the above are only specific implementation methods of the present invention and are not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the scope of protection 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: 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, and enter the 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, each object and attribute in the data member is traversed to obtain the source language text, the QT translation method is called to translate the source language text into the translation text, and the attribute value of the object is set according to the translation text.
2. A language switching method for developing applications based on QT according to claim 1, characterized in that: 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.
3. A language switching method for developing applications based on QT according to claim 2, 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> 4. A language switching method for developing applications based on QT according to claim 3, 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.
5. A language switching system based on QT development application, characterized in that: include: The object recording unit is used to 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; A switching response unit is used to respond to a language switching instruction, unload the source language file and load a new language file according to the language to be translated, traverse each object and attribute in the data member to obtain the source language text, call the QT translation method to translate the source language text into a translation text, and set the attribute value of the object according to the translation text.
6. A language switching system based on QT development application according to claim 5, characterized in that: 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.
7. A language switching system based on QT development application according to claim 6, 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> 8. The language switching system based on QT development application according to claim 5, 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
Operating system language switching method and device
CN104516781A
Method and device for realizing QT multi-language translation system and related components
CN114282550A
International translation system for front-end development
CN118966242A