Question

This was my original question:
I just want to take a screenshot (using the Print key) of my fullscreen QtQuick 2 application. But all I get is a black or sometimes white screenshot. When the application is not started in fullscreen it works.


SOLUTION

I thought I post a really nice solution here, even though it does not solve the original problem of taking the screenshot with an external tool.

Starting with the suggestion from the accepted answer I did the following:

First I added a signal to my QML main class (in main.qml)

signal takeScreenShot()

This signal is emmited by pressing a certain button. Then I wrote a C++ / QT class autside the QML code to handle this signal:

class QMLSupplement : public QObject
{
    Q_OBJECT
    public:
        QQuickView* view;

    public:
        QMLSupplement(QObject* parent = 0);

    public slots:
        void takeScreenShot();
};

The reference to QQuickView is used to take the screenshot.

void QMLSupplement::takeScreenShot()
{
    QString file;
    file = QDateTime::currentDateTime().toString("yyyy-MM-dd_hhmmss");
    file += ".png";

    qDebug() << "taking screenshot, saving here:" << file;

    view->grabWindow().save(file);
}

Finally I connect the signal and the slot in main.cpp:

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQuickView view;
    view.setSource(QUrl::fromLocalFile("./qml/main.qml"));
    view.setResizeMode(QQuickView::SizeRootObjectToView);

    QObject* rootObject = (QObject*) view.rootObject();

    QMLSupplement supplement;
    supplement.view = &view;
    QObject::connect(rootObject, SIGNAL(takeScreenShot()),
                     &supplement, SLOT(takeScreenShot()));

    view.show();
//  view.showFullScreen();

    return app.exec();
}
Was it helpful?

Solution

That's a limitation of the platform where you're running. If you care about this working, you need to implement the functionality yourself. Qt provides you with enough to get the contents of the Qt Quick 2 window and post it to the clipboard as an image.

In your print key handler, if you detect that the window is full-screen, you need to pass the QQuickWindow instance to a helper function:

void grabAndCopy(QQuickWindow * window) {
  QApplication::clipboard()->setImage(window->grabWindow());
}

...
if (window->windowState() == Qt::WindowFullScreen) grabAndCopy(window);    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top