c++ - QAbstractItemDelegate, editorEvent, CE_PushButton and item selection -
i'm having problem selection of delegated item represents 2 text lines , ce_pushbutton. want have row selected clicking button too. here paint function code:
void columntwolinesdelegate::paint( qpainter *painter, const qstyleoptionviewitem &option, const qmodelindex &index ) const { if (!index.isvalid()) return; if (option.state & qstyle::state_selected) { painter->setpen(qpen(qt::white)); if (option.state & qstyle::state_active) { painter->setbrush(qbrush(qpalette().highlight())); } else { painter->setbrush(qbrush(qpalette().color(qpalette::inactive, qpalette::highlight))); } painter->drawrect(option.rect); } else { painter->setpen(qpen(qt::black)); } qapplication::style()->drawprimitive(qstyle::pe_panelitemviewitem, &option, painter, 0); // ...it draws 2 text lines. painter->restore(); qrect tbuttonrect; tbuttonrect.setx(option.rect.width() - khorizzontalmarginsize); tbuttonrect.sety(option.rect.y() + (option.rect.height() / 2 - kbuttonsize / 2)); tbuttonrect.setwidth(kbuttonsize); tbuttonrect.setheight(kbuttonsize); qstyleoptionbutton tbutton; if (index.model()->data(index.model()->index(index.row(), 5)).toint() == 1) { tbutton.icon = marchive; } else { tbutton.icon = mreactive; } tbutton.iconsize = qsize(kbuttonsize - 2, kbuttonsize - 2); tbutton.rect = tbuttonrect; tbutton.features |= qstyleoptionbutton::flat; if (mcurrentrow == index.row()) { tbutton.state = qstyle::state_sunken | qstyle::state_enabled; } else { tbutton.state = qstyle::state_raised | qstyle::state_enabled; } qapplication::style()->drawcontrol(qstyle::ce_pushbutton, &tbutton, painter); }
and editorevent function code:
bool columntwolinesdelegate::editorevent( qevent *event, qabstractitemmodel *model, const qstyleoptionviewitem &option, const qmodelindex &index) { q_unused(model); if (event->type() != qevent::mousebuttonpress && event->type() != qevent::mousebuttonrelease) { return true; } // rect represents pushbutton qrect tbuttonrect; tbuttonrect.setx(option.rect.width() - khorizzontalmarginsize); tbuttonrect.sety(option.rect.y() + (option.rect.height() / 2 - kbuttonsize / 2)); tbuttonrect.setwidth(kbuttonsize); tbuttonrect.setheight(kbuttonsize); qmouseevent* mouseevent = static_cast<qmouseevent*>(event); // if mouse position outside button rect/button area use state_raised style if (!tbuttonrect.contains(mouseevent->pos())) { // allows row's selection return false; } if (event->type() == qevent::mousebuttonpress) { mcurrentrow = index.row(); } else if (event->type() == qevent::mousebuttonrelease) { mcurrentrow = -1; // when mousebuttonrelease emit signal clicked() emit documentrequest(index); } return true; }
only returning false allows row selection, i'd obtain same behavior clicking button. possible? in advance, danilo
Comments
Post a Comment