gpt4 book ai didi

c++ - QQuickWindow::grabWindow:场景图已经在使用中

转载 作者:太空宇宙 更新时间:2023-11-04 13:26:37 25 4
gpt4 key购买 nike

我尝试了这里显示的代码:How to take ScreenShot Qt/QML

执行时我收到标题中写的错误。

我的main.cpp是:

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickWindow>
#include <QImage>
#include <QDebug>
#include "screenshot.h"
#include <QQuickView>
#include <QQmlContext>

int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);

const char* drigUi = "DrigUI";
qmlRegisterType <screenCapture> (drigUi, 1, 0, "ScreenShot");

QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

return app.exec();
}

我使用了这个capture 函数:

void screenCapture::capture(QString const &path) const
{
QImage img = currentView_->grabWindow();
img.save(path);
}

并在构造函数中添加了以下内容:

currentView_ = new QQuickView;

我的main.qml:

import QtQuick 2.4
import QtQuick.Window 2.2

import DrigUI 1.0

Window
{
visible: true
height: 370
width: 370

ScreenShot { id: screenShot }

Rectangle
{
id: ll
height: 30
width: 50
x: 180; y: 0; color: "red"
MouseArea
{
anchors.fill: parent
onClicked: screenShot.capture ("l.png")
}
}
}

这个错误是什么意思?解决方法是什么?我还可以在这里提供什么信息?

最佳答案

我在 QtCenter.org too, and got the enlightenment from there. 上发布了这个问题

My I ask why you want to create a screenshot of an empty QQuickView? Wouldn't you want to take the screenshot of the Window you create in QML?

我意识到我没有在 main.cpp 中的任何地方使用 QQuickView。所以这意味着我使用的 QQuickView 是空的。

我的程序的另一部分使用 QQuickWindow 而不是 QQuickView,所以我用 QQuickWindow 替换了 QQuickView 作为如下:

ScreenCapture.h

#ifndef SCREENSHOT_H
#define SCREENSHOT_H

#include <QObject>

class QString;
class QQuickWindow;

class screenCapture : public QObject
{
Q_OBJECT
public:
explicit screenCapture (QQuickWindow *parent = 0);

public slots:
void capture (QString const &path) const;

private:
QQuickWindow *currentWindow;
};

#endif // SCREENSHOT_H

ScreenCapture.cpp

#include <QPixmap>
#include <QQuickView>
#include <QDebug>
#include "screenshot.h"

screenCapture::screenCapture(QQuickWindow *currentWindow) :
QObject(0), currentWindow (currentWindow)
{
}

void screenCapture::capture (QString const &path) const
{
QImage p = currentWindow->grabWindow ();
bool kk = p.save (path);
qDebug () << kk;
}

main.cpp

int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);

QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

QObject *topLevel = engine.rootObjects().value(0);
QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);

screenCapture launcher (window);

engine.rootContext()->setContextProperty("ScreenShot", &launcher);

window->show();

return app.exec();
}

现在,在QML端,我们可以直接使用

ScreenShot.capture ("/home/*****/Desktop/l.png")

我们想要的任何地方。它有效。

关于c++ - QQuickWindow::grabWindow:场景图已经在使用中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33165733/

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