A method and system for obtaining the size of a screen available area based on Qt

By obtaining the window handle and geometric information of the taskbar, the coordinates and size of the taskbar relative to the screen are obtained from the X server, which solves the problem of inaccurate screen available area size in the existing technology in multi-screen environment and realizes accurate calculation of available area in multi-screen environment.

CN120596148BActive Publication Date: 2026-08-25KYLIN CORP
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510711451.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-05-29
Publication Date
2026-08-25
Estimated Expiration
2045-05-29

AI Technical Summary

Technical Problem

In multi-screen environments, existing technologies using Qt to obtain the available screen area size suffer from inaccuracies due to reliance on the window manager, especially in extended mode where the available area size of the primary and secondary screens is incorrectly obtained.

Method used

By obtaining the window handle and geometric information of the taskbar, the coordinates and size of the taskbar relative to the screen are obtained from the X server. The taskbar area range is calculated, and the size of the available screen area is adjusted according to whether the screen includes the taskbar area, subtracting the taskbar area size or keeping the original size.

Benefits of technology

It enables accurate acquisition of the available area size of the main screen and extended screen in a multi-screen environment, avoiding dependence on the window manager and ensuring the accuracy of the acquisition results.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120596148B_ABST
    Figure CN120596148B_ABST
Patent Text Reader

Abstract

The application discloses a method and system for obtaining the size of a screen available area based on Qt, and the method comprises the following steps: obtaining the window handle of a task bar from an X server according to the name of the task bar; obtaining the geometric information of the task bar window from the X server according to the window handle of the task bar, wherein the geometric information of the task bar window comprises the coordinates of the task bar relative to a parent window and the window size of the task bar; converting the coordinates of the task bar relative to the parent window into the coordinates of the task bar relative to a current screen, and calculating the range of the task bar area according to the coordinates of the task bar relative to the current screen and the window size of the task bar; judging whether the current screen contains the task bar area, and if yes, subtracting the size of the corresponding task bar area from the original screen area size of the current screen to obtain the available area size of the current screen, otherwise, taking the original screen area size of the current screen as the available area size. The application solves the problem that the size of the screen available area obtained by relying on a window manager may be incorrect.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computers, and more specifically to a method and system for obtaining the size of the available area of ​​a screen based on Qt. Background Technology

[0002] Qt is a cross-platform C++ framework primarily used for developing graphical user interface (GUI) programs, but it can also be used to develop command-line programs without a GUI. Graphical software developed using Qt often needs to obtain the actual usable screen size (excluding the taskbar area). This is typically achieved by calling the `availableGeometry()` interface of the `QXcbScreen` class to obtain the geometric dimensions of the screen excluding the areas reserved by the window manager (such as the taskbar and system menu). The process is as follows: Figure 1 As shown, it includes the following steps:

[0003] 1. Determine if more than one monitor is connected;

[0004] 2. If so (more than one monitor is connected), determine whether to define obtaining the workspace geometry from the window manager;

[0005] (1) If not (undefined), return the total size of this screen;

[0006] (2) If it is (defined), then obtain the workspace geometry from the window manager;

[0007] ① Return to the intersection of the dimensions of this screen and the workspace geometry;

[0008] 3. If not (only one monitor is connected), then regardless of whether the workspace geometry is defined to be obtained from the window manager, the workspace geometry is obtained from the window manager, and the intersection of the screen and workspace geometry is returned.

[0009] While existing technology can obtain the correct screen availability size when a single monitor is connected, for cases with more than one connected screen in extended mode, the obtained size of the primary or secondary screen availability may be incorrect, regardless of whether the workspace is defined to be obtained from the window manager.

[0010] Specifically, when the number of screens is greater than one and the workspace is defined as being obtained from the window manager, the `availableGeometry()` interface first calls the `getworkArea` function (which uses the `_NET_WORKAREA` property obtained from the window manager) to get the entire workspace area, and then finds its intersection with the target screen area. Since the `_NET_WORKAREA` property is obtained from the window manager, its interpretation varies across different window managers in a multi-monitor environment. This makes relying on `_NET_WORKAREA` to obtain the available area on each monitor unreliable. Verification through specific code tests shows the following results... Figure 2 As shown, in this case, the available area size of both the main and secondary screens is the remaining size after subtracting the taskbar area, indicating that the available area size of the secondary screen is incorrect.

[0011] When the number of screens is greater than 1 and the workspace is not defined to be obtained from the window manager, the returned size of the available screen area is the total screen size, not the size excluding the taskbar area. This has been verified through specific code testing. Figure 3 As shown, in this case, the available area size of both the main and secondary screens is the original screen area size, indicating that the available area size of the main screen is incorrect. Summary of the Invention

