gpt4 book ai didi

c++ - 为什么我的窗口不显示?

转载 作者:行者123 更新时间:2023-11-30 05:01:15 27 4
gpt4 key购买 nike

我需要写一个 GrabWindow , 所以我派生了我的类(class) 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")
}
}

但它不会在开始时显示(我知道它是透明的,我的意思是抓取窗口不会开始显示)。如果不是 GrabWindow我用 WindowApplicationWindow然后一切正常我看到一个透明的全屏窗口。
怎么了?

最佳答案

您的 GrabWindow 未显示,因为当您设置 visible 属性时,它与您使用 Window 时不同可见属性。

您的只是QWindowvisible 属性。Window 没有直接实例化 QQuickWindow,它实例化了一个私有(private)的 Qt 类 QQuickWindowImpl,它用自定义的属性覆盖了 visible一。它似乎延迟了 QWindow::setVisible 的实际调用。

因此,我不认为 QQuickWindow 应该继承自。您可以尝试在 Component.onCompleted 中执行 visible = true,但我不确定它是否能解决您的问题。

我建议您不要子类化 QQuickWindow,而只是创建一个新类型并将其传递给现有的 Window

可能的 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
}
}

关于c++ - 为什么我的窗口不显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50393986/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com