Solved Check state QListWidget
-
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);
? -
I tried this but my program crash.
if(Task->checkState() == Qt::Checked) { qDebug() << "Checked"; Euro+=100.0; UpdateData(); Task->setFlags(Task->flags() & ~Qt::ItemIsUserCheckable); } else qDebug() << " Else"; The program has unexpectedly finished.
-
@naax
It should not be from the line you added. Whenever you get a "crash", run yourprogrammer[EDIT: that would be: program :-; ] in the Debugger, it will break on the crash, look at the "stack trace" to trace back to the exact line of your code which caused the fault. -
@JonB said in Check state QListWidget:
run your programmer in the Debugger
You would need a debug build of that programmer :-D
-
@jsulm
:) Indeed so, corrected, but in this case that might have been a good idea ;-) -
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); } } void MainWindow::highlightChecked(QListWidgetItem *item) { if(item->checkState() == Qt::Checked) { qDebug() << "Checked"; Euro+=100.0; UpdateData(); ui->listWidget->item(0)->flags() & ~Qt::ItemIsSelectable; } else { qDebug() << " Else"; } } void MainWindow::createConnections() { QObject::connect(ui->listWidget, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(highlightChecked(QListWidgetItem*))); }
The program is not crashing anymore, but this not working.
ui->listWidget->item(0)->flags() & ~Qt::ItemIsSelectable;
I can still check/uncheck any items.
-
@naax said in Check state QListWidget:
ui->listWidget->item(0)->flags() & ~Qt::ItemIsSelectable;
Because that statement will not set any flags on the item.
You are just applying an and operation on the temporary value returned by the call to
flags()
. -
@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)
?). -
if(item->checkState() == Qt::Checked) { qDebug() << "Checked"; Euro+=100.5; UpdateData(); //ui->listWidget->item(0)->setFlags(ui->listWidget->item(0)->flags() & ~Qt::ItemIsSelectable); ui->listWidget->blockSignals(true); //THIS LINE HELPED ME item->setFlags(item->flags() & ~Qt::ItemIsUserCheckable); ui->listWidget->blockSignals(false);//THIS LINE HELPED ME