[0012] The technical problem to be solved by the present invention is to provide a method and system for obtaining the available screen area size based on Qt, which eliminates the dependence on the window manager and avoids errors in the available screen area size when the number of connected screens is greater than 1 and in extended mode.

[0013] To solve the above-mentioned technical problems, the technical solution adopted by the present invention is as follows:

[0014] A method for obtaining the size of the available area of ​​the screen based on Qt includes the following steps:

[0015] Retrieve the window handle of the taskbar from the X server based on the taskbar name;

[0016] The geometric information of the taskbar window is obtained from the X server based on the window handle of the taskbar. The geometric information of the taskbar window includes the coordinates of the taskbar relative to the parent window and the window size of the taskbar.

[0017] Convert the coordinates of the taskbar relative to the parent window to the coordinates of the taskbar relative to the current screen, and calculate the range of the taskbar area based on the coordinates of the taskbar relative to the current screen and the window size of the taskbar.

[0018] Determine whether the current screen includes the taskbar area. If it does, subtract the corresponding taskbar area size from the original screen area size to obtain the available area size of the current screen. Otherwise, use the original screen area size as the available area size.

[0019] Furthermore, when retrieving the taskbar window handle from the X server based on the taskbar name, the following steps are included:

[0020] Retrieve information about all child windows under the parent window of the taskbar from the X server to form a window tree;

[0021] Traverse the window handles in the window tree and obtain the window names;

[0022] If the obtained window name matches a known taskbar name, then the corresponding window handle is the taskbar window handle.

[0023] Furthermore, when obtaining the window name, a request is sent to the X server through the xcb_get_property function, specifying the window name as the attribute to be obtained, specifying the window corresponding to the current window handle as the window for which the attribute needs to be obtained, and then waiting for and obtaining the attribute value queried by the X server, parsing the attribute value to obtain the window name of the current window handle.

[0024] Furthermore, when retrieving the geometric information of the taskbar window from the X server based on the taskbar window handle, a request is sent to the X server through the xcb_get_geometry function, specifying the taskbar window handle as the window handle for which the geometric information is to be queried, and then waiting for and retrieving the geometric information queried by the X server.

[0025] Furthermore, when converting the coordinates of the taskbar relative to the parent window to the coordinates of the taskbar relative to the current screen, a request is sent to the X server through the xcb_translate_coordinates function. The parent window of the taskbar is specified as the source window, the window of the current screen is specified as the target window, and the coordinates of the taskbar relative to the parent window are specified as the coordinate points to be converted. Then, the converted coordinates calculated by the X server are waited for and obtained as the coordinates of the taskbar relative to the current screen.

[0026] Furthermore, before determining whether the current screen contains the taskbar area, the process includes: obtaining the geometric information of the current screen, which includes the original screen area of ​​the current screen; when determining whether the current screen contains the taskbar area, the process includes: calculating the center point of the taskbar area, determining whether the center point of the taskbar area is within the original screen area of ​​the current screen, and if so, then the current screen contains the taskbar area; otherwise, the current screen does not contain the taskbar area.

[0027] Furthermore, after calculating the range of the taskbar area based on the coordinates of the taskbar relative to the current screen and the window size of the taskbar, the process also includes: converting the type of the taskbar area to QRegion; after obtaining the geometric information of the current screen, the process also includes: converting the geometric information of the current screen to QRegion; when determining whether the center point of the taskbar area is within the original screen area of ​​the current screen, the process specifically uses the contains function of the QRegion class, taking the center point of the taskbar area as the given point and the original screen area of ​​the current screen as the judgment area, and checking whether the given point is within the judgment area.

[0028] The present invention also proposes a system for obtaining the size of the available screen area based on Qt, including a processor and a memory, wherein the memory stores a computer program, and the computer program is executed by the processor to implement the steps of the method for obtaining the size of the available screen area based on Qt.

[0029] The present invention also proposes a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the steps of the method for obtaining the available screen area size based on Qt.

[0030] The present invention also proposes a computer program product, including a computer program that, when executed by a processor, implements the steps of the method for obtaining the available screen area size based on Qt.

[0031] Compared with the prior art, the advantages of the present invention are as follows:

[0032] This invention eliminates the dependency on the window manager. Based on the taskbar name, it uses an X server to obtain the geometric information of the taskbar, converts the coordinates in the geometric information into coordinates relative to the screen, and uses these coordinates to calculate the range of the taskbar area. When the screen includes the taskbar area, the original screen area size is subtracted from the corresponding taskbar area size to obtain the available area size. When the screen does not include the taskbar area, the original screen area size is used as the available area size, which can accurately obtain the available area size of the main screen and extended screen. Attached Figure Description

