本文介绍了如何截屏 Qt/QML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
QtQuick1 示例我无法让它在 QtQuick2 上运行
QtQuick1 exampleI can’t make it work on QtQuick2
screenCapture.hpp
screenCapture.hpp
#include <QObject>
class QString;
class QQuickView;
class screenCapture : public QObject
{
Q_OBJECT
public:
explicit screenCapture(QQuickView *parent = 0);
public slots:
void capture(QString const &path) const;
private:
QQuickView *currentView_;
};
screenCapture.cpp
screenCapture.cpp
#include <QPixmap>
#include <QQuickView>
#include <QString>
#include "screenCapture.hpp"
screenCapture::screenCapture(QQuickView *currentView) :
QObject(0), currentView_(currentView)
{
}
void screenCapture::capture(QString const &path) const
{
QPixmap::grabWidget(currentView_).save(path);
}
main.cpp
#include <QGuiApplication>
#include <QQuickPaintedItem>
#include <QQuickView>
#include <QQmlContext>
#include "screenCapture.hpp"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterType<screenCapture>("Image", 1, 0, "ScreenCapture");
qmlRegisterType<saveAbleImage>("Image", 1, 0, "SaveAbleImage");
QQuickView view;
view.setResizeMode(QQuickView::SizeRootObjectToView);
view.setSource(QStringLiteral("/Users/Qt/program/experiment_apps_and_libs/funnyCamera/qml/funnyCamera/main.qml"));
view.show();
screenCapture screenClass(&view);
view.rootContext()->setContextProperty("screenObject", &screenClass);
return app.exec();
}
main.qml
import QtQuick 2.0
Rectangle{
id : root
width : 1024
height : 768
MouseArea{
anchors.fill: root
onClicked: {
console.log("save image")
screenObject.capture("Pictures/saveTest.jpg")
}
}
}
错误信息
QPixmap::grabWidget 已弃用,请改用 QWidget::grab()QMetaObject::invokeMethod: 没有这种方法 QQuickView::grab(QRect)
QPixmap::grabWidget is deprecated, use QWidget::grab() insteadQMetaObject::invokeMethod: No such method QQuickView::grab(QRect)
推荐答案
我找到了一个解决方案,很简单,把函数capture"改成
I found a solution, pretty simple, change the function "capture" to
void screenCapture::capture(QString const &path) const
{
QImage img = currentView_->grabWindow();
img.save(path);
}
这篇关于如何截屏 Qt/QML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!