Detect clicked icon of item [SOLVED]
-
Hello,
I would like to detect when I click on icon of items in QTreeeView.
!http://img4.hostingpics.net/pics/404196Sanstitre.png!
In this "post":http://qt-project.org/forums/viewthread/15592 I see I need to reimplemente the editorEvent() but I don't know how to detect the icon clicked.
Can I detect it in this function ?
@connect(ui->treeViewBDD,SIGNAL(clicked(QModelIndex)),this,SLOT(f_IHM_Click_q(QModelIndex)));@ -
exactly ... you need to use the editorEvent() method.
in there do the following (assuming you use Qt4.x):
@
bool MyDelegate::editorEvent( QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index )
{
switch( event->type() )
{
case QEvent::MouseButtonPress:
{
QMouseEvent* me = static_cast<QMouseEvent*>(event);
if( me->button() == Qt::LeftButton )
{
QStyleOptionViewItemV4 opt = option;
initStyleOption(&opt, index);
const QWidget* widget = opt.widget;
QRect iconRect = widget->style()->subElementRect(QStyle::SE_ItemViewItemDecoration, opt, widget);if( iconRect.isValid() && iconRect.contains( widget->mapFromGlobal(me->globalPos()) ) ) { // click on icon ... } } } break; } return QStyledItemDelegate::editorEvent(event, model, option, index);
}
@ -
Tank you a lot.
But I have a problem (I use Qt 5.0.2)
@#pragma once
#include "qstyleditemdelegate.h"class CustomDelege : public QStyledItemDelegate
{
Q_OBJECT
public:
bool editorEvent( QEvent * , QAbstractItemModel *, const QStyleOptionViewItem & , const QModelIndex &);
};@@#include "CustomDelege.h"
#include "qevent.h"
#include <iostream>bool CustomDelege::editorEvent( QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index)
{
switch( event->type() )
{
case QEvent::MouseButtonPress:
{
QMouseEvent* me = static_cast<QMouseEvent*>(event);
if( me->button() == Qt::LeftButton )
{
QStyleOptionViewItemV4 opt = option;
initStyleOption(&opt, index);
const QWidget* widget = opt.widget;
QRect iconRect = widget->style()->subElementRect(QStyle::SE_ItemViewItemDecoration, qstyleoption_cast<const QStyleOptionViewItemV4 *>(&opt), widget);
if( iconRect.isValid() && iconRect.contains( widget->mapFromGlobal(me->globalPos()) ) )
{
std::string toto = "t";
// click on icon ...
}
}
}
break;
}
return QStyledItemDelegate::editorEvent(event, model, option, index);
}@@ui->treeViewBDD->setItemDelegate(new CustomDelege());@
It didn't enter in the case when I click on my icon.
-
please debug where it fails... add breakpoints or debug prints.
What's the value of iconRect? Is the mouse press event incoming?and btw. you do nothing but an assignment (std::string toto = "t";) in the if block. I hope this didn't let you assume that the code is not working?
-
Yes I am looking for debug where it fails,
the (std::string toto = âtâ;) it's just a test for see if it enter in the if.
When I add a breaking point in the if, and when I click in the icon,
iconRect = {x1=21 y1=1 x2=55 y2=14}
widget->mapFromGlobal(me->globalPos()) return the value x=39 and y=35
Then iconRect.isValid() return true but iconRect.contains( widget->mapFromGlobal(me->globalPos()) return false.The point is not in the iconRect. Any idea how to solve it ?
-
maybe i got something wrong with the globalPos in my mind.
Please try without mapping:
@
if( iconRect.isValid() && iconRect.contains(me->pos()) )
{...}
@ -
Yes !!!! It's work fine !
Thank you a lot !