Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Catch QFrame to QPixmap
Forum Updated to NodeBB v4.3 + New Features

Catch QFrame to QPixmap

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 5 Posters 3.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gourmet
    wrote on last edited by Gourmet
    #1

    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?

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      http://doc.qt.io/qt-5/qwidget.html#grab

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • G Offline
        G Offline
        Gourmet
        wrote on last edited by
        #3
               QPixmap pm( size() );
               pm = grab();
               QPainter p(&pm);
        
        • fails.
               QPixmap pm( size() );
               pm = grab(QRect(QPoint(0,0),size()));
               QPainter p(&pm);
        
        • fails too.
        jsulmJ 1 Reply Last reply
        0
        • G Gourmet
                 QPixmap pm( size() );
                 pm = grab();
                 QPainter p(&pm);
          
          • fails.
                 QPixmap pm( size() );
                 pm = grab(QRect(QPoint(0,0),size()));
                 QPainter p(&pm);
          
          • fails too.
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Gourmet How does it fail?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            Hi
            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);
              painter.setRenderHint(QPainter::Antialiasing);
              frame->render(&painter);
              painter.end();
              pix.save("/tmp/ramdisk/test2.png");
            
            }
            
            G 2 Replies Last reply
            0
            • mrjjM mrjj

              Hi
              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);
                painter.setRenderHint(QPainter::Antialiasing);
                frame->render(&painter);
                painter.end();
                pix.save("/tmp/ramdisk/test2.png");
              
              }
              
              G Offline
              G Offline
              Gourmet
              wrote on last edited by
              #6
              This post is deleted!
              1 Reply Last reply
              0
              • mrjjM mrjj

                Hi
                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);
                  painter.setRenderHint(QPainter::Antialiasing);
                  frame->render(&painter);
                  painter.end();
                  pix.save("/tmp/ramdisk/test2.png");
                
                }
                
                G Offline
                G Offline
                Gourmet
                wrote on last edited by Gourmet
                #7

                @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...

                mrjjM 1 Reply Last reply
                0
                • G Gourmet

                  @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...

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

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

                  G 1 Reply Last reply
                  0
                  • mrjjM mrjj

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

                    G Offline
                    G Offline
                    Gourmet
                    wrote on last edited by
                    #9

                    @mrjj
                    No.
                    Yes. I already said this before.

                    mrjjM 1 Reply Last reply
                    0
                    • G Gourmet

                      @mrjj
                      No.
                      Yes. I already said this before.

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @Gourmet
                      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.

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by SGaist
                        #11

                        Hi,

                        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.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        1
                        • mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

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

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            Gourmet
                            wrote on last edited by
                            #13

                            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.

                            mrjjM 1 Reply Last reply
                            0
                            • G Gourmet

                              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.

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

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

                              1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

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

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                G 1 Reply Last reply
                                0
                                • SGaistS SGaist

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

                                  G Offline
                                  G Offline
                                  Gourmet
                                  wrote on last edited by
                                  #16

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

                                  1 Reply Last reply
                                  0

                                  • Login

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • Users
                                  • Groups
                                  • Search
                                  • Get Qt Extensions
                                  • Unsolved