Solved Check state QListWidget
-
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. -
Sorry. My English is not enough good.
Also.
I have some items in my QListWidget.
All items are unchecked first.
For example, I will pick two items for this list and I change these items to "checked".
After this action, I want to block these picked items before being unchecked -
@naax
So in yourhighlightChecked(QListWidgetItem *Task)
slot, immediately afterUpdateData();
, maybe you wantTask->setFlags(Task->flags() & ~Qt::ItemIsUserCheckable);
?