如何在Qt快速图像中显示QPixmap? 我正在使用QML和C++进行一个小QT快速应用程序,用于截屏。函数的作用是:返回一个QPixmap。我想在Qt Quick图像中显示此屏幕截图,但该图像只接受URL。如何显示QPixmap而不必先将其保存到路径
My screenshot.cpp:如何在Qt快速图像中显示QPixmap? 我正在使用QML和C++进行一个小QT快速应用程序,用于截屏。函数的作用是:返回一个QPixmap。我想在Qt Quick图像中显示此屏幕截图,但该图像只接受URL。如何显示QPixmap而不必先将其保存到路径,c++,image,qt,qml,qpixmap,C++,Image,Qt,Qml,Qpixmap,My screenshot.cpp: #include <QtQuick> #include "screenshot.h" Screenshot::Screenshot() {} QPixmap *Screenshot::grabScreen() { QScreen *screen = QGuiApplication::primaryScreen(); if(screen) *originalPixmap = screen->grabWindow(0);
#include <QtQuick>
#include "screenshot.h"
Screenshot::Screenshot() {}
QPixmap *Screenshot::grabScreen() {
QScreen *screen = QGuiApplication::primaryScreen();
if(screen)
*originalPixmap = screen->grabWindow(0);
return originalPixmap;
}
#包括
#包括“screenshot.h”
屏幕截图::屏幕截图(){}
QPixmap*屏幕截图::grabScreen(){
QScreen*screen=QGuiApplication::primaryScreen();
如果(屏幕)
*originalPixmap=屏幕->抓取窗口(0);
返回原始像素图;
}
截图.h:
#ifndef SCREENSHOT_H
#define SCREENSHOT_H
#include <QObject>
class QString;
class QQuickView;
class Screenshot : public QObject
{
Q_OBJECT
public:
Screenshot();
Q_INVOKABLE QPixmap *grabScreen();
public slots:
private:
QPixmap *originalPixmap;
};
#endif // SCREENSHOT_H
\ifndef屏幕截图
#定义屏幕截图
#包括
类QString;
类QQuickView;
类截图:公共QObject
{
Q_对象
公众:
截图();
Q_可调用QPixmap*grabScreen();
公众时段:
私人:
QPixmap*原始Pixmap;
};
#endif//屏幕截图
main.qml:
import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.1
import QtQuick.Controls.Styles 1.4
import com.pixmate.screenshot 1.0
ApplicationWindow {
visible: true
width: 420
height: 380
menuBar: MenuBar {
Menu {
title: "Screen"
MenuItem {
text: "New Screenshot"
shortcut: "Ctrl+Shift+3"
}
MenuItem {
text: "Save Screenshot"
shortcut: "Ctrl+Shift+4"
}
}
Menu {
title: "Edit"
MenuItem { text: "Cut" }
MenuItem { text: "Copy" }
MenuItem { text: "Paste" }
}
}
Screenshot {
id: screenShot
}
Image {
id: screenshotImage
visible: true
anchors.horizontalCenter: parent.horizontalCenter
y: 10
width: 240
height: 160
source: "<insert URL here>"
}
GroupBox {
title: "Options"
y: 180
x: 10
width: parent.width-20
Column {
spacing: 10
Label {
text: "Screenshot Delay"
}
SpinBox {
maximumValue: 120
value: 3
suffix: " s"
width: 80
onValueChanged: {
if(value == 0) {
hideThisWindowCheckbox.enabled = false;
hideThisWindowCheckbox.checked = false;
} else {
hideThisWindowCheckbox.enabled = true;
}
}
}
CheckBox {
id: hideThisWindowCheckbox
text: "Hide this window"
checked: true
/*style : CheckBoxStyle {
padding.top: 10
}*/
}
}
Column {
x: 185
spacing: 10
RadioButton {
id: captureScreenRadioButton
text: "Capture entire screen"
checked: true
onClicked: {
captureActiveWindowRadioButton.checked = false
captureScreenSelectionRadioButton.checked = false
}
}
RadioButton {
id: captureActiveWindowRadioButton
text: "Capture active window"
enabled: false // disabled for now
onClicked: {
captureScreenRadioButton.checked = false
captureScreenSelectionRadioButton.checked = false
}
}
RadioButton {
id: captureScreenSelectionRadioButton
text: "Capture selected area"
enabled: false // disabled for now
onClicked: {
captureScreenRadioButton.checked = false
captureActiveWindowRadioButton.checked = false
}
}
}
}
Button {
id: newScreenshotButton
text: "New Screenshot"
y: 320
x: 10
onClicked: {
screenshotImage.data = screenShot.grabScreen();
}
}
Button {
id: saveScreenshotButton
text: "Save Screenshot"
y: 320
x: 165
}
Button {
id: quitButton
text: "Quit"
y: 320
x: 320
}
}
导入QtQuick 2.5
导入QtQuick.Window 2.2
导入QtQuick.Controls 1.4
导入QtQuick.Layouts 1.1
导入QtQuick.Controls.Styles 1.4
导入com.pixmate.screenshot 1.0
应用程序窗口{
可见:正确
宽度:420
身高:380
菜单栏:菜单栏{
菜单{
标题:“屏幕”
梅努伊特姆{
文字:“新截图”
快捷方式:“Ctrl+Shift+3”
}
梅努伊特姆{
文本:“保存屏幕截图”
快捷方式:“Ctrl+Shift+4”
}
}
菜单{
标题:“编辑”
MenuItem{text:“Cut”}
MenuItem{text:“复制”}
菜单项{text:“粘贴”}
}
}
截图{
id:截图
}
形象{
id:截屏图像
可见:正确
anchors.horizontalCenter:父级.horizontalCenter
y:10
宽度:240
身高:160
资料来源:“”
}
分组框{
标题:“选项”
y:180
x:10
宽度:parent.width-20
纵队{
间距:10
标签{
文本:“屏幕截图延迟”
}
旋转箱{
最大值:120
价值:3
后缀:“s”
宽度:80
onValueChanged:{
如果(值==0){
hideThisWindowCheckbox.enabled=false;
hideThisWindowCheckbox.checked=false;
}否则{
hideThisWindowCheckbox.enabled=true;
}
}
}
复选框{
id:hideThisWindowCheckbox
文本:“隐藏此窗口”
核对:正确
/*样式:复选框样式{
排名:10
}*/
}
}
纵队{
x:185
间距:10
单选按钮{
id:CaptureScreen单选按钮
文本:“捕获整个屏幕”
核对:正确
再次点击:{
captureActiveWindowRadioButton.checked=false
captureScreenSelectionRadioButton.checked=false
}
}
单选按钮{
id:captureActiveWindowRadioButton
文本:“捕获活动窗口”
enabled:false//暂时禁用
再次点击:{
captureScreenRadioButton.checked=false
captureScreenSelectionRadioButton.checked=false
}
}
单选按钮{
id:CaptureScreenSelectionRadio按钮
文本:“捕获选定区域”
enabled:false//暂时禁用
再次点击:{
captureScreenRadioButton.checked=false
captureActiveWindowRadioButton.checked=false
}
}
}
}
钮扣{
id:newScreenshotButton
文字:“新截图”
y:320
x:10
再次点击:{
screenshotImage.data=screenShot.grabScreen();
}
}
钮扣{
id:SaveScreenShot按钮
文本:“保存屏幕截图”
y:320
x:165
}
钮扣{
id:退出按钮
文本:“退出”
y:320
x:320
}
}
main.cpp
#include <QGuiApplication>
#include <QQmlContext>
#include <QQuickView>
#include <QQmlApplicationEngine>
#include "src/screenshot.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterType<Screenshot>("com.pixmate.screenshot", 1, 0, "Screenshot");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
#包括
#包括
#包括
#包括
#包括“src/screenshot.h”
int main(int argc,char*argv[])
{
QGUI应用程序应用程序(argc、argv);
qmlRegisterType(“com.pixmate.screenshot”,1,0,“screenshot”);
qqmlaplicationengine;
engine.load(QUrl(QStringLiteral(“qrc:/main.qml”));
返回app.exec();
}
编辑:
当我现在运行它时,我得到
qrc:/main.qml:126:错误:未知方法返回类型:QPixmap*
提前谢谢 看一看。应该有助于使用它。@BaCaRoZzo感谢您的回复!我看了你的链接,似乎不明白。我可以在requestPixmap()函数中使用grabWindow()并返回它吗?@BaCaRoZzo我找到了它,现在它可以工作了。我刚刚从requestPixmap中调用了grabWindow()函数。非常感谢:)非常好。:)请为您的问题提供一个精确的答案,如果西蒙的答案对您有帮助,请记住对其进行投票。谢谢。@ddriver,我应该把你的答案也贴出来。它的信息量很大,并且有可用的代码。谢谢分享。