문제

wordI have a vector of strings and I need to assign each string to a check box. I am trying to insert the check boxes into a scroll area. I have a pre-made scroll bar on my main UI that is named scroll bar. How do I represent each string in my vector as a check box in my scroll area?

Currently I have:

for(auto word: words){  ///words is a vector of words

    //My attempt to dynamicaly create a check box
    QCheckBox *checkbox = new QCheckBox( QString::fromStdString(word); 
    this->ui->scrollArea->setWidget(checkbox);
}

For some reason this code will only add a single check box with the word to the scroll area.

PS if there is another widget that would be easier to use other then a scroll bar I can use that instead as long as I have the ability to scroll.

도움이 되었습니까?

해결책

I would personally use a container widget as follows:

QWidget container;
QVBoxLayout* containerLayout = new QVBoxLayout();
container.setLayout(containerLayout);
ui->scrollArea->setWidget(container);
for(auto word: words){
    QCheckBox *checkbox = new QCheckBox(QString::fromStdString(word));
    containerLayout->addWidget(checkbox);
}

Note that your original code is syntactically incorrect. I added the missing closing bracket.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top