Results 1 to 6 of 6

Thread: Issues writing a simple image viewer class

  1. #1
    Join Date
    Aug 2008
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Issues writing a simple image viewer class

    Hey guys,

    In the past I've used QLabel for displaying images. I am writing a image viewer class so that I can do more with the image I'm displaying. Long term goals including applying transformations to the image and even animating those transformations. But for a first step I just want to display an image.

    I'm having issues with that however. If you look at the attached screenshot you'll see I've used my ImageViewer class to load an image of my dog, but it's only showing the top left portion of the image (you can just barely see his tail).

    I have a feeling there is something wrong I'm doing with the minimumSizeHint and sizeHint functions, but I'm not sure.

    I've attached the ImageViewer class along with a test program. I've also supplied the imageviewer.cpp inline below for quick reading.

    Any help would be much appreciated!
    Qt Code:
    1. #include "imageviewer.h"
    2.  
    3. #include <QPainter>
    4. #include <QVBoxLayout>
    5. #include <QScrollArea>
    6. #include <QDebug>
    7.  
    8.  
    9. ImageWidget::ImageWidget(QWidget *parent) : QWidget(parent)
    10. {
    11. }
    12.  
    13. ImageWidget::~ImageWidget()
    14. {
    15. }
    16.  
    17. void ImageWidget::setPixmap(const QPixmap &pixmap)
    18. {
    19. m_pixmap = pixmap;
    20. update();
    21. }
    22.  
    23. QSize ImageWidget::minimumSizeHint() const
    24. {
    25. // WHAT DO I PUT HERE???
    26. if(!m_pixmap.isNull())
    27. return m_pixmap.size();
    28. else
    29. return QWidget::sizeHint();
    30. }
    31.  
    32. QSize ImageWidget::sizeHint() const
    33. {
    34. // AND HERE???
    35. if(!m_pixmap.isNull())
    36. return m_pixmap.size();
    37. else
    38. return QWidget::sizeHint();
    39. }
    40.  
    41. void ImageWidget::paintEvent(QPaintEvent *event)
    42. {
    43. if(!m_pixmap.isNull())
    44. {
    45. QPainter painter(this);
    46. painter.setRenderHint(QPainter::Antialiasing);
    47. painter.save();
    48. transformPainter(&painter);
    49. drawPixmap(&painter);
    50. painter.restore();
    51. }
    52. else
    53. QWidget::paintEvent(event);
    54. }
    55.  
    56. void ImageWidget::transformPainter(QPainter *painter)
    57. {
    58. // does nothing yet...
    59. }
    60.  
    61. void ImageWidget::drawPixmap(QPainter *painter)
    62. {
    63. painter->drawPixmap(QPointF(0, 0), m_pixmap);
    64. }
    65.  
    66.  
    67. ImageViewer::ImageViewer(QWidget *parent/* = 0*/, Qt::WindowFlags f/* = 0*/) : QWidget(parent, f)
    68. {
    69. QVBoxLayout *baseLayout = new QVBoxLayout;
    70. m_scrollArea = new QScrollArea(this);
    71. m_imageWidget = new ImageWidget(m_scrollArea);
    72. m_scrollArea->setWidget(m_imageWidget);
    73. baseLayout->addWidget(m_scrollArea);
    74.  
    75. m_imageWidget->setMinimumSize(m_scrollArea->minimumSizeHint());
    76.  
    77. setLayout(baseLayout);
    78. }
    79.  
    80. ImageViewer::~ImageViewer()
    81. {
    82. }
    83.  
    84. void ImageViewer::setPixmap(const QPixmap &pixmap)
    85. {
    86. m_imageWidget->setPixmap(pixmap);
    87. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images
    Attached Files Attached Files

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Issues writing a simple image viewer class

    Your class is similar to QLabel...
    Also search the forums before you post..

    This thread is very much similar to yours...hope you get help from it...

  3. #3
    Join Date
    Aug 2008
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Issues writing a simple image viewer class

    Quote Originally Posted by aamer4yu View Post
    Your class is similar to QLabel...
    Also search the forums before you post..
    I know it's similar to QLabel. I stated in my initial post that I was trying to create an alternative to using QLabel.

    I did search the forums, but I only found info on QGraphicsScene.... and as I understand it QGraphicsScene is for managing a large number of graphical items. It sounds like overkill for displaying a single image. (Please correct me if I'm wrong however).

    Quote Originally Posted by aamer4yu View Post
    This thread is very much similar to yours...hope you get help from it...
    Again, references QGraphicsView/Scene. Seems to be overkill to display and do transformations on a single image.

  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: Issues writing a simple image viewer class

    Ok,

    I don't have read all, but why do you want to do all the nasty work with the size thing by your own? Let Qt do the dirty work for you!

    Since - I have said this this day already in an other post - QPixmap is a QPaintDevice use it! What I mean is:
    • store the given pixmap (gpm)
    • paint the gpm with transformations on a new pixmap (npm) you create
    • set this pixmap (npm) to the label

    By doing so, you don't have to take care about all the layout stuff...


    Lykurg

  5. #5
    Join Date
    Aug 2008
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Issues writing a simple image viewer class

    Quote Originally Posted by Lykurg View Post
    Ok,

    I don't have read all, but why do you want to do all the nasty work with the size thing by your own? Let Qt do the dirty work for you!

    Since - I have said this this day already in an other post - QPixmap is a QPaintDevice use it! What I mean is:
    • store the given pixmap (gpm)
    • paint the gpm with transformations on a new pixmap (npm) you create
    • set this pixmap (npm) to the label

    By doing so, you don't have to take care about all the layout stuff...
    Lykurg
    I've thought about that... but then I would have to store two copies of the image (one in my pixmap and one in the QLabel). Also I would have to copy the pixmap to the QLabel whenever I made a new transformation (I would imagine this would be an expensive operation if I were to animate my transformations).

    By doing it in the method I'm attempting I don't have any of those issues. I store one pixmap... and I paint that one pixmap with the desired transformations onto the widget.

    Instead of suggesting other options I would prefer some help with why my method is not working (unless people have suggestions that don't include QLabel )

  6. #6
    Join Date
    Aug 2008
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Issues writing a simple image viewer class

    Turns out my issue was really trivial. When adding an image to a scroll area you need to call setWidgetResizable to true... otherwise the widget is never resized:
    Qt Code:
    1. m_scrollArea->setWidgetResizable(true);
    To copy to clipboard, switch view to plain text mode 

    It now displays the image correctly (mostly that is... there are still some minor issues to resolve, but I think I can work those out).

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.