How to grab current qt application Window
-
Hello guys I am trying to screenshot my own qt application window.
I tried this code but I know there are some better way.void Test::capture(const QString &path) const { QPixmap originalPixmap; QScreen *screen = QGuiApplication::primaryScreen(); int y = 100; int w = 1600; int h = 450; originalPixmap = screen->grabWindow(0,x,y,w,h); originalPixmap.save(path); }
is there another way to screenshot or grab current qt application?
-
@jhovarie Use http://doc.qt.io/qt-5/qwidget.html#winId to get WId and don't pass x, y, w, h to grab the whole window.
-
void Test::capture(const QString &path) const { QWidget* parentWid = this; // Current widget while(parentWid->parent()) parentWid = parentWid->parent(); // climb up utill you reach the widget that's the parent of everything parentWid->grab().save(path); // save it }
-
@VRonin
Your method relies on starting from a widget. To find the app's main window I use this standalone approach:def findMainWindow() -> typing.Union[QMainWindow, None]: # Global function to find the (open) QMainWindow in application app = QApplication.instance() for widget in app.topLevelWidgets(): if isinstance(widget, QMainWindow): return widget return None
As I respect your expertise, if you have any comment on this it would be welcome?
-
As @JonB suggested, (i'm just translating in C++ are removing the
QMainWindow
assumption), ifTest
is not a widget inside the area you want to grabconst QWidgetList topWidg = QApplication::topLevelWidgets(); for(int i=0;i<topWidg.size();++i){ widget->grab().save("GrabImage" + QString::number(i+1) + ".png"); }
-
@VRonin said in How to grab current qt application Window:
parentWid
Your code doesn't work on Qt 5.15 (but thanks for your job). I fix it.
QWidget* parentWid = this; // Current widget while(parentWid->parent()) parentWid = parentWid->parentWidget(); // climb up utill you reach the widget that's the parent of everything parentWid->grab().save("path.png"); // save it