Catch QFrame to QPixmap

  • Qt 5.6.1, Android.
    I need catch content of QFrame to QPixmap. And then I will draw on this QPixmap by QPainter. I do this in class deriving from QFrame. Piece of code from paintEvent() of this class:

           QPixmap pm( size() );
    m:    pm.fill(Qt::black); // color noise appears without this
           render( &pm );
           QPainter p(&pm);

    Nothing. After render pm contains black field. Then I can draw on it, this works. The noise appearing in m: line tells me - render(&pm) does nothing. It does not change pm content. I tried use detailed call of render like this:

    render( &pm, QPoint(0,0), QRegion(0,0,width(),height()) );

    With this program fails.
    What I am doing wrong? How can I catch content of QFrame to QPixmap?

  •        QPixmap pm( size() );
           pm = grab();
           QPainter p(&pm);
    • fails.
           QPixmap pm( size() );
           pm = grab(QRect(QPoint(0,0),size()));
           QPainter p(&pm);
    • fails too.

  • Moderators

    @Gourmet How does it fail?

  • Qt Champions 2017

    You assign painter to pixmap AFTER you grab.
    Also where do you run this code ?
    Should not be in constructor as it might not have right size.

    void ManualControlForm::screendump() {
      QPixmap pix; // might need to set size
      QPainter painter(&pix);

  • This post is deleted!

  • @mrjj
    If render to QPainter then waterfall of messages

    (null):0 ((null)): QWidget::repaint: Recursive repaint detected

    appears in app window and then app fails.

    I run this code inside paintEvent() as I said before. Looks like render to QPainter calls paintEvent() - therefore recursion appears. But I need get content of frame inside paintEvent()... I have to think more...

  • Qt Champions 2017

    Can you show the whole function ?
    Do you ask the QFrame to render() inside its own paintEvent ??

  • @mrjj
    Yes. I already said this before.

  • Qt Champions 2017

    Well you are still not showing full code.
    I done this many times and normally have zero issue with it.
    (for screen shot features)
    So you are clearly doing something odd, wrong or unexpected.
    But you only said
    "piece of code from paintEvent() of this class"
    and show nothing more relevant information.

  • Lifetime Qt Champion


    That's the main problem, you're asking the widget to paint itself when it's already painting itself, hence the error you get.

    Do that somewhere else but not in the paintEvent of your QFrame.

  • Qt Champions 2017

    Ah, i missed the YES mean he called inside QFrame paint. :)
    That will lead to recursive behavior.
    Thanks @SGaist

  • Yes, gotcha... just made QPixmap pm class-wide field and removed pm = grab(QRect(QPoint(0,0),size())); to another method before update() - and all is working. To be on safe side just set pm = pm.scaled(size()); in init. Don't know is it necessary or not.

  • Qt Champions 2017

    It will grab it in its exact size.
    So should not be needed.

  • Lifetime Qt Champion

    It's not since in the constructor the size of the widget is still unknown.

  • @SGaist
    Not in constructor - Instead of this my widget has another init function called by plugin loader.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.