Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QScrollArea for customwidget
Qt 6.11 is out! See what's new in the release blog

QScrollArea for customwidget

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 634 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    haney
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved