PDA

View Full Version : QItemDelegate enter event



bunjee
18th April 2008, 20:20
The following doesn't work with a QItemDelegate:

bool ZeChatServer_delegate::editorEvent(QEvent *event,
QAbstractItemModel *model,
const QStyleOptionViewItem &option,
const QModelIndex &index)
{
if (event->type() == QEvent::Enter)
{
std::cout << "Tac tac\n";

return true;
}
return false;
}

How am I supposed to proceed to catch the enter / leave event of a QItemDelegate ?
Thanks.

wysota
18th April 2008, 20:42
You should check for QStyle::State_MouseOver flag in QStyleOptionViewItem and set the WA_Hover attribute for the viewport to receive paint requests for hover events.

bunjee
18th April 2008, 21:14
I did that :



mListView->viewport()->setAttribute(Qt::WA_Hover, true);
mListView->viewport()->setMouseTracking(true);

void ZeChatServer_delegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QItemDelegate::paint(painter, option, index);

if (option.state == QStyle::State_MouseOver)
{
std::cout << "Tac tac\n";
}
}

Not working so far.

Edit :
Solved :

if (option.state & QStyle::State_MouseOver)

bunjee
3rd May 2008, 02:08
Ok now I have a new problem:

How am I supposed to know when the cursor has left the Item ?
I have a problem with my button, it stays sunken when the cursor goes out of the item while clicking.

bunjee
3rd May 2008, 14:44
I did some research and found this :

void QAbstractItemView::entered ( const QModelIndex & index )

unfortunately there is no "left" signal.
Anyone ?

bunjee
3rd May 2008, 19:38
According to JPN:

For a QTableView you would connect to signals like:
void QAbstractItemView::doubleClicked ( const QModelIndex & index )
void QAbstractItemView::entered ( const QModelIndex & index )

I don't see any particular signal for leaving an item. But of course you can save row/col or a persistent index for the previously colourized cell and uncolourize during the entered signal of another item and possibly leave event of the whole table view.

I guess that's the direction I'll move forward to. Even if I find it unconvenient.

wysota
4th May 2008, 20:56
If you are only after colouring the item, why not do it like this?

#include <QtGui>

class Delegate : public QItemDelegate {
public:
Delegate(QObject *parent=0) : QItemDelegate(parent){}
void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const {
if(option.state & QStyle::State_MouseOver){
painter->fillRect(option.rect, Qt::red);
}
QItemDelegate::paint(painter, option, index);
}
};

int main(int argc, char **argv){
QApplication app(argc, argv);
QTableView tv;
tv.viewport()->setAttribute(Qt::WA_Hover);
QStandardItemModel model(8,8);
tv.setModel(&model);
tv.setItemDelegate(new Delegate(&tv));
tv.show();
return app.exec();
}