I am trying to save a snapshow of a QMainWindow
with widgets and title bar.
This is how I tried:
from time import sleep
from PySide6.QtCore import QRect
from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton, QStyle
application = QApplication()
extra_top = application.style().pixelMetric(QStyle.PixelMetric.PM_TitleBarHeight)
button = QPushButton("Hello, world!")
w = QMainWindow()
w.setCentralWidget(button)
w.show()
# Try a bunch of things
w.update()
w.repaint()
w.show()
sleep(0.1)
w.update()
w.repaint()
w.show()
sleep(0.1)
# This snapshot shows the button, but not the title bar
w.grab(
QRect(
0,
-extra_top,
w.width(),
w.height() + extra_top,
)
).save("grabWidget.png")
# This snapshot shows the title bar, but not the button
w.screen().grabWindow(
w.winId(),
x=0,
y=-extra_top,
w=w.width(),
h=w.height() + extra_top,
).save("grabWindow.png")
First snapshot:
Second snapshot:
How do I get the best of both?