Nominate our 2022 Qt Champions!

How to implement a scrollbar for Qframe in which user add many labels by clicking button

  • how can i implement a scrollbar for a Qframe in which elements like Labels, QTextEdit are added dynamically when a button is clicked by user(done programmatically not by drag and drop), i want when the elements exceed the screen size the scrollbar works automatically for that QFrame in which these elements are added.

  • Qt Champions 2017

    How about using the QScrollArea class. Sample example is already in given the Qt documentation for this. You can check Qt Documentation for this.

  • @dheerendra I have already read it but couldn't understand how can i implement it for my Qframe.

  • Qt Champions 2017

    You can check this ? Your Frame is also Qwidget only as it inherits from QWidget.
    scrollArea = new QScrollArea;

    Question - Why do you want QFrame ?

  • I have used QFrame to divide my screen into three parts and in each part different elements are show like for a paint application we have a left palette for showing elements then a centre palette for adding elements(only here i want to add scrollarea)and a third one the right side for showing the all the data inputed in those QTextEdit at the centre.

  • Qt Champions 2017

    Ok. got it. It should work.

  • It is showing a scrollarea at the top left corner and i have used it for my QFrame which is placed at the centre of the screen.

  • Qt Champions 2017

    Must be some logic issue. Can you give sample code ?

  • Qt Champions 2017

    I tried something like follows. It works perfectly without any issue. This shows the scrollbar when frame size incrases.

    this->m_Area = new QScrollArea;
    this->m_Frame = new QFrame;
    this->m_Frame->setFrameStyle(QFrame::Panel | QFrame::Raised);

  • mainwindow.cpp

    MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)
    edit = new QTextEdit(this);
    wordLabel = new QLabel(this);
    m_Area = new QScrollArea(this);
    m_Frame = new QFrame(this);
    m_Frame->setFrameStyle(QFrame::Panel | QFrame::Raised);
    Still showing the scrollarea at the top left corner only

  • Qt Champions 2017

    As I said it is logic issue. Please create separate widget class. Inside the constructor You should do like this. Create ui object. Create texfield. Create other object. Add all them to horizontal layout. Set the horizontal layout to your frame. Set the frame to scroll area. Please don't pass the parent to frame, scrollarea.

  • Lifetime Qt Champion


    That's because m_Area is a "free" widget, you don't put it as central widget of your QMainWindow subclass thus it's put on the default location (0, 0) which happens to be top left. It will also not get resized automatically since it's not part of any layout. In the case of QMainWindow, like I wrote before, you can set it as central widget.

Log in to reply