@VRonin
Doing a few (rather simple) tests, I found the following (which confirms your suspicion)
Works: (but no check box) - this is expected to work
// create item
QListWidgetItem* item = new QListWidgetItem(name, ui->listDriveInfo);
// item->setText(name);
// item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
// item->setCheckState(enabled ? Qt::Checked : Qt::Unchecked);
// add item to list
ui->listDriveInfo->addItem(item);
this crashes on setFlags() call
// create item
QListWidgetItem* item = new QListWidgetItem(name, ui->listDriveInfo);
// item->setText(name);
//set checkable and state
Qt::ItemFlags localFlags = item->flags();
item->setFlags(localFlags | Qt::ItemIsUserCheckable);
item->setCheckState(enabled ? Qt::Checked : Qt::Unchecked);
// add item to list
ui->listDriveInfo->addItem(item);
but these 2 both work (without setting owning QListWidget)
// create item
QListWidgetItem* item = new QListWidgetItem(name);
// item->setText(name);
//set checkable and state
Qt::ItemFlags localFlags = item->flags();
item->setFlags(localFlags | Qt::ItemIsUserCheckable);
item->setCheckState(enabled ? Qt::Checked : Qt::Unchecked);
// add item to list
ui->listDriveInfo->addItem(item);
and
// create item
QListWidgetItem* item = new QListWidgetItem();
// item->setText(name);
//set checkable and state
Qt::ItemFlags localFlags = item->flags();
item->setFlags(localFlags | Qt::ItemIsUserCheckable);
item->setCheckState(enabled ? Qt::Checked : Qt::Unchecked);
// add item to list
ui->listDriveInfo->addItem(item);
Testing:
I create a new application, did exactly the same (add a string and a checkbox) and that work 100%. Create a form, throw on a
QListWidget and run it with this code.
QListWidgetItem* item = new QListWidgetItem("New", ui->listWidget);
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
item->setCheckState(Qt::Unchecked);
QListWidgetItem* item1 = new QListWidgetItem("New 1", ui->listWidget);
item1->setFlags(item->flags() | Qt::ItemIsUserCheckable);
item1->setCheckState(Qt::Unchecked);
QListWidgetItem* item2 = new QListWidgetItem("New 2", ui->listWidget);
item2->setFlags(item->flags() | Qt::ItemIsUserCheckable);
item2->setCheckState(Qt::Unchecked);
ui->listWidget->addItem(item);
ui->listWidget->addItem(item1);
ui->listWidget->addItem(item2);
connect(ui->listWidget, &QListWidget::itemClicked, this, [this](QListWidgetItem * i) {
// Get drive info from driveList
int pos = ui->listWidget->row(i);
// check state & set state
bool active = i->checkState() == Qt::Checked;
qDebug() << (active ? "enabled" : "disabled");
});
So my question is, bug (on whose side) or some setting on my side? Thoughts on what to look for?