Results 1 to 7 of 7

Thread: Scaling window with image

  1. #1
    Join Date
    Oct 2012
    Location
    Warsaw,Poland
    Posts
    17
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Scaling window with image

    Hi Community!

    First of all, I want to say hallo, because it's my first post here I have a problem, which should be trivial, but I can't handle it by myself.

    I have an application with two images as Pixmaps. I want them to be scaled when window is resized for example as 40% of width of the window. I've tried a resizeEvent function and there I've implemented resizing the images. It works, but only when window is making bigger, and when I want to make it smaller it's blocked because of the size of images... How can I disable the reducing-block-functionality?

    Sorry for my poor English. If my problem is not clear for you, I'll give an example...

    Greets,
    Daniel

  2. #2
    Join Date
    Oct 2011
    Posts
    27
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Scaling window with image

    Hi

    I think that you can't shrink your image because of your widget minimum size ... and not the pixmap
    try adding setMinimumSize(10,10) to see if it works.

    but normally, if you set your QPixmap into a QLabel, you should not need to reimplement resizeEvent, just use QLabel::setScaledContents(true);

    hope it helps ...

  3. #3
    Join Date
    Oct 2012
    Location
    Warsaw,Poland
    Posts
    17
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Scaling window with image

    Ok, thanks thomas@itest. Could you tell me one more thing: How to keep the height and width in proportion? I can't find that option...

  4. #4
    Join Date
    Oct 2011
    Posts
    27
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Scaling window with image

    me neither, i don't find this option, ...
    so, I think that you were right : you must reimplement resizeEvent and scale the pixmap by hand using QPixmap::scale and Qt::AspectRatioMode

    something like :

    Qt Code:
    1. void myWidget::resizeEvent(QResizeEvent *event)
    2. {
    3. myLabel->setPixmap(myPixmap.scaled(event->size(),Qt::KeepAspectRatioByExpanding));
    4. ...
    5. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Oct 2012
    Location
    Warsaw,Poland
    Posts
    17
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Scaling window with image

    I think it won't work...

    On one hand we set:
    Qt Code:
    1. ImageLabel->setScaledContents(true);
    To copy to clipboard, switch view to plain text mode 
    and setting it we have an image label, which will expand to every free place not keeping the propotion. And then after resizeEvent:
    1) Firstly - scaling the label image wont have any effect because we set ScaledContents earlier, so image wont keep proportion.
    2) Secondly - we change the size of a label, so we couldn't shrink it...

    How do you think?

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Scaling window with image

    This should be useful
    Qt Code:
    1. int QWidget::heightForWidth ( int w ) const
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  7. #7
    Join Date
    Oct 2012
    Location
    Warsaw,Poland
    Posts
    17
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Scaling window with image

    Hmm, in Reference Documentation is something like this:
    Qt Code:
    1. void QSizePolicy::setHeightForWidth ( bool dependent )
    2. Sets the flag determining whether the widget's preferred height depends on its width, to dependent.
    To copy to clipboard, switch view to plain text mode 
    But it doesn't work... Maybe I do something wrong...
    Qt Code:
    1. QSizePolicy sizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    2. sizePolicy.setHeightForWidth(true);
    3.  
    4. MyGrid=new QGridLayout();
    5. setLayout(MyGrid);
    6.  
    7. Image1=new QImage(1000,1000,QImage::Format_RGB32);
    8. Image2=new QImage(1000,1000,QImage::Format_RGB32);
    9. Pixmap1=new QPixmap(QPixmap::fromImage(*Image1));
    10. Pixmap2=new QPixmap(QPixmap::fromImage(*Image2));
    11.  
    12. ImageLabel1=new QLabel();
    13. ImageLabel1->setScaledContents(true);
    14. ImageLabel1->setSizePolicy(sizePolicy);
    15. ImageLabel1->setPixmap(Pixmap1->scaled(300,300,Qt::KeepAspectRatio));
    16.  
    17. ImageLabel2=new QLabel();
    18. ImageLabel2->setScaledContents(true);
    19. ImageLabel1->setSizePolicy(sizePolicy);
    20. ImageLabel2->setPixmap(Pixmap2->scaled(300,300,Qt::KeepAspectRatio));
    21.  
    22. MyGrid->addWidget(ImageLabel1,0,0);
    23. MyGrid->addWidget(ImageLabel2,0,1);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 17th October 2011, 13:56
  2. Replies: 1
    Last Post: 27th May 2011, 20:27
  3. Non-scaling image added to scene
    By Thomas_Lerman in forum Qt Programming
    Replies: 10
    Last Post: 1st February 2011, 21:35
  4. QWebView and image scaling
    By naresh in forum Qt Programming
    Replies: 1
    Last Post: 25th November 2010, 03:38
  5. Fast image drawing/scaling in Qt 3.3
    By eriwik in forum Qt Programming
    Replies: 1
    Last Post: 21st June 2006, 10:45

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.