英文:
How to render QDialog to QImage with title bar?
问题
以下是代码部分的翻译:
QImage img(_dialog->size(), QImage::Format_RGBA8888);
{
QPainter painter(&img);
_dialog->render(&painter);
}
img.save("xxx.png");
我尝试将一个 QDialog 渲染到 QImage,代码非常简单。
我的对话框有一个标题栏,但在保存的图像中看不到它,我应该怎么做才能渲染标题栏呢?
我还尝试了 grab()
,标题栏也缺失了:
_dialog->grab().save("xxx.png");
英文:
I'm trying to render a QDialog to a QImage, the code is very simple:
QImage img(_dialog->size(), QImage::Format_RGBA8888);
{
QPainter painter(&img);
_dialog->render(&painter);
}
img.save("xxx.png");
My dialog has a title bar, but I can't see it in the saved image, what should I do to render the title bar?
I also tried grab()
, titile bar is missing too:
_dialog->grab().save("xxx.png");
答案1
得分: 1
如果对话框实际可见在屏幕上,那么您可以使用 QScreen::grabWindow()
,请参考 https://doc.qt.io/qt-6/qscreen.html#grabWindow。在这个函数中,您可以指定要抓取的矩形坐标,因此请使用从 QWidget::frameGeometry()
获取的矩形,该矩形包括窗口标题栏和边框。请参考 https://doc.qt.io/qt-6/qwidget.html#frameGeometry-prop。
如果对话框在屏幕上不可见,那么我认为您运气不太好,Qt 可能无法帮助您。
英文:
If the dialog is actaully visible on screen, then you can use QScreen::grabWindow()
, see https://doc.qt.io/qt-6/qscreen.html#grabWindow In this function you can specify the rectangle coordinates you want to grab, so use the rectangle which you obtain from QWidget::frameGeometry()
which includes the window title bar and frame. See https://doc.qt.io/qt-6/qwidget.html#frameGeometry-prop
If the dialog is not visible on screen then I think you are out of luck, Qt probably cannot help you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论