Abhi Garg
Abhi Garg

Reputation: 73

How can I put a QFormLayout in a scrollArea?

My program has a main window which creates a widget called wdg. This widget has a QFormLayout with 193 rows. I want to be able to scroll down the QFormLayout. I have tried making a QScrollArea and integrate it with the layout and widget but it results in the widget not showing or scrollbar not showing. I think it has something do with the fact that the height of the new window seems to be as long as the full layout and goes off screen. I tried setting the geometry of the widget to a limited size but it just changed position not size. Results in widget not showing:

QWidget *wdg = new QWidget;
QScrollArea *scroll = new QScrollArea;
QFormLayout *formLayout = new QFormLayout(wdg);
int lenght = keys.size();
        for(int x=0; x<=lenght-1; x++)
{

            QComboBox* combo = new QComboBox;
            combo->addItem("Present");
            combo->addItem("Present and Voting");
            combo->addItem("Absent");
            combo->addItem("Absent from Commitee");
            combo->setProperty("MyIndex", x);
            combo->setCurrentIndex(status[x]);
 formLayout->addRow(keys.at(x),combo);
connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(roll(int)));

}
scroll->setWidget(wdg);
 wdg->setLayout(formLayout);

wdg->show();

Results in widget showing but no scrollbar or layout:

QWidget *wdg = new QWidget;
QScrollArea *scroll = new QScrollArea(wdg);
QFormLayout *formLayout = new QFormLayout();
int lenght = keys.size();
        for(int x=0; x<=lenght-1; x++)
{

            QComboBox* combo = new QComboBox;
            combo->addItem("Present");
            combo->addItem("Present and Voting");
            combo->addItem("Absent");
            combo->addItem("Absent from Commitee");
            combo->setProperty("MyIndex", x);
            combo->setCurrentIndex(status[x]);
 formLayout->addRow(keys.at(x),combo);
 connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(roll(int)));

}
scroll->setLayout(formLayout);


wdg->show();

Upvotes: 2

Views: 2056

Answers (1)

eyllanesc
eyllanesc

Reputation: 244301

You have to create a widget that contains the QFormLayout, and then set that widget to QScrollArea, and that QScrollArea you must set it in the widget wdg through another layout:

QWidget *wdg = new QWidget;
QScrollArea *scroll = new QScrollArea;

QWidget *content_widget = new QWidget;

QFormLayout *formLayout = new QFormLayout(content_widget);

QStringList items{"Present", "Present and Voting", "Absent", "Absent from Commitee"};

for(int x=0; x < keys.size(); x++)
{

    QComboBox *combo = new QComboBox;
    combo->addItems(items);
    combo->setProperty("MyIndex", x);
    combo->setCurrentIndex(status[x]);
    formLayout->addRow(keys.at(x),combo);
    connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(roll(int)));
}

scroll->setWidget(content_widget);
wdg->setLayout(new QVBoxLayout);
wdg->layout()->addWidget(scroll);
wdg->show();

Output:

enter image description here

in the following link there is an example.

Upvotes: 3

Related Questions