Qt5中,取消了 QPainter::grabWidget ,新增了Widget::grab.下面是Qt手册中所述:
[static] QPixmap QPixmap::grabWidget(QObject *widget, const QRect &rectangle)
Use QWidget::grab() instead.
QPixmap QWidget::grab(const QRect &rectangle = QRect( QPoint( 0, 0 ), QSize( -1, -1 ) ))
Renders the widget into a pixmap restricted by the given rectangle. If the widget has any children, then they are also painted in the appropriate positions.
If a rectangle with an invalid size is specified (the default), the entire widget is painted.
This function was introduced in Qt 5.0.
所以我们可以直接通过控件中自带的grab来获取指定区域的pixmap了,如:
pixmap=textEdit->grab();
下面附上完整的抓取TextEdit中文本生成pdf的例子:
#ifndef MAINWINDOW_H
#define MAINWIN