为什么使用 QQuickWindow::grabWindow() 会导致 window 变成图像?
Why using QQuickWindow::grabWindow() causes a window to turn into an image?
我有一个 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
代码创建了一个 QQuickWidget,源设置为 Main.qml。单击 qml 中的按钮时,我想截屏。但是单击按钮后,快速小部件内的 QQuickWindow 变成图像,按钮也变成图像。我已经用 QWidget::createWindowContainer 进行了测试并且它有效,但最好的解决方案是使用 QQuickWidget。有人知道为什么会发生这种情况吗?
QQuickWidget 使用 non-visible QQuickWindow 来呈现项目,并且该呈现在 QWidget 中再次呈现,因此在尝试保存图像时它会干扰,导致延迟,正如我已经在 .
一个可能的解决方案是直接从QQuickWidget中抓取:
#ifndef WINDOWGRABBER_H
#define WINDOWGRABBER_H
#include <QObject>
#include <QQuickWidget>
class WindowGrabber : public QObject
{
Q_OBJECT
public:
WindowGrabber(QQuickWidget *widget): m_widget(widget) {}
Q_INVOKABLE void grabWindow() {
if(m_widget){
QPixmap img = m_widget->grab();
qDebug() << img;
}
}
private:
QPointer<QQuickWidget> m_widget;
};
#endif // WINDOWGRABBER_H
#include <QApplication>
#include <QQuickWidget>
#include <QQmlContext>
#include "windowgrabber.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QQuickWidget quickWidget;
WindowGrabber grabber(&quickWidget);
quickWidget.rootContext()->setContextProperty("windowGrabber", &grabber);
quickWidget.setSource(QUrl("qrc:/main.qml"));
quickWidget.show();
return a.exec();
}
Button {
id: button
text: "grab window"
onClicked: windowGrabber.grabWindow()
}
我有一个 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
代码创建了一个 QQuickWidget,源设置为 Main.qml。单击 qml 中的按钮时,我想截屏。但是单击按钮后,快速小部件内的 QQuickWindow 变成图像,按钮也变成图像。我已经用 QWidget::createWindowContainer 进行了测试并且它有效,但最好的解决方案是使用 QQuickWidget。有人知道为什么会发生这种情况吗?
QQuickWidget 使用 non-visible QQuickWindow 来呈现项目,并且该呈现在 QWidget 中再次呈现,因此在尝试保存图像时它会干扰,导致延迟,正如我已经在
一个可能的解决方案是直接从QQuickWidget中抓取:
#ifndef WINDOWGRABBER_H
#define WINDOWGRABBER_H
#include <QObject>
#include <QQuickWidget>
class WindowGrabber : public QObject
{
Q_OBJECT
public:
WindowGrabber(QQuickWidget *widget): m_widget(widget) {}
Q_INVOKABLE void grabWindow() {
if(m_widget){
QPixmap img = m_widget->grab();
qDebug() << img;
}
}
private:
QPointer<QQuickWidget> m_widget;
};
#endif // WINDOWGRABBER_H
#include <QApplication>
#include <QQuickWidget>
#include <QQmlContext>
#include "windowgrabber.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QQuickWidget quickWidget;
WindowGrabber grabber(&quickWidget);
quickWidget.rootContext()->setContextProperty("windowGrabber", &grabber);
quickWidget.setSource(QUrl("qrc:/main.qml"));
quickWidget.show();
return a.exec();
}
Button {
id: button
text: "grab window"
onClicked: windowGrabber.grabWindow()
}