How do I check the state of a QListWidget's item's checkBox?



  • Hello,
    I've been struggling with a problem about items of QListWidgets.
    The user can add new items and a checkBox will appear next to the item:

    void MainWindow::on_pushButton_add_clicked()
    {
        QListWidgetItem* new_item = new QListWidgetItem("New Item " + QString::number(ui->listWidget->count()+1), ui->listWidget); //new item
        new_item->setFlags(new_item->flags() | Qt::ItemIsUserCheckable); // set checkable flag
        new_item->setCheckState(Qt::Unchecked); // initialize check state as unchecked
    }
    

    Now I'd like to make the flow of a certain function dependent by the check-state of a specific item's checkBox.
    But I can't seem to find a function like isChecked() for it.
    I have tried a lot of methods but none of them seemed to work.

    if(ui->listWidget->childAt(c)->i == Qt::Checked)
    {
        //only execute this when the checkbox at index c is checked
    }
    

    I have tried using itemAt(0, c), index, and many more..

    Does anyone know a workaround?
    Thanks


  • Qt Champions 2016

    Hi

    bool ischeck=ui->listWidget->item(0)->checkState();

    (for row 0)



  • @mrjj said in How do I check the state of a QListWidget's item's checkBox?:

    bool ischeck=ui->listWidget->item(0)->checkState();

    This works! thank you


  • Qt Champions 2016

    Hi
    np:)
    If you need to check it when it happens you can use the
    QListWidget::itemChanged(QListWidgetItem* item) signal
    and check the items->checkState();


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.