How do I make a changeable list of CheckBoxes?



  • I am quite new to Qt, I picked it when I needed to create a GUI for a program I need for my thesis. I need to create a list of options that the user would select or deselect, and its size would vary depending on configuration files. I have found "this very old topic":http://www.qtcentre.org/archive/index.php/t-8119.html explaining how to do it with CheckBoxes in a tabWidget, but either something changed during the last 7 years or I have done something wrongly, the latter being more probable. My attempt to reproduce the code crashes with a segmentation fault in QWidgetPrivate::reparentFocusWidgets(QWidget*), backtrace leads through QWidget::setParent(QWidget*, QFlagsQt::WindowType), QWidget::setParent(QWidget*) to QScrollArea::setWidget(QWidget*) in my code.

    My code:
    @ QWidget* checkBoxes = new QWidget(ui->table); // table is QListWidget in the ui created through QtDesigner
    QVBoxLayout* layout = new QVBoxLayout(checkBoxes);
    for (unsigned int i = 0; i < sampleSpectrum->data.size(); i++) { // tested that this circles through the data correctly
    QCheckBox* thisLine = new QCheckBox(checkBoxes);
    thisLine->setChecked(true);
    thisLine->setText(QString((*sampleSpectrum->data[i]).name.c_str())); // I have tested that this is a valid string
    thisLine->setGeometry(QRect(10, 10, 271, 21)); // copied this from an example Check Box I've drawn in QtDesigner
    layout->addWidget(thisLine);
    lines.push_back(thisLine); // this is a std::vector
    }
    ui->tableArea->setWidget(checkBoxes); // tableArea is QScrollArea defined in the ui created by QtDesigner@

    Any ideas?


  • Moderators

    try the code this way:
    @
    QWidget* checkBoxes = new QWidget();
    QVBoxLayout* layout = new QVBoxLayout;
    checkBoxes->setLayout(layout);
    for (unsigned int i = 0; i < sampleSpectrum->data.size(); i++)
    {
    QCheckBox* thisLine = new QCheckBox;
    thisLine->setChecked(true);
    thisLine->setText(QString((*sampleSpectrum->data[i]).name.c_str()));
    layout->addWidget(thisLine);
    lines.push_back(thisLine); // this is a std::vector
    }
    ui->tableArea->setWidget(checkBoxes);
    @
    this should work (not tested though)

    Basically the parenting is done automatically when you set a layout to a widget and also so by QScrollArea::setWidget().



  • It works. Thanks for the quick reply.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.