[0033] Figure 1 This is a flowchart of the availableGeometry() interface of the QXcbScreen class.

[0034] Figure 2 To set up the window manager to get the available area size of the workspace, obtain the results.

[0035] Figure 3 This retrieves the results for obtaining the available area size of the workspace without setting the window manager.

[0036] Figure 4 This is a schematic diagram illustrating the steps of an embodiment of the present invention.

[0037] Figure 5 This is a detailed flowchart of an embodiment of the present invention.

[0038] Figure 6 This is a timing diagram for an embodiment of the present invention.

[0039] Figure 7 The available area size is obtained for an embodiment of the present invention. Detailed Implementation

[0040] The present invention will be further described below with reference to the accompanying drawings and specific preferred embodiments, but this does not limit the scope of protection of the present invention.

[0041] Before introducing specific embodiments of the present invention, relevant concepts or terms will be explained.

[0042] availableGeometry() interface

[0043] The `availableGeometry()` interface returns the available screen size. Prior to Qt5, this was achieved using `QApplication::desktop()->availableGeometry()` in the `QDesktopWidget` class; in Qt5 and later, it's achieved using `QGuiApplication::screens()->availableGeometry()` in the `QtGui / QScreen` class. However, when there are more than one screen and the display is in extended mode, both `QDesktopWidget` and `QtGui / QScreen` methods will produce incorrect results when obtaining the available size for a single screen. This is because in the X11 display environment, both methods ultimately call the `availableGeometry()` interface of the `QXcbScreen` class in the `xcb` module.

[0044] Example 1

[0045] In the "Kylin Desktop Operating System V10" (hereinafter referred to as "Kylin V10 system"), Qt is frequently used to develop applications. The default Qt version in the Kylin V10 system is 5.12.8. However, the latest official Qt 6.9 version uses the same method to obtain the available screen size in the QXcbScreen class as the current version. After actual testing and code analysis, the existing method (regardless of whether QApplication::desktop() or QGuiApplication::screens()'s availableGeometry() is used, they all call the availableGeometry() of the QXcbScreen class of qt xcb) may obtain the wrong size of the available area of ​​the main screen or secondary screen when the number of connected screens in the Kylin V10 system is greater than 1 and in extended mode.

[0046] To address the aforementioned issues, this embodiment proposes a method for obtaining the available screen area size based on Qt. This method combines the taskbar window name and the X server in its design, replacing the original scheme for obtaining the available screen geometry size in the availableGeometry() interface of Qt's QXcbScreen class. It removes the dependency on the window manager and ensures that the obtained available area sizes of both the main screen and the secondary screen are correct.

[0047] like Figure 4 and Figure 5 As shown, the method in this embodiment includes the following steps:

[0048] S1) Obtain the window handle of the taskbar from the X server based on the taskbar name;

[0049] S2) Obtain the geometric information of the taskbar window from the X server based on the taskbar window handle. The geometric information of the taskbar window includes the coordinates of the taskbar relative to the parent window and the window size of the taskbar.

[0050] S3) Convert the coordinates of the taskbar relative to the parent window to the coordinates of the taskbar relative to the current screen, and calculate the range of the taskbar area based on the coordinates of the taskbar relative to the current screen and the window size of the taskbar.

[0051] S4) Determine whether the current screen contains the taskbar area. If yes, subtract the corresponding taskbar area size from the original screen area size of the current screen to obtain the available area size of the current screen. Otherwise, use the original screen area size of the current screen as the available area size.

[0052] The following is a detailed explanation of each step.

[0053] Step S1 in this embodiment includes the following steps:

[0054] S11) Obtain information about all child windows under the parent window of the taskbar from the X server to form a window tree;

[0055] The component that generates the taskbar window in the system is ukui-panel, and the window name is "UKUIPanel". The parent window of the taskbar is the Root window. The Root window is automatically created by the X Server when it starts up. It is a logical mapping of all physical display devices (such as a virtual aggregation of single screens or multiple screens). Its size covers the entire display area managed by the X Server. Even if there are multiple physical screens, they are regarded as a logical single screen.

[0056] Therefore, when retrieving information about all child windows under the parent window of the taskbar from the X server, the xcb_query_tree function is used to retrieve information about all child windows under the root window from the X server through the root window handle, thus obtaining the window tree.

[0057] S12) Traverse the window handles in the window tree and get the window names;

