Daniel
Daniel

Reputation: 151

QT scrollbar does not appear when the size of the content exceeds the window size

I'm working with qt, and I'm creating dynamically the objects of the following pictures (each timeline is a widget). The structure is that I'm adding this widget to a verticalLayout, that is the widget contained by the scrollArea. But unfortunately the scrollbar does not appear when instead it should be present: enter image description here If I increase the size of the window, the content is shown correctly: enter image description here But since the number of timelines created inside the window can be greater than the screen size, I need a scrollbar. What could be the problem? that

EDIT: Some source code, the constructor of the main window: some code is not present because it is created with QTCreator

Schedule::Schedule(QString pathname, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Schedule)
{
    ui->setupUi(this);
    ui->scrollArea->setLayout(ui->pageLayout);

    traceParser parser(pathname);
    parser.readJson();
    ArchitectureParameter arch = parser.getArchParam();

    QString taskName;


    for(std::list<QString>::iterator taskNameIter = parser.getTaskNames().begin();
        taskNameIter != parser.getTaskNames().end(); taskNameIter++)
    {
        taskName = *taskNameIter;
        TaskSchedule *t = new TaskSchedule(this , taskName, 80, arch.nCPU(), arch.maxTime(),
                                          parser.getExecList(taskName), parser.getTaskSimpleEventsMap(taskName));
        t->resize(600, t->height());
        t->resize(600, t->width());
        ui->pageLayout->addWidget(t);

    }

}

Upvotes: 1

Views: 1295

Answers (1)

Alexander Tyapkov
Alexander Tyapkov

Reputation: 5017

Probably this happens because you set the layout on the scrollArea. Here is the fast snippet which works for me:

QWidget* testWidget = new QWidget;
QVBoxLayout* layout = new QVBoxLayout;
QStringList strings;
strings << "asdfasd" << "asdffdfd" << "asdvvsdf" << "asdfccasdf";
Q_FOREACH(QString string, strings){
    TagButton* btn = new TagButton();
    btn->setText(string);
    layout->addWidget(btn);
}
testWidget->setLayout(layout);

QScrollArea* scrollArea = new QScrollArea;
scrollArea->setWidget(testWidget);

scrollArea->show();

Notice that I am setting the layout on the testWidget and then setWidget on scrollArea

Upvotes: 1

Related Questions