Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Getting the signal when a checkbox is toggled in a QtableWidget



  • Hello everyone!

    I am struggling with getting the signal when a checkbox of an item is checked in QTableWidget. I created the checkboxes this way:
    for (int i=0; i<rowcount ; i++)
    { QTableWidgetItem *item = new QTableWidgetItem();
    ui->tablewidget->setItem(i,2,item);
    item->setCheckState(Qt::Unchecked);}

    the problem is when ever a checkbox is checked in this QtableWidget i need to store the state of this checkbox . I also have a QListWidget containing a number of items and when ever i select an item in this QList the checkboxes in the QtableWidget need to be updated. So every item in the QlistWidget have different states of the checkboxes according to the user choices.

    I was searching for the solution on my own for a long time, but because I did not find anything suitable I am writing my question here.

    Thank you for your help!


  • Lifetime Qt Champion

    Hi and welcome to the forums.

    The QListWidget/QTableWidget::itemChanged is triggered when the checked state is change so you can
    get it like you would for a text change .

    // This hook signal up to a lambda slot an changed color of item if checked or not. 
    QObject::connect(ui->listWidget, &QListWidget::itemChanged, this, [](QListWidgetItem * item) {
            if (item->checkState() == Qt::Checked)
                item->setBackgroundColor(QColor("#ffffb2"));//yellow
            else
                item->setBackgroundColor(QColor("#ffffff"));
        });
    

    That said. What does the QList and QtableWidget have in common item wise ?
    Im asking as using the View version of List and Table actually allows to share a model between them
    so if one sets check on some item, it will also show checked in the other. automatically.
    However, it requires that the data can arranged in the model so it makes sense to share it.



  • @mrjj said in Getting the signal when a checkbox is toggled in a QtableWidget:

    Im asking as using the View version of List and Table actually allows to share a model between them
    so if one sets check on some item, it will also show checked in the other. automatically.

    Ooohhh! :)

    But I don't quite get why you'd use it? By the time you can select/check something in the TableView anyway, why do you also want a ListView to select/check as well?


Log in to reply