代码之家  ›  专栏  ›  技术社区  ›  Denis Steinman

为什么我的窗口不显示?

  •  1
  • Denis Steinman  · 技术社区  · 6 年前

    我需要写一个 GrabWindow ,所以我派生了我的类 抓取窗口 从…起 QQuickWindow :

    #include <QtQuickWidgets/QtQuickWidgets>
    #include <QString>
    
    class GrabWindow : public QQuickWindow {
        Q_OBJECT
    public:
        explicit GrabWindow(QQuickWindow *parent = nullptr);
    
    public slots:
        void capture(QString const &path);
    };
    // .CPP
    #include "grab_window.h"
    #include <QImage>
    
    GrabWindow::GrabWindow(QQuickWindow *parent) : QQuickWindow(parent) {
    
    }
    
    void GrabWindow::capture(const QString &path) {
        QImage img = this->grabWindow();
        img.save(path);
    }
    

    在QML中注册后: qmlRegisterType<GrabWindow>("myapp", 1, 0, "GrabWindow"); 在QML中定义窗口后:

    import QtQuick 2.4
    import QtQuick.Controls 2.2
    import QtQuick.Window 2.3
    import myapp 1.0
    
    GrabWindow {
        id : translationWindow
        width : 1024
        height : 768
        color: "transparent"
        visibility: "FullScreen"
        visible: true;
        signal capture(string path)
    
        MouseArea {
            anchors.fill: parent
            onClicked: translationWindow.capture("/home/user/saveTest.jpg")
        }
    }
    

    但它不会在开始时显示(我知道它是透明的,我的意思是抓取窗口不会开始显示)。如果代替 抓取窗口 我使用 Window ApplicationWindow 然后一切都很好,我看到了一个透明的全屏幕窗口。
    怎么了?

    1 回复  |  直到 6 年前
        1
  •  1
  •   GrecKo    6 年前

    你的 GrabWindow 未显示,因为在设置 visible 属性它与您使用 Window 看得见的 所有物

    你的只是 看得见的 的属性 QWindow 不直接实例化 QQuickWindow ,它实例化一个私有Qt类 QQuickWindowImpl 将覆盖 看得见的 具有自定义属性的属性。 这似乎延迟了 QWindow::setVisible 在以后的时间。

    因此,我认为 QQuickWindow 意味着继承自。你可以试试 visible = true 在您的 Component.onCompleted 但我不确定这会解决你的问题。

    相反,我建议您不要子类化 QQuickWindow 但只需创建一个新类型并将其传递给现有类型

    可能的API可能是:

    Window {
        id: myWindow
        //...
        MouseArea {
            anchors.fill: parent
            onClicked: WindowGrabber.grab(myWindow, path) //singleton type
        }
    }
    

    Window {
        id: myWindow
        //...
        WindowGrabber { // regular type
            id: windowGrabber
            window: myWindow
        }
        MouseArea {
            anchors.fill: parent
            onClicked: windowGrabber.grab(path) // you could even add a path property in WindowGrabber and not have it as a function parameter if that makes sense for your use case
        }
    }