How to dynamically insert a custom widget into a scroll area



  • I have a custom widget, built in designer, made up of a row of label, progress bar, check box. In the main form, I have a scroll area, also created in the designer. I want to know how to effectively dynamically add the custom widget as as rows in the scroll area.

    MainWindow.cpp
    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "ui_Controls.h"

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_actionE_xit_triggered()
    {
    QApplication::quit();
    }

    void MainWindow::on_actionFile_triggered()
    {
    int i = ui->verticalLayout->count();

    ctls = new Controls();

    QHBoxLayout* hblayout = new QHBoxLayout();

    hblayout->addWidget(ctls);

    QWidget* window = new QWidget;
    window->setLayout(hblayout);
    window->show();

    ui->verticalLayout->addWidget(window);

    }
    @

    The Main Window grows downward rather than a vertical scroll bar appearing.


  • Lifetime Qt Champion

    Hi,

    You need to set a widget on the QScrollArea containing a QVBoxLayout. Then add your newly created widget with to that layout.

    Hope it helps



  • [quote author="SGaist" date="1392499717"]Hi,

    You need to set a widget on the QScrollArea containing a QVBoxLayout. Then add your newly created widget with to that layout.

    Hope it helps[/quote]

    I changed the approach as to how to display the widget and instead use a QListWidget. The thread is "here":http://qt-project.org/forums/viewthread/38539/. Still doesn't work


  • Lifetime Qt Champion

    It's not exactly the same concept then



  • No less troubling though. Can you offer assistance?



  • I think QListWidget is rather limited. I would say use QListView if you are going to endup with lots of items in your scroll area and are prepared write your own paint delegate.

    It really can't get any easier then using a VerticalGridLayout and using its addWidget() method



  • [quote author="DBoosalis" date="1392758583"]I think QListWidget is rather limited. I would say use QListView if you are going to endup with lots of items in your scroll area and are prepared write your own paint delegate.

    It really can't get any easier then using a VerticalGridLayout and using its addWidget() method

    [/quote]

    Got an example? I only know of QGridLayout



  • Documentation is a good place to start.



  • [quote author="DBoosalis" date="1392761494"]Documentation is a good place to start. [/quote]

    I can't find any documentation on VerticalGridLayout. That's why I asked for an example. I wanted to see an example how it can't get any easier. BTW VerticalGridLayout is part of Java.


  • Lifetime Qt Champion

    There's no vertical grid layout, you have "QVBoxLayout":http://qt-project.org/doc/qt-5/qvboxlayout.html to layout things vertically. You still can use a QGridLayout if you want, you just need to add rows


Log in to reply
 

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