当光标进入单元格时,您可以覆盖 eventFilter 方法以突出显示您的复选框。http://qt-project.org/doc/qt-4.8/qobject.html#eventFilter
bool ExampleDialog::eventFilter(QObject *obj, QEvent *event)
{
if (qobject_cast<QTableWidgetItem*>(object)) {
if (event->type() == QEvent::HoverEnter) {
QCheckBox* qcb = object->findChild<QCheckBox*>();
//here you can do something to highlight your checkbox
return true;
}
else if(event->type() == QEvent::HoverLeave){
QCheckBox* qcb = object->findChild<QCheckBox*>();
//here you have to stop highlighting checkbox
return true;
}
else {
return false;
}
}
}
覆盖后,您必须像这样在小部件上安装事件过滤器
widget->installEventFilter(this); //this points to parent of the widget, the exampleDialog
并使用 QTableWidgetItem* 的单击槽来设置您的复选框。
void QTableWidgetItemClicked( int row, int column )
{
QCheckBox* qcb = QObject::sender()->findChild<QCheckBox*>();
qcb->setChecked(!qcb->isChecked());
}
请记住将 QTableWidgetItems 信号连接到插槽。