如何将QDialog呈现为带有标题栏的QImage?

huangapple go评论59阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年3月9日 16:34:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75682104.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定