Getting a list of window WIds in QT

前端 未结 1 1862
无人共我
无人共我 2021-01-23 04:33

I\'m writing a library in QT which will take screenshots of arbitrary external windows. I know how to take the screenshot using QScreen::grabWindow(), but this take

相关标签:
1条回答
  • 2021-01-23 04:41

    This isn't possible with Qt. If you want your library to be platform independent, you need to write a code for each platform you want to support.

    To make this platform independent, you have to write a (public) function in which you test for the platform using preprocessor #if:

    #ifdef __unix__
        // call unix specific code
    #elseif ...
        // other platforms
    #else
    #error Platform not supported!
    #endif
    

    For the unix specific code, you need to use xlib, which manages the windows in a tree. From the following code, you will get ALL windows, and in X11 there are a lot of invisible windows and windows which you don't think that they are separate windows. So you definitely have to filter the results, but this depends on which window types you want to have.

    Take this code as a start:

    #include <X11/Xlib.h>
    
    // Window is a type in Xlib.h
    QList<Window> listXWindowsRecursive(Display *disp, Window w)
    {
        Window root;
        Window parent;
        Window *children;
        unsigned int childrenCount;
    
        QList<Window> windows;
        if(XQueryTree(disp, w, &root, &parent, &children, &childrenCount))
        {
            for(unsigned int i = 0; i < childrenCount; ++i)
            {
                windows << children[i];
                windows << listXWindowsRecursive(disp, children[i]);
            }
            XFree(children);
        }
        return windows;
    }
    
    Display *disp = XOpenDisplay(":0.0");
    Window rootWin = XDefaultRootWindow(disp);
    QList<Window> windows = listXWindowsRecursive(disp, rootWin);
    
    foreach(Window win, windows)
    {
        // Enumerate through all windows
    }
    
    0 讨论(0)
提交回复
热议问题