QTableWidget Checkbox get state and location

北战南征 提交于 2019-12-08 12:33:20

I simply iterate over all cell widgets:

for (int i = 0; i < t->rowCount(); i++) {
    for (int j = 0; j < t->columnCount(); j++) {
        QWidget *pWidget = t->cellWidget(i, j);
        QCheckBox *checkbox = pWidget->findChild<QCheckBox *>();
        if (checkbox && checkbox->isChecked())
            qDebug() << t->horizontalHeaderItem(j)->text() << i;
    }
}

It's been awhile since I've programmed with Qt, but I believe there is not really good way of doing this. I've done all of these solutions with success.

1) Iterate through all the cell widgets like svlasov's answer says. This has some scalability issues.

2) Create hash maps where the pointer to the button is the key and the indices you want are the values. You can get which button was clicked with QObject::sender().

3) Store the indices you want as properties on the button when you create the buttons (see setProperty() in QObject's documentation). For example,

button->setProperty("x index", x);  

In your slot, use QObject::sender() to get the pointer to the button and then call

button->property("x");

I've generally found the third option to be the cleanest and best performing.

Note that these answers also work for QTreeWidgets and QListWidgets.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!