Results 1 to 5 of 5

Thread: QImage Help Pls

  1. #1
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QImage Help Pls

    Hi,

    I have a widget of size, lets say (400,400). This widget i used to show an image. Also, this widget has a kind of box or frame of size (350,350). When user opens an image file, if the image size is bigger than the frame, then the part of the image that is outside the frame should look a little hazy whereas the part of the image that is inside the frame should be clear.

    How can I do it?

    Thanks a lot.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QImage Help Pls

    You could use QLabel for showing the image, then use custom painting to draw the frame and for example a transparent gray layer over the part which should be "hazy".

    Qt Code:
    1. void ImageLabel::paintEvent(QPaintEvent* e)
    2. {
    3. // let the label paint the image (setPixmap()) underneath
    4. QLabel::paintEvent(e);
    5.  
    6. // init painter for custom drawing
    7. QPainter painter(this);
    8.  
    9. // draw a frame over the image (black, 2 pixels wide)
    10. QRect frameRect(0, 0, 350, 350);
    11. painter.setPen(QPen(Qt::black, 2));
    12. painter.setBrush(Qt::NoBrush);
    13. painter.drawRect(frameRect);
    14.  
    15. // calc clip region
    16. QRegion clipRegion(rect());
    17. clipRegion = clipRegion.subtract(frameRect);
    18. painter.setClipRegion(clipRegion);
    19.  
    20. // draw a gray layour over the rest to make it look hazy
    21. painter.setPen(Qt::NoPen);
    22. QColor hazyColor(QColor(Qt::gray));
    23. hazyColor.setAlpha(100);
    24. painter.setBrush(QBrush(hazyColor));
    25. painter.drawRect(rect());
    26. }
    To copy to clipboard, switch view to plain text mode 

    PS. above code is written on the fly and is not guaranteed to work 100%
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QImage Help Pls

    Thanks a lot for the code. Is kind of working but not perfectly. I will look into it, and post back the working code.

    Thanks.

  4. #4
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QImage Help Pls

    Another related help please.

    How can I draw the image centered ?

    What i mean is, if the image size is, lets say 500,500, then for the widget of size 400,400, the image should be drawn from -50 to 450 (x-scale) and -50 to 450 (y-scale), so that the center of the image co-incides with the center of the widget.

    How can I do this ?

    Thanks a lot.

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QImage Help Pls

    I'm not sure if QLabel is capable of that, you might have to draw the pixmap yourself then.
    At least you can make use of:
    void QStyle::drawItemPixmap ( QPainter * painter, const QRect & rect, int alignment, const QPixmap & pixmap ) const [virtual]
    J-P Nurmi

Similar Threads

  1. [Qt4] How to load Url image into QImage?
    By Gonzalez in forum Qt Programming
    Replies: 6
    Last Post: 13th October 2014, 10:10
  2. QImage : trouble with save() method
    By krivenok in forum Qt Programming
    Replies: 12
    Last Post: 3rd May 2006, 20:55
  3. QImage or QPixmap?
    By Dark_Tower in forum Qt Programming
    Replies: 9
    Last Post: 1st April 2006, 17:08
  4. Replies: 3
    Last Post: 15th March 2006, 11:44
  5. Trouble with image painting (QPainter + QImage)
    By krivenok in forum Qt Programming
    Replies: 1
    Last Post: 4th February 2006, 20:25

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.