Results 1 to 6 of 6

Thread: Issues writing a simple image viewer class

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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.