[Qt-interest] [ModelView] item delegate rendering a widget
Mildred Ki'Lya
ml.mildred593 at gmail.com
Wed Jul 8 12:28:17 CEST 2009
On 07/06/2009 12:54 PM, Martin Hofius wrote:
> Am Montag, 6. Juli 2009 schrieb Mildred Ki'Lya:
>
>> Hi,
>>
>> I want to be able to draw a QPushButton in every cell of a table, and
>> that button to be actually modifyable (have a two state: pressed and
>> released).
>>
>> It seems it's not that easy to do, and I found on the archives a
>> possible solution.
>>
>> The mail is from Nicolas Castagne, the 8th of December 2006.
>> The subject is: Re: [ModelView] item delegate rendering a widget
>> Somehow, the attached file with a possible solution can't be downloaded.
>>
>> http://lists.trolltech.com/qt-interest/2006-12/thread00210-0.html
>> (bottom of page)
>>
>> Do you know where to get it? Can someone who has it send it to me?
>> Thanks,
>>
> maybe the spreadsheet example from
> http://doc.trolltech.com/4.5/demos-spreadsheet.html
> can help you in a similar way?
>
> Greetings
> Martin
>
Thanks
But I needed a way to modify the widget without double clicking to enter
in edit mode
I tried a simple solution which seems to work (only with buttons though):
in the delagate, I painted the button according to the boolean state of
the cell:
void ItemDelegate::paint(
QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex & index ) const
{
...
if( IN_THE_RIGHT_CELL ) {
bool state = BOOLEAN_STATE_OF_THE_CELL;
QStyle *sty = QApplication::style();
QStyleOptionButton opts;
opts.direction = option.direction;
opts.fontMetrics = option.fontMetrics;
opts.palette = option.palette;
opts.rect = option.rect;
opts.state = QStyle::State_Enabled | option.state;
opts.state |= state ? QStyle::State_Sunken : QStyle::State_Raised;
opts.text = STRING_VALUE_OF_THE_BUTTON;
sty->drawControl(QStyle::CE_PushButton, &opts, painter, 0);
return;
}
... (call parent method)
}
I also activated in the QTableView:
viewport()->setAttribute(Qt::WA_Hover, true);
viewport()->setMouseTracking(true);
so the mouseOver events change the state of the button (notice how in
the paint() method, we reuse the option.state
And to modify the state of the button by clicking on it:
bool ItemDelegate::editorEvent (
QEvent *event,
QAbstractItemModel *model,
const QStyleOptionViewItem &option,
const QModelIndex &index)
{
...
if( IN_THE_RIGHT_CELL ) {
bool newval = ! BOOLEAN_STATE_OF_THE_CELL;
switch(event->type()){
default: break;
case QEvent::KeyPress: {
QKeyEvent *ev = static_cast<QKeyEvent*>(event);
if(!ev) break;
if(ev->key() == Qt::Key_Space) {
model->setData(index, newval);
return true;
}
}
case QEvent::MouseButtonRelease:
model->setData(index, newval);
return true;
case QEvent::MouseButtonDblClick:
return true;
}
}
... (call parent method)
}
We take the following events (the others are discarded and passed to the
parent class):
* The key press (space key only): change the state with the keyboard
* The mouse button release to change the state
* the double click event (and we do nothing) to prevent going in edit
mode (createEditor() called ... we don't want that)
I hope this can help someone else.
As I said, I had to reimplement the events to change the widget state.
So I don't think it would work beyond very simple things.
Mildred
--
Mildred Ki'Lya
╭───────── mildred593@online.fr ──────────
│ Jabber, GoogleTalk: <mildred at jabber.fr>
│ Website: <http://ki.lya.online.fr> GPG ID: 9A7D 2E2B
│ Fingerprint: 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 9A7D 2E2B
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: OpenPGP digital signature
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090708/dc5fe2b0/attachment.bin
More information about the Qt-interest-old
mailing list