[0058] In this embodiment, when obtaining the window name, a request is sent to the X server through the xcb_get_property function, specifying the window name as the attribute to be obtained, specifying the window corresponding to the current window handle as the window for which the attribute needs to be obtained, and then waiting for and obtaining the attribute value queried by the X server, parsing the attribute value to obtain the window name of the current window handle;

[0059] S13) If the obtained window name matches a known taskbar name, then the corresponding window handle is the taskbar window handle.

[0060] In step S2 of this embodiment, when obtaining the geometric information of the taskbar window from the X server based on the taskbar window handle, a request is sent to the X server through the xcb_get_geometry function, specifying the taskbar window handle as the window handle for which the geometric information is to be queried, and then waiting for and obtaining the geometric information queried by the X server to obtain the xcb_get_geometry_reply_t structure.

[0061] By using steps S1 and S2, the geometric information of the coordinates and window size of the taskbar relative to the Root window in "Galaxy Kylin Desktop Operating System V10" can be accurately obtained.

[0062] Step S3 in this embodiment includes the following steps:

[0063] S31) Convert the coordinates of the taskbar relative to the parent window to the coordinates of the taskbar relative to the current screen. Specifically, send a request to the X server through the xcb_translate_coordinates function, specifying the parent window of the taskbar (i.e., the root window) as the source window, specifying the window of the current screen as the target window, and specifying the coordinates of the taskbar relative to the parent window as the coordinate points to be converted. Then wait for and obtain the converted coordinates calculated by the X server, and use them as the coordinates of the taskbar relative to the current screen.

[0064] S32) Calculate the range of the taskbar area based on the coordinates of the taskbar relative to the current screen and the window size of the taskbar, and convert the type of the taskbar area to QRegion type;

[0065] S33) Obtain the geometric information of the current screen. The geometric information of the current screen includes the original screen area of ​​the current screen. Specifically, the top-left corner coordinates (usually (0,0)) and the width and height of the screen are obtained through the geometry() function. After obtaining the geometric information of the current screen, the geometric information of the current screen is converted into QRegion type for convenient subsequent calculation.

[0066] It should be noted that in this embodiment, the specific timing for obtaining the geometric information of the current screen only needs to ensure that it is completed before executing step S4. For example, the step of obtaining the geometric information of the current screen can also be executed before step S1, such as... Figure 6 As shown.

[0067] Step S3 converts the coordinates of the taskbar geometry relative to the parent window into coordinates relative to the screen. The size of the taskbar area is then calculated based on the window size and the converted coordinates. Simultaneously, the size of the taskbar area and the screen geometry are converted into QRegion type for easier subsequent calculations.

[0068] Step S4 in this embodiment includes the following steps:

[0069] S41) Determine whether the current screen contains the taskbar area. Specifically, this includes: calculating the center point of the taskbar area, determining whether the center point of the taskbar area is within the original screen area of ​​the current screen, specifically using the contains function of the QRegion class, taking the center point of the taskbar area as the given point, taking the original screen area of ​​the current screen as the judgment area, and checking whether the given point is within the judgment area. If the check result is true, then the current screen contains the taskbar area; if the check result is false, then the current screen does not contain the taskbar area.

[0070] S42) If the current screen includes a taskbar area, subtract the size of the corresponding taskbar area from the original screen area size of the current screen to obtain the available area size of the current screen.

[0071] S43) If the current screen does not contain a taskbar area, then the original screen area size of the current screen is used as the available area size.

[0072] Step S4 accurately calculates the available area of ​​the current screen (whether the taskbar area needs to be subtracted), applicable to both single-screen and multi-screen expansion scenarios. In single-screen or multi-screen replication mode, the available area size of each screen is the original screen size minus the taskbar area size; in multi-screen expansion mode, the available area size of the main screen is the original screen size minus the taskbar area size, and the available area size of other extended screens is the original screen size.

[0073] The effectiveness of the method in this embodiment will be verified through experiments below.

[0074] Test environment: The host computer is connected to two monitors and set to extended mode, both with a resolution of 1920x1080. Screen1 is the main screen, and screen2 is the extended screen. Using steps S1 to S4 on the "Galaxy Kylin Desktop Operating System V10", the available area sizes of the main screen and extended screen are obtained. The results are as follows... Figure 7 As shown, the available area sizes for both the main screen and the extended screen are correct.

[0075] Example 2

[0076] This embodiment proposes a system for obtaining the size of the available screen area based on Qt, including a processor and a memory. The memory stores a computer program, which is executed by the processor to implement the steps of the method for obtaining the size of the available screen area based on Qt described in Embodiment 1.

