qt - QStyledItemDelegate: how to make checkbox button to change its state on click -
i have delegate mydelegate
used qlistwidget
. delegate derived qstyleditemdelegate
. 1 of goals of mydelegate
place checkbox button on each row of listwidget
. done within paint()
event of mydelegate
:
void mydelegate::paint(qpainter *painter, const qstyleoptionviewitem &option, const qmodelindex &index) const { qstyleditemdelegate::paint(painter, option, index); // ... drawing other delegate elements qstyleoptionbutton checkbox; // setting checkbox's size , position // draw checkbox qapplication::style()->drawcontrol(qstyle::ce_checkbox, &checkbox, painter); }
at first thought checkbox automatically change state on click, since specified qstyle::ce_checkbox
. not case. looks have specify checkbox visual behavior manually.
data-wise, when user clicks on checkbox, signal emitted , scene data changed. perform action in editorevent()
:
bool mydelegate::editorevent(qevent *event, qabstractitemmodel *model, const qstyleoptionviewitem &option, const qmodelindex &index) { if (event->type() == qevent::mousebuttonrelease) { if (/* within checkbox area */) emit this->clickedcheckbox(index); } }
the backend part works. however, cannot figure out how make checkbox button change visual state checked unchecked, , backwards.
i realized can change checkbox state manually doing paint()
:
checkbox.state = somecondition? (qstyle::state_enabled | qstyle::state_on) : (qstyle::state_enabled | qstyle::state_off) ;
qstyle::state_on/off
trick of manual checkbox state change.
but not know how set somecondition
, should set up. tried introduce private bool
variable set in editorevent()
when checkbox area gets click, however, not produce desired behavior: sets other checkboxes of listwidget
same visual state. so, behaved global condition checkboxes.
i feel like, accomplish task, have re-implement button , make change checkbox state on click. i'm lost on way , not sure how approach problem. qstyleoptionbutton
api not see clicked()
or other method use.
so, question is: how make checkbox behave checkbox, visually? if need re-implement checkbox, class inherit?
you can set value describes checkbox state in mydelegate::editorevent
, use paint proper checkbox:
const int check_role = qt::userrole + 1; bool mydelegate::editorevent(qevent *event, qabstractitemmodel *model, const qstyleoptionviewitem &option, const qmodelindex &index) { if (event->type() == qevent::mousebuttonrelease) { bool value = index.data(check_role).tobool(); // invert checkbox state model->setdata(index, !value, check_role); return true; } return qstyleditemdelegate::editorevent(event, model, option, index); } void mydelegate::paint(qpainter *painter, const qstyleoptionviewitem &option, const qmodelindex &index) const { qstyleoptionbutton cbopt; cbopt.rect = option.rect; bool ischecked = index.data(check_role).tobool(); if (ischecked) { cbopt.state |= qstyle::state_on; } else { cbopt.state |= qstyle::state_off; } qapplication::style()->drawcontrol(qstyle::ce_checkbox, &cbopt, painter); }
Comments
Post a Comment