Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do I make a changeable list of CheckBoxes?

How do I make a changeable list of CheckBoxes?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dugi
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      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().

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Dugi
        wrote on last edited by
        #3

        It works. Thanks for the quick reply.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved