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. Detecting clicks on QTreeView icons.
Forum Updated to NodeBB v4.3 + New Features

Detecting clicks on QTreeView icons.

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 625 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.
  • S Offline
    S Offline
    summit
    wrote on last edited by
    #1

    I have a tree view which i am customizing using delegates to create icon.

    this is the Delegate class

    class PixelDelegate : public QStyledItemDelegate
    {
    	Q_OBJECT
    public:
    	using QStyledItemDelegate::QStyledItemDelegate;
    
    	void paint(QPainter *painter, const QStyleOptionViewItem &option,
    		const QModelIndex &index) const override;
    
    	QSize sizeHint(const QStyleOptionViewItem &option,
    		const QModelIndex &index) const override;
    
    	QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
    		const QModelIndex &index) const override;
    	void setEditorData(QWidget *editor, const QModelIndex &index) const override;
    	void setModelData(QWidget *editor, QAbstractItemModel *model,
    		const QModelIndex &index) const override;
    
    	void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option
    		, const QModelIndex &index) const override;
    
    };
    
    

    which i am adding to the tree view.

    PixelDelegate *delegate = new PixelDelegate(this); // For icons in tree view
    treeView->setItemDelegate(delegate);
    
    this is how the tree view looks.
    
    ![TreeView.png](https://ddgobkiprc33d.cloudfront.net/5d1e4bcb-394e-412a-a9d6-e44e8c043695.png) 
    

    How can i get the click on the icon of the tree item ?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi

      Do you draw the icon in "paint" yourself so you know the location ?
      If yes, you can use
      https://doc.qt.io/qt-5/qabstractitemdelegate.html#editorEvent

      as it also is called for mouse events so you should be able to know if it was pressed and do check if
      icons was hit.

      S 2 Replies Last reply
      1
      • mrjjM mrjj

        Hi

        Do you draw the icon in "paint" yourself so you know the location ?
        If yes, you can use
        https://doc.qt.io/qt-5/qabstractitemdelegate.html#editorEvent

        as it also is called for mouse events so you should be able to know if it was pressed and do check if
        icons was hit.

        S Offline
        S Offline
        summit
        wrote on last edited by
        #3

        @mrjj Yes i am drawing the icon in the paint event , thanks for the answer i will give it a try.

        1 Reply Last reply
        0
        • mrjjM mrjj

          Hi

          Do you draw the icon in "paint" yourself so you know the location ?
          If yes, you can use
          https://doc.qt.io/qt-5/qabstractitemdelegate.html#editorEvent

          as it also is called for mouse events so you should be able to know if it was pressed and do check if
          icons was hit.

          S Offline
          S Offline
          summit
          wrote on last edited by
          #4

          @mrjj I have implemented the function and i am getting the mouse clicks but not aware of the next process of how do we check for the icon being pressed.

          mrjjM 1 Reply Last reply
          0
          • S summit

            @mrjj I have implemented the function and i am getting the mouse clicks but not aware of the next process of how do we check for the icon being pressed.

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            @summit

            Hi
            You should store the location of the icon in some variable so we can use it later.
            When you paint it.
            like QRect iconRect. As class member. in .h
            Then set it in paint when you paint the icon.

            in EditorEvent
            the pos() of the event give the mouse point where we clicked.

            So check would be ( pseudo code )

            if (iconRect.contains( event->pos() )) ..we hit icon..

            You might need to cast the event to MouseEvent to have pos().

            S 1 Reply Last reply
            1
            • mrjjM mrjj

              @summit

              Hi
              You should store the location of the icon in some variable so we can use it later.
              When you paint it.
              like QRect iconRect. As class member. in .h
              Then set it in paint when you paint the icon.

              in EditorEvent
              the pos() of the event give the mouse point where we clicked.

              So check would be ( pseudo code )

              if (iconRect.contains( event->pos() )) ..we hit icon..

              You might need to cast the event to MouseEvent to have pos().

              S Offline
              S Offline
              summit
              wrote on last edited by
              #6

              @mrjj Thank you very much for your kind help.

              1 Reply Last reply
              1

              • Login

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