Results 1 to 9 of 9

Thread: Render windget to QPixmap

  1. #1
    Join Date
    Nov 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Render windget to QPixmap

    I have application whith whidgets on mainform. I need to render one of widgets (qwtPlot) to QPixmap using QGraphicsScene. I use such code for that:
    Qt Code:
    1. QPixmap pixmap(300, 300);
    2. QPainter painter(&pixmap);
    3. painter.fillRect(0, 0, 300, 300, Qt::black);
    4. QGraphicsProxyWidget *widget = scene->addWidget(qwtPlot);
    5. qwtPlot->repaint();
    6. qwtPlot->show();
    7. scene->render(&painter, QRectF(), QRect(), Qt::IgnoreAspectRatio);
    8. if (pixmap.save(QApplication::applicationDirPath() + "/image.png", "PNG"))
    9. {
    10. box.setText("Saved");
    11. box.exec();
    12. }
    13. delete widget;
    14. delete scene;
    To copy to clipboard, switch view to plain text mode 

    But I haven't any whidgets in "image.png" file - all file is black. Why my widget don't show in the file?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Render windget to QPixmap

    Why do you want to create a scene and add a widget to it only to render it? Isn't that pure overhead? See QPixmap::grabWidget().

  3. #3
    Join Date
    Nov 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Render windget to QPixmap

    I need to render my widget to QPixmap whith given size and whith same quality as on the screen. I think QGraphicsScene help me whith it.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Render windget to QPixmap

    Quote Originally Posted by giker View Post
    I need to render my widget to QPixmap whith given size and whith same quality as on the screen.
    And in contrast QPixmap::grabWidget() (and some other functions of QPixmap) does what?

  5. #5
    Join Date
    Nov 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Render windget to QPixmap

    Result of grabWidget() depends on current state of widget. For example, if user change mainwindow's of application sizes of child widgets will be changed. I want to get view of widget that independens on parent widget size.

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Render windget to QPixmap

    Ehm, yes, but you are creating a new instance before you add it to the scene via addWidget(). Instead of that simply pass it to grabWidget().

  7. #7
    Join Date
    Nov 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Render windget to QPixmap

    I don't uderstand you. I want know: can I render widgets that already shown at application's mainform or not? Because I can render widgets that created in code but never show at the screen:

    Qt Code:
    1. QPixmap pixmap(300, 300);
    2. QPainter painter(&pixmap);
    3. painter.fillRect(0, 0, 300, 300, Qt::black);
    4. QwtPlot *qwtPlot = new QwtPlot();//this widget will show in file
    5. QGraphicsProxyWidget *widget = scene->addWidget(qwtPlot);
    6. scene->render(&painter, QRectF(), QRect(), Qt::IgnoreAspectRatio);
    7. if (pixmap.save(QApplication::applicationDirPath() + "/image.png", "PNG"))
    8. {
    9. box.setText("Saved");
    10. box.exec();
    11. }
    12. delete widget;
    13. delete scene;
    14. delete qwtPlot;
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Render windget to QPixmap

    Quote Originally Posted by giker View Post
    I want know: can I render widgets that already shown at application's mainform or not?
    Yes, with grabWidget(). But only in the size they are actually shown.
    Because I can render widgets that created in code but never show at the screen:

    Qt Code:
    1. QPixmap pixmap(300, 300);
    2. QPainter painter(&pixmap);
    3. painter.fillRect(0, 0, 300, 300, Qt::black);
    4. QwtPlot *qwtPlot = new QwtPlot();//this widget will show in file
    5. QGraphicsProxyWidget *widget = scene->addWidget(qwtPlot);
    6. scene->render(&painter, QRectF(), QRect(), Qt::IgnoreAspectRatio);
    7. if (pixmap.save(QApplication::applicationDirPath() + "/image.png", "PNG"))
    8. {
    9. box.setText("Saved");
    10. box.exec();
    11. }
    12. delete widget;
    13. delete scene;
    14. delete qwtPlot;
    To copy to clipboard, switch view to plain text mode 
    Yes and this is as often enough told nonsense. Use also grabWidget() for that.

  9. #9
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,313
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Render windget to QPixmap

    Use QwtPlot::print - see the bode example how to use it. Also consider to use a vector graphics format like PDF or SVG.

    Note that Qwt 6.x has a floating point based render engine, what gives you perfectly scalable outputs for vector graphics. Also the API for rendering graphic formats has been improved and should be more comfortable: again see the bode example.

    Uwe

    PS: For Qwt related questions better use the Qwt subforum. I miss most of the questions not posted there.

Similar Threads

  1. How to render a QGraphicsItem to a QPixmap?
    By theodolite in forum Qt Programming
    Replies: 0
    Last Post: 30th July 2010, 11:57
  2. Replies: 4
    Last Post: 28th August 2008, 13:13
  3. Replies: 1
    Last Post: 21st August 2008, 07:44
  4. QPixmap::grabWidget and QWidget::render loose alpha
    By EricF in forum Qt Programming
    Replies: 5
    Last Post: 11th March 2008, 08:42
  5. Replies: 5
    Last Post: 9th April 2007, 14:26

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.