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!
#include "imageviewer.h"
#include <QPainter>
#include <QVBoxLayout>
#include <QScrollArea>
#include <QDebug>
{
}
ImageWidget::~ImageWidget()
{
}
void ImageWidget
::setPixmap(const QPixmap &pixmap
) {
m_pixmap = pixmap;
update();
}
QSize ImageWidget
::minimumSizeHint() const {
// WHAT DO I PUT HERE???
if(!m_pixmap.isNull())
return m_pixmap.size();
else
}
QSize ImageWidget
::sizeHint() const {
// AND HERE???
if(!m_pixmap.isNull())
return m_pixmap.size();
else
}
{
if(!m_pixmap.isNull())
{
painter.
setRenderHint(QPainter::Antialiasing);
painter.save();
transformPainter(&painter);
drawPixmap(&painter);
painter.restore();
}
else
}
void ImageWidget
::transformPainter(QPainter *painter
) {
// does nothing yet...
}
void ImageWidget
::drawPixmap(QPainter *painter
) {
painter
->drawPixmap
(QPointF(0,
0), m_pixmap
);
}
ImageViewer
::ImageViewer(QWidget *parent
/* = 0*/, Qt
::WindowFlags f
/* = 0*/) : QWidget(parent, f
){
m_imageWidget = new ImageWidget(m_scrollArea);
m_scrollArea->setWidget(m_imageWidget);
baseLayout->addWidget(m_scrollArea);
m_imageWidget->setMinimumSize(m_scrollArea->minimumSizeHint());
setLayout(baseLayout);
}
ImageViewer::~ImageViewer()
{
}
void ImageViewer
::setPixmap(const QPixmap &pixmap
) {
m_imageWidget->setPixmap(pixmap);
}
#include "imageviewer.h"
#include <QPainter>
#include <QVBoxLayout>
#include <QScrollArea>
#include <QDebug>
ImageWidget::ImageWidget(QWidget *parent) : QWidget(parent)
{
}
ImageWidget::~ImageWidget()
{
}
void ImageWidget::setPixmap(const QPixmap &pixmap)
{
m_pixmap = pixmap;
update();
}
QSize ImageWidget::minimumSizeHint() const
{
// WHAT DO I PUT HERE???
if(!m_pixmap.isNull())
return m_pixmap.size();
else
return QWidget::sizeHint();
}
QSize ImageWidget::sizeHint() const
{
// AND HERE???
if(!m_pixmap.isNull())
return m_pixmap.size();
else
return QWidget::sizeHint();
}
void ImageWidget::paintEvent(QPaintEvent *event)
{
if(!m_pixmap.isNull())
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.save();
transformPainter(&painter);
drawPixmap(&painter);
painter.restore();
}
else
QWidget::paintEvent(event);
}
void ImageWidget::transformPainter(QPainter *painter)
{
// does nothing yet...
}
void ImageWidget::drawPixmap(QPainter *painter)
{
painter->drawPixmap(QPointF(0, 0), m_pixmap);
}
ImageViewer::ImageViewer(QWidget *parent/* = 0*/, Qt::WindowFlags f/* = 0*/) : QWidget(parent, f)
{
QVBoxLayout *baseLayout = new QVBoxLayout;
m_scrollArea = new QScrollArea(this);
m_imageWidget = new ImageWidget(m_scrollArea);
m_scrollArea->setWidget(m_imageWidget);
baseLayout->addWidget(m_scrollArea);
m_imageWidget->setMinimumSize(m_scrollArea->minimumSizeHint());
setLayout(baseLayout);
}
ImageViewer::~ImageViewer()
{
}
void ImageViewer::setPixmap(const QPixmap &pixmap)
{
m_imageWidget->setPixmap(pixmap);
}
To copy to clipboard, switch view to plain text mode
Bookmarks