item delegates: fix clicking on tristate checkboxes.

QCheckBox cycles through the 3 states, but item delegates didn't
do that.

Change-Id: Iad1e464341033ca357925fe8064f53bb584459f4
Reviewed-by: Thorbjørn Lund Martsum <tmartsum@gmail.com>
Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com>
This commit is contained in:
David Faure 2013-08-28 21:09:18 +02:00 committed by The Qt Project
parent 0a3eb0fe44
commit 8f4b6f1cd1
3 changed files with 61 additions and 4 deletions

View File

@ -1295,8 +1295,11 @@ bool QItemDelegate::editorEvent(QEvent *event,
return false;
}
Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked
? Qt::Unchecked : Qt::Checked);
Qt::CheckState state = static_cast<Qt::CheckState>(value.toInt());
if (flags & Qt::ItemIsTristate)
state = ((Qt::CheckState)((state + 1) % 3));
else
state = (state == Qt::Checked) ? Qt::Unchecked : Qt::Checked;
return model->setData(index, state, Qt::CheckStateRole);
}

View File

@ -764,8 +764,11 @@ bool QStyledItemDelegate::editorEvent(QEvent *event,
return false;
}
Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked
? Qt::Unchecked : Qt::Checked);
Qt::CheckState state = static_cast<Qt::CheckState>(value.toInt());
if (flags & Qt::ItemIsTristate)
state = ((Qt::CheckState)((state + 1) % 3));
else
state = (state == Qt::Checked) ? Qt::Unchecked : Qt::Checked;
return model->setData(index, state, Qt::CheckStateRole);
}

View File

@ -1198,6 +1198,57 @@ void tst_QItemDelegate::editorEvent_data()
<< (int)(Qt::LeftButton)
<< true
<< (int)(Qt::Unchecked);
QTest::newRow("unchecked, tristate, release")
<< QRect(0, 0, 20, 20)
<< QString("foo")
<< (int)(Qt::Unchecked)
<< (int)(Qt::ItemIsEditable
|Qt::ItemIsSelectable
|Qt::ItemIsUserCheckable
|Qt::ItemIsTristate
|Qt::ItemIsEnabled
|Qt::ItemIsDragEnabled
|Qt::ItemIsDropEnabled)
<< true
<< (int)(QEvent::MouseButtonRelease)
<< (int)(Qt::LeftButton)
<< true
<< (int)(Qt::PartiallyChecked);
QTest::newRow("partially checked, tristate, release")
<< QRect(0, 0, 20, 20)
<< QString("foo")
<< (int)(Qt::PartiallyChecked)
<< (int)(Qt::ItemIsEditable
|Qt::ItemIsSelectable
|Qt::ItemIsUserCheckable
|Qt::ItemIsTristate
|Qt::ItemIsEnabled
|Qt::ItemIsDragEnabled
|Qt::ItemIsDropEnabled)
<< true
<< (int)(QEvent::MouseButtonRelease)
<< (int)(Qt::LeftButton)
<< true
<< (int)(Qt::Checked);
QTest::newRow("checked, tristate, release")
<< QRect(0, 0, 20, 20)
<< QString("foo")
<< (int)(Qt::Checked)
<< (int)(Qt::ItemIsEditable
|Qt::ItemIsSelectable
|Qt::ItemIsUserCheckable
|Qt::ItemIsTristate
|Qt::ItemIsEnabled
|Qt::ItemIsDragEnabled
|Qt::ItemIsDropEnabled)
<< true
<< (int)(QEvent::MouseButtonRelease)
<< (int)(Qt::LeftButton)
<< true
<< (int)(Qt::Unchecked);
}
void tst_QItemDelegate::editorEvent()