Solved Check state QListWidget
-
Hello guys.
I have created some checkboxes but this is inelastic more. So I created QListWidget with flags
QListWidgetItem* item = 0; for(int i = 0; i < ui->listWidget->count(); ++i){ item = ui->listWidget->item(i); item->setFlags(item->flags() | Qt::ItemIsUserCheckable); item->setCheckState(Qt::Unchecked); item->setTextAlignment(Qt::AlignCenter); }
but I don't know how I can check isChecked() for created items.
I tried this:if(item->checkState() == Qt::Checked) { qDebug() << "test"; Euro+=100; }
but is happing nothing.
-
@naax said in Check state QListWidget:
ui->listWidget->item(0)->flags() & ~Qt::ItemIsSelectable;
I typed exactly what you want:
Task->setFlags(Task->flags() & ~Qt::ItemIsUserCheckable);
Apply that to your
ui->listWidget->item(0)
if that is what you want to do (I doubt that is right, why would you want to apply to specifically/onlyitem(0)
?). -
@naax
Where do you do this? What isitem
pointing to? Have you checked the item before you call it? -
void MainWindow::ListWidgetSettings() { QListWidgetItem* item = 0; for(int i = 0; i < ui->listWidget->count(); ++i){ item = ui->listWidget->item(i); item->setFlags(item->flags() | Qt::ItemIsUserCheckable); item->setCheckState(Qt::Unchecked); item->setTextAlignment(Qt::AlignCenter); if((item)->checkState() == Qt::Checked) { qDebug() << "test"; Euro+=100; } } }
-
And why should return item->checkState() Qt::Checked when you set it to Qt::Unchecked two lines before?
-
I used it for new items. If I create a new item should be unchecked first.
-
@naax This does not answer my question.
-
All items are unchecked. After "check" I want to do something like with normal checkboxes.
if (item.isChecked()) { //code }
I used this:
item->checkState() Qt::Checked
because I don't know how I can do it right
-
@naax said in Check state QListWidget:
After "check" I want to do something like with normal checkboxes.
Then write a function which is called when you e.g. press on a 'Save' button or something else and read out the states.
-
Hi,
Connect a slot to the QListWidget::itemChanged signal and act there.
-
Can you show me how I can do it?
-
connect(ui->listWidget, &QListWidget::itemChanged, this, &MainWindow::onItemChanged);
Add the corresponding slot to your MainWindow class and do whatever you need in there.
-
@SGaist said in Check state QListWidget:
void MainWindow::ListWidgetSettings() { for(int i = 0; i < ui->listWidget->count(); ++i){ Task = ui->listWidget->item(i); Task->setFlags(Task->flags() | Qt::ItemIsUserCheckable); Task->setCheckState(Qt::PartiallyChecked); Task->setTextAlignment(Qt::AlignCenter); } } void MainWindow::highlightChecked(QListWidgetItem *Task) { if(Task->checkState() == Qt::Checked) { qDebug() << "Checked"; Euro+=100; UpdateData(); } else qDebug() << " Else"; } void MainWindow::createConnections() { connect(ui->listWidget, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(highlightChecked(QListWidgetItem*))); }
Its works, but only for the last item in my QListWidget
-
What does
UpdateData()
do ? -
This function update "Euro" label.
QString str; ui->ShowTotalEuro->setText(str.asprintf("%.1f", Euro));
-
@naax said in Check state QListWidget:
Its works, but only for the last item in my QListWidget
This should not be the case. How do you know it is?
-
Ok, guys, I got this.
I forget change it :
//connect(ui->listWidget, &QListWidget::itemChanged, this, &MainWindow::on_pushButton_clicked);
into this
connect(ui->listWidget, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(highlightChecked(QListWidgetItem*)));
Do you know how I can "block" Task? I mean if I set one task to "checked" and after this, I cant uncheck this?
Before when I used normal QCheckbox I used "setEnabled(false), but here I can't do it or I just don't know-how. -
@naax said in Check state QListWidget:
setEnabled(false)
disables a checkbox. Since you useditem->setFlags(item->flags() | Qt::ItemIsUserCheckable);
to allow user to check/uncheck the checkbox here, I imagine you needitem->setFlags(item->flags() & ~Qt::ItemIsUserCheckable);
? -
if I use this, I can't "check" items. And I want this after "check".
item->setFlags(item->flags() & ~Qt::ItemIsUserCheckable);
void MainWindow::ListWidgetSettings() { for(int i = 0; i < ui->listWidget->count(); ++i){ Task = ui->listWidget->item(i); Task->setFlags(Task->flags() | Qt::ItemIsUserCheckable); Task->setCheckState(Qt::PartiallyChecked); Task->setTextAlignment(Qt::AlignCenter); } } void MainWindow::highlightChecked(QListWidgetItem *Task) { if(Task->checkState() == Qt::Checked) { qDebug() << "Checked"; Euro+=100.0; UpdateData(); } else qDebug() << " Else"; }
-
@naax said in Check state QListWidget:
if I use this, I can't "check" items.
You asked for:
Do you know how I can "block" Task? I mean if I set one task to "checked" and after this, I cant uncheck this?
Before when I used normal QCheckbox I used "setEnabled(false), but here I can't do it or I just don't know-how.
If you went
setEnabled(false)
on a particular checkbox, perhaps after it was checked(?), then it seems to me this would do the same thing? Perhaps someone else understands what you seek.... -
To add to @JonB: you can change the flags anytime you need. So I would say, for example in
highlightChecked
after the item has been changed.