[0077] This embodiment also proposes a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the steps of the method for obtaining the available screen area size based on Qt as described in Embodiment 1.

[0078] This embodiment also proposes a computer program product, including a computer program that, when executed by a processor, implements the steps of the method for obtaining the available screen area size based on Qt as described in Embodiment 1.

[0079] The above description is merely a preferred embodiment of the present invention. The scope of protection of the present invention is not limited to the above embodiments. All technical solutions falling within the scope of the present invention's concept are within the scope of protection of the present invention. It should be noted that for those skilled in the art, any improvements and modifications made without departing from the principles of the present invention should also be considered within the scope of protection of the present invention.

Claims

1. A method for obtaining the size of the usable area of ​​a screen based on Qt, characterized in that, Without relying on a window manager, the following steps are included: Retrieve the window handle of the taskbar from the X server based on the taskbar name; The geometric information of the taskbar window is obtained from the X server based on the window handle of the taskbar. The geometric information of the taskbar window includes the coordinates of the taskbar relative to the parent window and the window size of the taskbar. Convert the coordinates of the taskbar relative to the parent window to coordinates of the taskbar relative to the current screen, calculate the range of the taskbar area based on the coordinates of the taskbar relative to the current screen and the window size of the taskbar; convert the type of the taskbar area to QRegion type. Obtain the geometric information of the current screen, including the original screen area, and convert the current screen geometry to a QRegion type. Determine if the current screen contains a taskbar area. If so, subtract the corresponding taskbar area size from the original screen area size to obtain the available area size of the current screen. Otherwise, use the original screen area size as the available area size. The process of determining if the current screen contains a taskbar area includes: calculating the center point of the taskbar area; determining if the center point of the taskbar area is within the original screen area of ​​the current screen; if so, the current screen contains the taskbar area; otherwise, the current screen does not contain the taskbar area. Specifically, the process of determining if the center point of the taskbar area is within the original screen area of ​​the current screen uses the `contains` function of the QRegion class, taking the center point of the taskbar area as a given point and the original screen area of ​​the current screen as the judgment area, and checking if the given point is within the judgment area.

2. The method for obtaining the available screen area size based on Qt according to claim 1, characterized in that, When retrieving the taskbar window handle from the X server based on the taskbar name, the following steps are included: Retrieve information about all child windows under the parent window of the taskbar from the X server to form a window tree; Traverse the window handles in the window tree and obtain the window names; If the obtained window name matches a known taskbar name, then the corresponding window handle is the taskbar window handle.

3. The method for obtaining the size of the usable screen area based on Qt according to claim 1, characterized in that, When retrieving the window name, the X server is requested through the xcb_get_property function. The window name is specified as the property to be retrieved, and the window corresponding to the current window handle is specified as the window for which the property is to be retrieved. Then, the X server is waited for and the property value is retrieved. The property value is parsed to obtain the window name of the current window handle.

4. The method for obtaining the size of the usable screen area based on Qt according to claim 1, characterized in that, When retrieving the geometric information of the taskbar window from the X server based on the taskbar window handle, the process involves sending a request to the X server using the xcb_get_geometry function, specifying the taskbar window handle as the handle of the window whose geometric information is to be queried, and then waiting for and retrieving the geometric information retrieved by the X server.

5. The method for obtaining the size of the usable screen area based on Qt according to claim 1, characterized in that, When converting the coordinates of the taskbar relative to its parent window to coordinates relative to the current screen, the xcb_translate_coordinates function sends a request to the X server, specifying the parent window of the taskbar as the source window, the window of the current screen as the target window, and the coordinates of the taskbar relative to its parent window as the coordinates to be converted. Then, it waits for and obtains the converted coordinates calculated by the X server, which are used as the coordinates of the taskbar relative to the current screen.

6. A system for obtaining the size of the available area of ​​a screen based on Qt, characterized in that, It includes a processor and a memory, wherein the memory stores a computer program, which is executed by the processor to implement the steps of the method for obtaining the size of the available screen area based on Qt as described in any one of claims 1 to 5.

7. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program, which, when executed by a processor, implements the steps of the method for obtaining the size of the available screen area based on Qt as described in any one of claims 1 to 5.

8. A computer program product, characterized in that, The method includes a computer program that, when executed by a processor, implements the steps of the method for obtaining the size of the available screen area based on Qt as described in any one of claims 1 to 5.

Citation Information

Patent Citations

  • Method of detecting and treating tuberous sclerosis complex associated disorders

    AU2002226079A1

  • Window setting method and device

    CN106020831A