Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Qt Widget to scroll through widgets?

Tags:

c++

scroll

qt

I am currently working on a Qt project for my school. For this project I need to list an unknown number of elements in a window without resizing its content.

I've used some VBoxLayout in the past, but it isn't what I am searching at all. This widget resizes its content depending on the number of elements it contains. What I would like is to add as much widgets as I need into the "scrolling widget", which will stack next to each other automatically and won't resize.

I tried using QScrollArea but I wasn't able to make elements stack on each others.

Here is a small drawing explaining my problem: enter image description here

like image 945
Aymeric Avatar asked Apr 13 '11 09:04

Aymeric


1 Answers

Here is how I do it with a QVBoxLayout and a QScrollArea:

//scrollview so all items fit in window
    QScrollArea* techScroll = new QScrollArea(tabWidget);
    techScroll->setBackgroundRole(QPalette::Window);
    techScroll->setFrameShadow(QFrame::Plain);
    techScroll->setFrameShape(QFrame::NoFrame);
    techScroll->setWidgetResizable(true);

    //vertical box that contains all the checkboxes for the filters 
    QWidget* techArea = new QWidget(tabWidget);
    techArea->setObjectName("techarea");
    techArea->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
    techArea->setLayout(new QVBoxLayout(techArea));
    techScroll->setWidget(techArea);

Then when adding items you do it like this (with lay = techArea->layout() and parent = techarea:

for(std::set<Event::Enum>::iterator it = validEvents.begin(); it != validEvents.end();
    ++it){
        QCheckBox* chk = new QCheckBox(
        "text", parent);

        if(lay){
            lay->addWidget(chk);
        }   

    }
like image 75
RedX Avatar answered Sep 29 '22 12:09

RedX