QTableWidgetItem中checkBox的判断问题

在QTableWidget中会经常用的checkBox,但是如何判断复选框是否被选中呢,方法是利用QTableWidget::cellChanged()函数,检查单元格内容的变化,然后连接此信号,在槽函数中检测checkBox的状态,代码如下:

QTableWidget *tableWidget = new QTableWidget;
QTableWidgetItem *firstColumn = new QTableWidgetItem(tr("test"));
firstColumn->setCheckState(Qt::Checked);//加入复选框
connect(tableWidget, SIGNAL(cellChanged(int,int)), this, SLOT(changeTest(int, int)));
void changeTest(int row, int col)
{
      if(tableWidget ->item(row, col)->checkState() == Qt::Checked)
          ...
      else
          ...
}

如果需要的话,可以通过判断信号发出者防止死循环。

This entry was posted in Qt, 原创 and tagged , , . Bookmark the permalink.

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">