QScrollArea for customwidget



  • Hi,

    I want to add new QComboBox when user presses Add QPushButton. I have added a customWidget to the QScrollArea. When I clicked Add, new combo is added but size is getting reduced. So if we add more combos they are not visible.

    Ideally, if we add more combos I need a vertical scrollbar to be available. I have given my sample code. Plz share your views. Highly appreciate your help in this regard.

    Below code is working.

    // header file
    @

    #ifndef PLOTCONTROLS_H
    #define PLOTCONTROLS_H

    #include <QtCore>
    #include <QtGui>

    class CustomWidget: public QWidget
    {
    Q_OBJECT
    private:
    QVBoxLayout* vbox;
    public:
    CustomWidget(QWidget *parent=0);
    QPushButton *btn;
    public slots:
    void addCustomWidget();
    };

    class PlotControls: public QWidget
    {
    Q_OBJECT
    private:
    QScrollArea *scroll;
    CustomWidget custom;
    public:
    PlotControls(QWidget
    parent=0);
    };

    #endif // PLOTCONTROLS_H
    @

    // main.cpp
    @

    #include <stdlib.h>
    #include <QtGui>
    #include <QtCore/QDebug>

    #include "PlotControls.h"

    CustomWidget::CustomWidget(QWidget parent)
    {
    QComboBox
    combo1 = new QComboBox;
    btn = new QPushButton("Add");
    vbox = new QVBoxLayout;
    vbox->addWidget(combo1);
    QObject::connect(btn,SIGNAL(clicked()),this,SLOT(addCustomWidget()));
    this->setLayout(vbox);
    }

    void CustomWidget::addCustomWidget()
    {
    this->vbox->addWidget(new QComboBox);
    this->setLayout(vbox);
    }

    PlotControls::PlotControls(QWidget *parent)
    {

    QScrollArea* scroll = new QScrollArea;
    QLabel* l1 = new QLabel("Title:");
    QLineEdit* le1 = new QLineEdit;
    QGridLayout* grid = new QGridLayout;
    grid->addWidget(l1,0,0);
    grid->addWidget(le1,0,1);
    
    custom = new CustomWidget(scroll);
    scroll->setWidget(custom);
    grid->addWidget(scroll,3,0);
    grid->addWidget(custom->btn);
    this->setLayout(grid);
    

    }

    class MyMainWindow:public QMainWindow
    {
    private:
    QDockWidget* dock;
    public:
    MyMainWindow()
    {
    QGroupBox *grp = new QGroupBox;
    QVBoxLayout *v = new QVBoxLayout;
    QTextEdit *te = new QTextEdit;
    v->addWidget(te);
    dock = new QDockWidget("Plot Controls",this);
    dock->setAllowedAreas(Qt::BottomDockWidgetArea);
    dock->setWidget(new PlotControls);
    this->addDockWidget(Qt::BottomDockWidgetArea, dock);
    grp->setLayout(v);
    this->setCentralWidget(grp);

     }
    

    };

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    MyMainWindow m;
    m.show();
    return app.exec();
    }

    @

    Regards,
    Haney.


Log in to reply
 

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