title: QScrollArea Custom Widget not Repainting Properly
body: <p>I have a QScrollArea that contains my custom widget. When I change the data of the custom widget(which changes sizeHint() and the widget takes up more space) the end of each line and the last few lines are cut off. I've created a little program to replicate the issue.</p>
  
  <p><strong><em>widget.h</em></strong></p>
  
  <pre><code>#ifndef WIDGET_H
  #define WIDGET_H
  
  #include &lt;QWidget&gt;
  #include &lt;QString&gt;
  
  class Widget : public QWidget
  {
      Q_OBJECT
  
  public:
      explicit Widget(QWidget *parent = 0);
  
      void changeData(const QString &amp;, const unsigned &amp;, const unsigned &amp;);
  
      QSize sizeHint() const override;
  protected:
      void paintEvent(QPaintEvent *event) override;
  
  private:
      QString lines;
      unsigned lineSize = 0, rowSize = 0;
  };
  
  #endif // WIDGET_H
  </code></pre>
  
  <p><strong><em>main.cpp</em></strong></p>
  
  <pre><code>#include "widget.h"
  #include &lt;QApplication&gt;
  #include &lt;QtWidgets&gt;
  
  Widget::Widget(QWidget *parent) : QWidget(parent)
  {
      changeData("TESTING\nFUNCTIO\n1234567\nWORDS77\n", 7, 4);
      update();
  }
  
  void Widget::changeData(const QString &amp;l, const unsigned &amp;lineSz, const unsigned &amp;rowSz)
  {
      lines = l;
      lineSize = lineSz;
      rowSize = rowSz;
  }
  
  void Widget::paintEvent(QPaintEvent * /*event*/)
  {
      QPainter painter(this);
      painter.setRenderHint(QPainter::Antialiasing);
  
      QFont defaultFont = painter.font();
      QPen defaultPen = painter.pen();
      defaultFont.setLetterSpacing(QFont::AbsoluteSpacing, lineSize / 2 - 3);
      // set pen and font
      painter.setFont(defaultFont);
      painter.setPen(defaultPen);
      // starting point
      QPoint prntPoint(5, 15);
  
      for (QString::size_type ind = 0, row = 1; ind &lt; lines.size(); ++ind)
      {
          if (lines[ind] != '\n') // if it's a letter
          {
              // print the letter and move the point to the right
              painter.drawText(prntPoint, QString(lines[ind]));
              prntPoint.setX(prntPoint.x() + 20);
          }
          else if (lines[ind] == '\n') // if the character is a newline
          {
              prntPoint.setX(5); // move point back to the very left
              prntPoint.setY(row * 20 + 15); // move down
              ++row;
          }
      }
  }
  
  QSize Widget::sizeHint() const
  {
      return QSize(lineSize * 20, rowSize * 20);
  }
  
  
  
  int main(int argc, char *argv[])
  {
      QApplication a(argc, argv);
  
      Widget *w = new Widget;
      QScrollArea area;
  
      area.setWidget(w);
      area.show();
  
      //change string and other data variables
      w-&gt;changeData("1234567890\nQWERTYU123\nLETTERS123\nWORDSSS123\n"
                    "TESTING123\nFUNCTIO123\nLETTERS123\nWORDSSS123\n", 10, 8);
  
      return a.exec();
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/egNsJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/egNsJ.png" alt="Widget not updating properly"></a></p>
  
  <p>As you can see the data we set in the main function is cut off, only the first 7 characters and first 4 lines are printed, basically taking the first string we set in the constructor and taking a subset of this new string and printing it.</p>
  
  <p><a href="https://i.stack.imgur.com/PrCEV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PrCEV.png" alt="Without QScrollArea"></a></p>
  
  <p>Removing QScrollArea fixes the issue but I need the scrolling capabilities for my application. Any help will be appreciated. Thank you</p>