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.



  • 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.



  • You can check this ? Your Frame is also Qwidget only as it inherits from QWidget.
    scrollArea = new QScrollArea;
    scrollArea->setBackgroundRole(QPalette::Dark);
    scrollArea->setWidget(<yourframeobject>);

    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.



  • 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.



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



  • 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->setFixedSize(2000,800);
    
    this->m_Frame->setFrameStyle(QFrame::Panel | QFrame::Raised);
    this->m_Area->setWidget(m_Frame);
    this->m_Area->show();


  • mainwindow.cpp

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    edit = new QTextEdit(this);
    wordLabel = new QLabel(this);
    m_Area = new QScrollArea(this);
    m_Frame = new QFrame(this);
    m_Frame->setFixedSize(2000,800);
    m_Frame->setFrameStyle(QFrame::Panel | QFrame::Raised);
    m_Area->setWidget(m_Frame);
    m_Area->show();
    }
    Still showing the scrollarea at the top left corner only



  • 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

    Hi,

    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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.