获取窗口:场景图已经在使用中

I tried the code shown here: How to take ScreenShot Qt/QML

  • 我尝试了这里显示的代码:如何截屏Qt/QML

On execution I am getting the error written in the title.

  • 在执行过程中,我将在标题中写入错误。

My main.cpp is:

  • 我的主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();
}

I used this capture function:

  • 我使用了这个捕获函数:
void screenCapture::capture(QString const &path) const
{
    QImage img = currentView_->grabWindow();
    img.save(path);
}

and added the following in the constructor:

  • 并在构造函数中添加以下内容:
currentView_ = new QQuickView;

My main.qml :

  • 我的主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")
        }
    }
}

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();
}

Now, in QML side, we can directly use

  • 现在,在QML方面,我们可以直接使用

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

  • 截图。捕获(“/ home / * * * * * /桌面/ l.png”)

anywhere we want. It works.

  • 任何我们想要的。它的工作原理。

转自:获取窗口:场景图已经在使用中 - QQuickWindow::grabWindow: scene graph already in use - 开发者知识库 (itdaan.com)

c++ - QQuickWindow::grabWindow: scene graph already in use - Stack Overflow 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用Qt框架来实现这个功能。首先,您需要在主窗口添加一个场景和视,可以使用QGraphicsScene和QGraphicsView类来实现。然后,您可以使用QGraphicsItem类来创建自定义形项,并将它们添加到场景。 为了实现获取鼠标点击处的所有形项,您可以使用QGraphicsScene的items()函数来获取场景所有的形项,然后使用QGraphicsItem的contains()函数来判断鼠标点击处是否在某个形项内部。如果是,则将该形项添加到一个列表。 以下是一个示例代码片段,演示如何实现这个功能: ```cpp // 创建场景和视 QGraphicsScene* scene = new QGraphicsScene(); QGraphicsView* view = new QGraphicsView(scene); // 创建自定义形项 MyGraphicsItem* item1 = new MyGraphicsItem(); MyGraphicsItem* item2 = new MyGraphicsItem(); MyGraphicsItem* item3 = new MyGraphicsItem(); // 将形项添加到场景 scene->addItem(item1); scene->addItem(item2); scene->addItem(item3); // 处理鼠标点击事件 void MyGraphicsView::mousePressEvent(QMouseEvent* event) { // 获取鼠标点击处的坐标 QPointF pos = mapToScene(event->pos()); // 获取场景所有的形项 QList<QGraphicsItem*> items = scene()->items(); // 遍历所有的形项,判断是否包含鼠标点击处 QList<QGraphicsItem*> clickedItems; foreach (QGraphicsItem* item, items) { if (item->contains(pos)) { clickedItems.append(item); } } // 处理点击的形项 // ... } ``` 请注意,这只是一个示例代码片段,您需要根据您的具体需求进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值