I want to grab screen of my QML application. But my root QML object is ApplicationWindow
, so I can\'t use QQuickView
to show it. Instead I have to use
You can use the QObject *obj, QEvent *ev to take screenshot as well.
Main.cpp
QObject *objekt = engine.rootObjects().first();
classObj->root_object = objekt;
app.installEventFilter(classObj);
classObj.h
QObject *root_object = nullptr;
bool eventFilter(QObject *obj, QEvent *ev);
classObj.cpp
bool classObj::eventFilter(QObject *obj, QEvent *ev)
{
QString dateInterval = QString::number(QDateTime::currentSecsSinceEpoch());
QDir dir("Screenshots");
if(!dir.exists())
dir.mkpath(".");
if(ev->type() == QEvent::MouseButtonPress) {
QQuickWindow *window = qobject_cast(root_object);
window->grabWindow().save("Screenshots/Screenshot-"+dateInterval+".png");
}
return QObject::eventFilter(obj, ev);
}
this will take screenshot whenever some event happenes.