Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Detect clicked icon of item [SOLVED]
Forum Updated to NodeBB v4.3 + New Features

Detect clicked icon of item [SOLVED]

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 3.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kevin44115
    wrote on last edited by
    #1

    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)));@

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      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);
      

      }
      @

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kevin44115
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          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?

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kevin44115
            wrote on last edited by
            #5

            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 ?

            1 Reply Last reply
            0
            • raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              maybe i got something wrong with the globalPos in my mind.
              Please try without mapping:
              @
              if( iconRect.isValid() && iconRect.contains(me->pos()) )
              {...}
              @

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kevin44115
                wrote on last edited by
                #7

                Yes !!!! It's work fine !
                Thank you a lot !

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved