title: Scrolling list of labels on Qt
body: <p>I'm trying to create a scrollbar for my labels. For the moment, if the users create too many labels, the sizes of the button and of the text zone are reduced, that's why I wanted to create a scrollbar then if there is too many labels, they will not change the aspect of the window.</p>
  
  <p>This is my actual code : </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;QApplication&gt;
  #include &lt;QPushButton&gt;
  #include &lt;QLineEdit&gt;
  #include &lt;QWidget&gt;
  #include &lt;QFormLayout&gt;
  #include "LibQt.hpp"
  
  LibQt::LibQt() : QWidget()
  {
    this-&gt;size_x = 500;
    this-&gt;size_y = 500;
    QWidget::setWindowTitle("The Plazza");
    setFixedSize(this-&gt;size_x, this-&gt;size_y);
    manageOrder();
  }
  
  LibQt::~LibQt()
  {
  }
  
  void LibQt::keyPressEvent(QKeyEvent* event)
  {
    if (event-&gt;key() == Qt::Key_Escape)
      QCoreApplication::quit();
    else
      QWidget::keyPressEvent(event);
  }
  
  void LibQt::manageOrder()
  {
    this-&gt;converLayout = new QFormLayout;
    this-&gt;testline = new QLineEdit;
    this-&gt;m_button = new QPushButton("Send");
    this-&gt;m_button-&gt;setCursor(Qt::PointingHandCursor);
    this-&gt;m_button-&gt;setFont(QFont("Comic Sans MS", 14));
    this-&gt;converLayout-&gt;addRow("Order : ", this-&gt;testline);
    this-&gt;converLayout-&gt;addWidget(this-&gt;m_button);
    QObject::connect(m_button, SIGNAL(clicked()), this, SLOT(ClearAndGetTxt()));
    CreateLabel("test");
    CreateLabel("test2");
  }
  
  void            LibQt::CreateLabel(std::string text)
  {
    QString qstr = QString::fromStdString(text);
  
    this-&gt;label = new QLabel(qstr);
    this-&gt;converLayout-&gt;addWidget(this-&gt;label);
    this-&gt;setLayout(converLayout);
  }
  
  std::string     LibQt::ClearAndGetTxt()
  {
    QString txt = this-&gt;testline-&gt;text();
  
    if (!txt.isEmpty())
      {
        this-&gt;usertxt = txt.toStdString();
        std::cout &lt;&lt; this-&gt;usertxt &lt;&lt; std::endl;
        this-&gt;testline-&gt;clear();
        CreateLabel(this-&gt;usertxt);
        return (this-&gt;usertxt);
      }
    return (this-&gt;usertxt);
  }
  
  std::string     LibQt::getUsertxt()
  {
    return (this-&gt;usertxt);
  }
  </code></pre>
  
  <p>And this is the .hpp : </p>
  
  <pre><code>#ifndef _LIBQT_HPP_
  #define _LIBQT_HPP_
  
  #include &lt;QApplication&gt;
  #include &lt;QWidget&gt;
  #include &lt;QPushButton&gt;
  #include &lt;QFormLayout&gt;
  #include &lt;QLabel&gt;
  #include &lt;QLineEdit&gt;
  #include &lt;QKeyEvent&gt;
  
  class   LibQt : public QWidget
  {
    Q_OBJECT
  
  public:
    LibQt();
    ~LibQt();
    void manageOrder();
    std::string getUsertxt();
    void keyPressEvent(QKeyEvent *event);
    void keyPressEventEnter(QKeyEvent *event);
    void CreateLabel(std::string text);
  public slots:
    std::string ClearAndGetTxt();
  protected:
    int   size_x;
    int   size_y;
    QPushButton *m_button;
    QLineEdit *testline;
    std::string usertxt;
    QLabel *label;
    QFormLayout *converLayout;
  };
  
  #endif /* _LIBQT_HPP_ */
  </code></pre>