PDA

View Full Version : Scrollbars not showing up on QScrollArea with a custom Widget



astropirit
11th August 2010, 10:02
hi all!
I have a custom widget which is pretty much a modified drawing widget from the pluginanddraw example. I loaded an image onto it, but it doesn't fully fit since the image might be too big for the widget. So i placed this widget insode a QScrollBar to be able to see the entire thing. However, the scroll bars for some odd reason do not show up.

In the documentations i read that custom widgets must have the sizeHint method and this widget does infect have it.

here is my code for the widget:

RenderArea.h

#ifndef RENDERAREA_H
#define RENDERAREA_H

#include <QColor>
#include <QImage>
#include <QPainterPath>
#include <QWidget>
#include <QFile>
#include <QPainter>
#include <QMouseEvent>
#include <QByteArray>
#include <QString>
#include "interfaces.h"

class RenderArea : public QWidget
{
Q_OBJECT

public:
RenderArea(QWidget *parent = 0);

bool openImage(const QString &fileName);
bool saveImage(const QString &fileName, const char *fileFormat);
void setImage(const QImage &image);
void insertShape(const QPainterPath &path);
void setBrushColor(const QColor &color);
void setBrushWidth(int width);
void setBrush(BrushInterface *brushInterface, const QString &brush);

QImage image() const { return theImage; }
QColor brushColor() const { return color; }
int brushWidth() const { return thickness; }
QSize sizeHint() const;

protected:
void paintEvent(QPaintEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);

private:
void setupPainter(QPainter &painter);

QImage theImage;
QColor color;
int thickness;

BrushInterface *brushInterface;
QString brush;
QPoint lastPos;

QPainterPath pendingPath;
};


#endif // RENDERAREA_H


RenderArea.cpp

#include "RenderArea.h"

RenderArea::RenderArea(QWidget *parent) :
QWidget(parent),
color(Qt::blue),
thickness(3),
brushInterface(0),
lastPos(-1, -1)
{
setAttribute(Qt::WA_StaticContents);
setAttribute(Qt::WA_NoBackground);
theImage.load(":/resources/Screenshot.png");

}

bool RenderArea::openImage(const QString &fileName)
{
QImage image;
if (!image.load(fileName))
return false;

setImage(image);
return true;
}

bool RenderArea::saveImage(const QString &fileName, const char *fileFormat)
{
return theImage.save(fileName, fileFormat);
}

void RenderArea::setImage(const QImage &image)
{
theImage = image.convertToFormat(QImage::Format_RGB32);
update();
updateGeometry();
}

void RenderArea::insertShape(const QPainterPath &path)
{
pendingPath = path;
#ifndef QT_NO_CURSOR
setCursor(Qt::CrossCursor);
#endif
}

void RenderArea::setBrushColor(const QColor &color)
{
this->color = color;
}

void RenderArea::setBrushWidth(int width)
{
thickness = width;
}

void RenderArea::setBrush(BrushInterface *brushInterface, const QString &brush)
{
this->brushInterface = brushInterface;
this->brush = brush;
}

QSize RenderArea::sizeHint() const
{
return theImage.size();
}

void RenderArea::paintEvent(QPaintEvent * /* event */)
{
QPainter painter(this);
painter.drawImage(QPoint(0, 0), theImage);
}

void RenderArea::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
if (!pendingPath.isEmpty()) {
QPainter painter(&theImage);
setupPainter(painter);

const QRectF boundingRect = pendingPath.boundingRect();
QLinearGradient gradient(boundingRect.topRight(),
boundingRect.bottomLeft());
gradient.setColorAt(0.0, QColor(color.red(), color.green(),
color.blue(), 63));
gradient.setColorAt(1.0, QColor(color.red(), color.green(),
color.blue(), 191));
painter.setBrush(gradient);
painter.translate(event->pos() - boundingRect.center());
painter.drawPath(pendingPath);

pendingPath = QPainterPath();
#ifndef QT_NO_CURSOR
unsetCursor();
#endif
update();
} else {
if (brushInterface) {
QPainter painter(&theImage);
setupPainter(painter);
const QRect rect = brushInterface->mousePress(brush, painter,
event->pos());
update(rect);
}

lastPos = event->pos();
}
}
}

void RenderArea::mouseMoveEvent(QMouseEvent *event)
{
if ((event->buttons() & Qt::LeftButton) && lastPos != QPoint(-1, -1)) {
if (brushInterface) {
QPainter painter(&theImage);
setupPainter(painter);
const QRect rect = brushInterface->mouseMove(brush, painter, lastPos, event->pos());
update(rect);
}

lastPos = event->pos();
}
}

void RenderArea::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton && lastPos != QPoint(-1, -1)) {
if (brushInterface) {
QPainter painter(&theImage);
setupPainter(painter);
QRect rect = brushInterface->mouseRelease(brush, painter,
event->pos());
update(rect);
}

lastPos = QPoint(-1, -1);
}
}

void RenderArea::setupPainter(QPainter &painter)
{
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(color, thickness, Qt::SolidLine, Qt::RoundCap,
Qt::RoundJoin));
}