如何在Qt Quick Image中显示QPixmap?

时间:2016-01-04 15:03:42

标签: c++ image qt qml qpixmap

我正在使用QML和C ++编写一个小型Qt Quick应用程序来截取屏幕截图。 grabWindow()函数返回一个QPixmap。我想在Qt Quick Image中显示此屏幕截图,但Image仅接受URL。如何显示我的QPixmap而不必先将其保存到路径?

我的screenshot.cpp:

#include <QtQuick>
#include "screenshot.h"

Screenshot::Screenshot() {}

QPixmap *Screenshot::grabScreen() {
  QScreen *screen = QGuiApplication::primaryScreen();
  if(screen)
    *originalPixmap = screen->grabWindow(0);
  return originalPixmap;
}

screenshot.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

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
    }
}

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

编辑: 当我现在运行它时,我得到了

  

qrc:/main.qml:126:错误:未知方法返回类型:QPixmap *

提前致谢!

0 个答案:

没有答案