Results 1 to 5 of 5

Thread: How to save widgets in layout to single file

  1. #1
    Join Date
    Jun 2012
    Posts
    219
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    28
    Thanked 3 Times in 3 Posts

    Default Re: How to save widgets in layout to single file

    I have a window that contains a horizontal layout that consists of 1-6 widgets. Each widget is a simple label with a pixmap.

    Basically, the window consists of from 1-6 images that scale so they fit horizontally in a fixed window size.

    I'd like to save a "composite image" to a file that's what's seen inside the window's horizontal layout.

    I've seen examples on how to do this in Qt 5:

    Qt Code:
    1. myWidget->grab().save("image.png");
    To copy to clipboard, switch view to plain text mode 

    but I need to do it in Qt4.8.

    Is there a similar idiom to do it in Qt4?

    Thanks,

    Dave Thomas


    Added after 17 minutes:


    Actually, the layout always contains six widgets. But, only 1-6 are set to be visible.

    That's how the scaling is done so they fit the window size.
    Last edited by davethomaspilot; 24th September 2014 at 12:35.

  2. #2
    Join Date
    Apr 2012
    Location
    India.
    Posts
    88
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanked 8 Times in 8 Posts

    Default Re: How to save widgets in layout to single file

    read documentation about QPixmap::grabWidget(...) function, it will return QPixmap and QPixmap has function to save it.

  3. The following user says thank you to Rajesh.Rathod for this useful post:

    davethomaspilot (24th September 2014)

  4. #3
    Join Date
    Jun 2012
    Posts
    219
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    28
    Thanked 3 Times in 3 Posts

    Default Re: How to save widgets in layout to single file

    There are N pixmaps, one associated with each widget in the layout.

    I'm looking for a combined pixmap of all the widgets contained in the layout.

    Of course I can write code to use QPainter to build it from each pixmap, eg (untested):

    Qt Code:
    1. QImage *compositeImage = new QImage(subImageWidth*subImages.count(),subImages[0].height(),QImage::Format_RGB888);
    2.  
    3. QString fname = fg->getSaveFileName();
    4. QPainter painter(compositeImage);
    5.  
    6. for (int i=0;i<subImages.count();i++)
    7. painter.drawImage(i*subImageWidth,0,subImages[i]);
    8.  
    9. compositeImage->save(fname);
    10. delete compositeImage;
    To copy to clipboard, switch view to plain text mode 

    I'm asking if there's an easier/better way to do it in Qt4 like is done in Qt5.


    Added after 50 minutes:


    Rajesh, I dismissed your good suggestion too quickly. I'm sorry!

    I read the documentation you pointed me too, and I think it is what I need.

    I'll give it a try!

    Thanks,

    Dave Thomas
    Last edited by davethomaspilot; 24th September 2014 at 14:22.

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,332
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    317
    Thanked 871 Times in 858 Posts

    Default Re: How to save widgets in layout to single file

    Just be aware that QPixmap::grabWidget() is deprecated in Qt 5. Also, there is no need to allocate memory for the QImage (ie. using "new" as you do in line 1 above). You can simply declare a QImage on the stack and the memory will be freed up when it goes out of scope at the end of the method.

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: How to save widgets in layout to single file

    Quote Originally Posted by davethomaspilot View Post
    Of course I can write code to use QPainter to build it from each pixmap
    And that is what you should do.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 0
    Last Post: 13th November 2013, 18:43
  2. Replies: 0
    Last Post: 30th April 2012, 15:17
  3. Replies: 5
    Last Post: 18th April 2010, 23:31
  4. Single Log File for Qt Project.
    By qtlinuxnewbie in forum Newbie
    Replies: 5
    Last Post: 16th February 2010, 13:00
  5. Replies: 11
    Last Post: 7th July 2006, 13:09

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
  •  
Qt is a trademark of The Qt Company.