Insert a scrollbar in a qt widget using qtcreator

2019-08-08 20:00发布

问题:

I have created my own widget that I'm going to put into another widget from code. The form widget are created with qtCreator. I have arranged the "subwidgets" into a verticalLayout in order to put one under the other, but since the number of the inserted widget is not fixed I need to have a scrollbar. So I have placed my verticalLayout into a scrollArea, but the scrollbar never appears, why? the structure "father->child" is the following: formWidget->scrollArea->verticalLayout Thanks

EDIT: The problem is the following: I was creating the widget from a the clicked slot of a QPushButton: in the costructor of that widget, I was creating a QScrollArea and a VerticalLayout. After having been inserted all the widgets that I want in the layout, I added the layout to the scrollArea. This is the wrong thing: in order to make visible the scrollArea in the widget created from the button, is necessary to inserted that widget in the scrollbar, directly from the clicked slot code. For further detail, I attach the code of the clicked slot and the widget constructor

Button slot (clicked)

scrollArea= new QScrollArea;
scheduleWindow = new Schedule(traceFilePath);
scrollArea->setWidget(scheduleWindow);
scrollArea->resize(scheduleWindow->getWidth(), scheduleWindow->getHeight());
QRect rec = QApplication::desktop()->screenGeometry();
unsigned int desktopHeight = rec.height();
unsigned int desktopWidth = rec.width();
if(scheduleWindow->getWidth() > desktopWidth ||
   scheduleWindow->getHeight() > desktopHeight)
    scrollArea->showMaximized();
else
    scrollArea->show();

Widget constructor

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

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

    QString taskName;

    unsigned int nTasks = 0;
    TaskSchedule *t;
    for(std::list<QString>::iterator taskNameIter = parser.getTaskNames().begin();
        taskNameIter != parser.getTaskNames().end(); taskNameIter++)
    {
        taskName = *taskNameIter;
        nTasks++;
        cout<<taskName.toStdString()<<endl;
        t = new TaskSchedule(this , taskName, 80, arch.nCPU(), arch.maxTime(),
                                          parser.getExecList(taskName), parser.getTaskSimpleEventsMap(taskName));
        ui->pageLayout->addWidget(t);
    }
    cout<<nTasks<<endl;
    width = 2*t->getLineXPosStart() + t->getTickLength()*arch.maxTime();
    height = nTasks*(2*TASKSCH_Y_OFFSET + arch.nCPU()*t->getCpuHeight());
    ui->area->resize(width, height);
    ui->area->setMinimumSize(width, height);
    this->adjustSize();

} 

回答1:

You must place a scrollArea, just like you said. And then drag all the items you want to be "scrollable" inside that area. Once they are inside the scrollArea, you should select it and set a layout, vertical or horizontal. And then, when you resize the widget, the scrollBar appears when needed.

Could you please maybe ilustrate us with some pictures. So we could understand the problem? Seems like a graphical problem, so it's hard to guide you without something to look at...

EDIT: I believe in order to rearrange "subwidgets" in a layout, first you must define the parent of these "subwidgets" the layout itself.



回答2:

Are the items in the vertical layout exceeding the height/width limits so a scrollbar is needed? As far as I know the scrollbar only shows up once those dimensions are exceeded (you should be able to change this behavior though).