[Qt-interest] Render hidden widget to QPixmap

Girish Ramakrishnan girish at forwardbias.in
Tue Feb 9 08:58:22 CET 2010


Liebe Markus (RtP2/TEF72) wrote:
> Hi Girish,
> 
>> QWidget::render() is supposed to work on hidden widgets. If it
>> doesn't please report a bug in the Qt tracker.
> 
> Well it seems to me as if it does not work. I'll test again with Qt
> 4.6.1 because at the moment I am using 4.6.0. Depending on the
> results I will report a bug in the Qt tracker. As Alex proposed I had
> a try with the Qpixmap::grabWidget() function, but that did not work,
> too. Even though it was better than Qwidget::render() because It got
> half the widget rendered (as from left to right of the widget) and
> kept the rest plain gray as well.
> 
>> As a work around, maybe you can display your widget somewhere
>> outside the screen and then call render :-) ?
> 
> That is exactly what I am doing now. It works, but seems kindof hacky
> to me ;-)
> 

I just quickly checked, the program below works for me on Linux/Qt 4.6.1.

#include <QtGui>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QWidget *window = new QWidget;
    QVBoxLayout *layout = new QVBoxLayout(window);
    for (int i = 0; i < 10; i++) {
        layout->addWidget(new QPushButton(QString("button %1").arg(i)));
    }
    window->adjustSize();

    QPixmap pixmap(window->size());
    window->render(&pixmap);

    pixmap.save("screenshot.png");

    return 1;
}

Girish



More information about the Qt-interest-old mailing list