我有一个 QQuickWidget 并想使用 QQuickWindow::grabWindow() 抓取屏幕截图。但是,当我这样做时,QQuickWindow 会变成图像并且没有响应。
下面是一个最小的可重现代码: 该错误在 Qt5.13 到 Qt5.15.1 的发布模式下是可重现的(出于某种原因,Qt 在调试中抛出了一个断言)
//TestWidget.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets quickwidgets
CONFIG += c++11
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h \
windowgrabber.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
DISTFILES += \
Main.qml
RESOURCES += \
qml.qrc
//main.cpp
#include <QApplication>
#include <QQuickWidget>
#include <QQmlContext>
#include "windowgrabber.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QQuickWidget *quickWidget = new QQuickWidget;
quickWidget->rootContext()->setContextProperty("windowGrabber", new WindowGrabber(quickWidget));
quickWidget->setSource(QUrl("qrc:/Main.qml"));
quickWidget->show();
return a.exec();
}
//main.qml
import QtQuick 2.0
import QtQuick.Controls 2.0
Page {
Button {
id: button
text: "grab window"
onClicked: windowGrabber.grabWindow(button)
}
}
//WindowGrabber.h
#ifndef WINDOWGRABBER_H
#define WINDOWGRABBER_H
#include <QObject>
#include <QQuickItem>
#include <QQuickWindow>
class WindowGrabber : public QObject
{
Q_OBJECT
public:
WindowGrabber(QObject *parent = nullptr) : QObject(parent) {}
Q_INVOKABLE static void grabWindow(QQuickItem *item) {
item->window()->grabWindow();
}
};
#endif // WINDOWGRABBER_H
该代码创建了一个源设置为 Main.qml 的 QQuickWidget。我想在单击 qml 内的按钮时截屏。但是在单击按钮后,quickwidget 中的 QQuickWindow 变成了图像,按钮也变成了图像。我已经用 QWidget::createWindowContainer 进行了测试,它可以工作,但最好的解决方案是使用 QQuickWidget。任何人都知道为什么会发生这种情况?