2

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();
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Abhi Garg
  • 73
  • 7
  • Another thing I noticed is that even without a scrollarea whenever I try to increase the height of the widget it disappears and I cant get it to reappear without restarting the program. – Abhi Garg Mar 04 '18 at 19:41
  • Did you check this [post](https://stackoverflow.com/questions/49020491/qlabel-and-qpushbutton-align/49023510#49023510) .. it might help – Mohammad Kanan Mar 04 '18 at 19:48

1 Answers1

3

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.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thank you so much. Is there any reason this works with the MSVC compiler and not MINGW? – Abhi Garg Mar 04 '18 at 21:27
  • @AbhiGarg Do you get an error? What does it mean that it does not work? Do you have the same version of Qt for both compilers? – eyllanesc Mar 04 '18 at 23:27
  • It doesn't show an error it justs shows an outlined box inside the widget and no formlayout. I have the same version for both compilers – Abhi Garg Mar 05 '18 at 04:54
  • @AbhiGarg I think it's a bug, check if your installation is correct, I've tried it with mingw on Linux and Windows and it works fine. – eyllanesc Mar 05 '18 at 04:58