summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--examples/quick/quickwidgets/quickwidget/main.cpp30
-rw-r--r--examples/quick/quickwidgets/quickwidget/rotatingsquare.qml6
2 files changed, 28 insertions, 8 deletions
diff --git a/examples/quick/quickwidgets/quickwidget/main.cpp b/examples/quick/quickwidgets/quickwidget/main.cpp
index 65258d958e..3f3638a43f 100644
--- a/examples/quick/quickwidgets/quickwidget/main.cpp
+++ b/examples/quick/quickwidgets/quickwidget/main.cpp
@@ -39,6 +39,7 @@
****************************************************************************/
#include <QQuickWidget>
+#include <QQuickItem>
#include <QQmlError>
#include <QtWidgets>
@@ -50,8 +51,9 @@ public:
private slots:
void quickWidgetStatusChanged(QQuickWidget::Status);
void sceneGraphError(QQuickWindow::SceneGraphError error, const QString &message);
- void grabToFile();
- void renderToFile();
+ void grabFramebuffer();
+ void renderToPixmap();
+ void grabToImage();
private:
QQuickWidget *m_quickWidget;
@@ -91,8 +93,9 @@ MainWindow::MainWindow()
setCentralWidget(centralWidget);
QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
- fileMenu->addAction(tr("Grab to imFage"), this, &MainWindow::grabToFile);
- fileMenu->addAction(tr("Render to pixmap"), this, &MainWindow::renderToFile);
+ fileMenu->addAction(tr("Grab framebuffer"), this, &MainWindow::grabFramebuffer);
+ fileMenu->addAction(tr("Render to pixmap"), this, &MainWindow::renderToPixmap);
+ fileMenu->addAction(tr("Grab via grabToImage"), this, &MainWindow::grabToImage);
fileMenu->addAction(tr("Quit"), qApp, &QCoreApplication::quit);
}
@@ -113,8 +116,7 @@ void MainWindow::sceneGraphError(QQuickWindow::SceneGraphError, const QString &m
template<class T> void saveToFile(QWidget *parent, T *saveable)
{
- QString t;
- QFileDialog fd(parent, t, QString());
+ QFileDialog fd(parent);
fd.setAcceptMode(QFileDialog::AcceptSave);
fd.setDefaultSuffix("png");
fd.selectFile("test.png");
@@ -122,19 +124,31 @@ template<class T> void saveToFile(QWidget *parent, T *saveable)
saveable->save(fd.selectedFiles().first());
}
-void MainWindow::grabToFile()
+void MainWindow::grabFramebuffer()
{
QImage image = m_quickWidget->grabFramebuffer();
saveToFile(this, &image);
}
-void MainWindow::renderToFile()
+void MainWindow::renderToPixmap()
{
QPixmap pixmap(m_quickWidget->size());
m_quickWidget->render(&pixmap);
saveToFile(this, &pixmap);
}
+void MainWindow::grabToImage()
+{
+ QFileDialog fd(this);
+ fd.setAcceptMode(QFileDialog::AcceptSave);
+ fd.setDefaultSuffix("png");
+ fd.selectFile("test_grabToImage.png");
+ if (fd.exec() == QDialog::Accepted) {
+ QMetaObject::invokeMethod(m_quickWidget->rootObject(), "performLayerBasedGrab",
+ Q_ARG(QVariant, fd.selectedFiles().first()));
+ }
+}
+
int main(int argc, char **argv)
{
QApplication app(argc, argv);
diff --git a/examples/quick/quickwidgets/quickwidget/rotatingsquare.qml b/examples/quick/quickwidgets/quickwidget/rotatingsquare.qml
index 0c25eddf88..03b1114d20 100644
--- a/examples/quick/quickwidgets/quickwidget/rotatingsquare.qml
+++ b/examples/quick/quickwidgets/quickwidget/rotatingsquare.qml
@@ -57,4 +57,10 @@ Rectangle {
anchors.centerIn: parent
text: "Qt Quick running in a widget"
}
+
+ function performLayerBasedGrab(fn) {
+ root.grabToImage(function(result) {
+ result.saveToFile(fn);
+ });
+ }
}