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
Qt Code:
  1. #ifndef RENDERAREA_H
  2. #define RENDERAREA_H
  3.  
  4. #include <QColor>
  5. #include <QImage>
  6. #include <QPainterPath>
  7. #include <QWidget>
  8. #include <QFile>
  9. #include <QPainter>
  10. #include <QMouseEvent>
  11. #include <QByteArray>
  12. #include <QString>
  13. #include "interfaces.h"
  14.  
  15. class RenderArea : public QWidget
  16. {
  17. Q_OBJECT
  18.  
  19. public:
  20. RenderArea(QWidget *parent = 0);
  21.  
  22. bool openImage(const QString &fileName);
  23. bool saveImage(const QString &fileName, const char *fileFormat);
  24. void setImage(const QImage &image);
  25. void insertShape(const QPainterPath &path);
  26. void setBrushColor(const QColor &color);
  27. void setBrushWidth(int width);
  28. void setBrush(BrushInterface *brushInterface, const QString &brush);
  29.  
  30. QImage image() const { return theImage; }
  31. QColor brushColor() const { return color; }
  32. int brushWidth() const { return thickness; }
  33. QSize sizeHint() const;
  34.  
  35. protected:
  36. void paintEvent(QPaintEvent *event);
  37. void mousePressEvent(QMouseEvent *event);
  38. void mouseMoveEvent(QMouseEvent *event);
  39. void mouseReleaseEvent(QMouseEvent *event);
  40.  
  41. private:
  42. void setupPainter(QPainter &painter);
  43.  
  44. QImage theImage;
  45. QColor color;
  46. int thickness;
  47.  
  48. BrushInterface *brushInterface;
  49. QString brush;
  50. QPoint lastPos;
  51.  
  52. QPainterPath pendingPath;
  53. };
  54.  
  55.  
  56. #endif // RENDERAREA_H
To copy to clipboard, switch view to plain text mode 

RenderArea.cpp
Qt Code:
  1. #include "RenderArea.h"
  2.  
  3. RenderArea::RenderArea(QWidget *parent) :
  4. QWidget(parent),
  5. color(Qt::blue),
  6. thickness(3),
  7. brushInterface(0),
  8. lastPos(-1, -1)
  9. {
  10. setAttribute(Qt::WA_StaticContents);
  11. setAttribute(Qt::WA_NoBackground);
  12. theImage.load(":/resources/Screenshot.png");
  13.  
  14. }
  15.  
  16. bool RenderArea::openImage(const QString &fileName)
  17. {
  18. QImage image;
  19. if (!image.load(fileName))
  20. return false;
  21.  
  22. setImage(image);
  23. return true;
  24. }
  25.  
  26. bool RenderArea::saveImage(const QString &fileName, const char *fileFormat)
  27. {
  28. return theImage.save(fileName, fileFormat);
  29. }
  30.  
  31. void RenderArea::setImage(const QImage &image)
  32. {
  33. theImage = image.convertToFormat(QImage::Format_RGB32);
  34. update();
  35. updateGeometry();
  36. }
  37.  
  38. void RenderArea::insertShape(const QPainterPath &path)
  39. {
  40. pendingPath = path;
  41. #ifndef QT_NO_CURSOR
  42. setCursor(Qt::CrossCursor);
  43. #endif
  44. }
  45.  
  46. void RenderArea::setBrushColor(const QColor &color)
  47. {
  48. this->color = color;
  49. }
  50.  
  51. void RenderArea::setBrushWidth(int width)
  52. {
  53. thickness = width;
  54. }
  55.  
  56. void RenderArea::setBrush(BrushInterface *brushInterface, const QString &brush)
  57. {
  58. this->brushInterface = brushInterface;
  59. this->brush = brush;
  60. }
  61.  
  62. QSize RenderArea::sizeHint() const
  63. {
  64. return theImage.size();
  65. }
  66.  
  67. void RenderArea::paintEvent(QPaintEvent * /* event */)
  68. {
  69. QPainter painter(this);
  70. painter.drawImage(QPoint(0, 0), theImage);
  71. }
  72.  
  73. void RenderArea::mousePressEvent(QMouseEvent *event)
  74. {
  75. if (event->button() == Qt::LeftButton) {
  76. if (!pendingPath.isEmpty()) {
  77. QPainter painter(&theImage);
  78. setupPainter(painter);
  79.  
  80. const QRectF boundingRect = pendingPath.boundingRect();
  81. QLinearGradient gradient(boundingRect.topRight(),
  82. boundingRect.bottomLeft());
  83. gradient.setColorAt(0.0, QColor(color.red(), color.green(),
  84. color.blue(), 63));
  85. gradient.setColorAt(1.0, QColor(color.red(), color.green(),
  86. color.blue(), 191));
  87. painter.setBrush(gradient);
  88. painter.translate(event->pos() - boundingRect.center());
  89. painter.drawPath(pendingPath);
  90.  
  91. pendingPath = QPainterPath();
  92. #ifndef QT_NO_CURSOR
  93. unsetCursor();
  94. #endif
  95. update();
  96. } else {
  97. if (brushInterface) {
  98. QPainter painter(&theImage);
  99. setupPainter(painter);
  100. const QRect rect = brushInterface->mousePress(brush, painter,
  101. event->pos());
  102. update(rect);
  103. }
  104.  
  105. lastPos = event->pos();
  106. }
  107. }
  108. }
  109.  
  110. void RenderArea::mouseMoveEvent(QMouseEvent *event)
  111. {
  112. if ((event->buttons() & Qt::LeftButton) && lastPos != QPoint(-1, -1)) {
  113. if (brushInterface) {
  114. QPainter painter(&theImage);
  115. setupPainter(painter);
  116. const QRect rect = brushInterface->mouseMove(brush, painter, lastPos, event->pos());
  117. update(rect);
  118. }
  119.  
  120. lastPos = event->pos();
  121. }
  122. }
  123.  
  124. void RenderArea::mouseReleaseEvent(QMouseEvent *event)
  125. {
  126. if (event->button() == Qt::LeftButton && lastPos != QPoint(-1, -1)) {
  127. if (brushInterface) {
  128. QPainter painter(&theImage);
  129. setupPainter(painter);
  130. QRect rect = brushInterface->mouseRelease(brush, painter,
  131. event->pos());
  132. update(rect);
  133. }
  134.  
  135. lastPos = QPoint(-1, -1);
  136. }
  137. }
  138.  
  139. void RenderArea::setupPainter(QPainter &painter)
  140. {
  141. painter.setRenderHint(QPainter::Antialiasing, true);
  142. painter.setPen(QPen(color, thickness, Qt::SolidLine, Qt::RoundCap,
  143. Qt::RoundJoin));
  144. }
To copy to clipboard, switch view to plain text mode