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. Catch the click on the icon of an QListview entry.
QtWS25 Last Chance

Catch the click on the icon of an QListview entry.

Scheduled Pinned Locked Moved Solved General and Desktop
qlistviewqicon
10 Posts 2 Posters 1.2k Views
  • 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.
  • H Offline
    H Offline
    Hans Hansen
    wrote on 3 Apr 2022, 10:31 last edited by
    #1

    Hi,
    I have an QListView, that works like expected.
    Before every entry an icon is shown. But how can I catch the click on Icon?
    With the clicked(const QModelIndex &index) signal of the view, I only get the line in the view/model.
    The itemDelegateForIndex call, don't deliver me an pointer to the QIcon of the line.
    So how can I do it?

    Thanks for any ideas.

    1 Reply Last reply
    0
    • H Offline
      H Offline
      Hans Hansen
      wrote on 3 Apr 2022, 15:24 last edited by
      #10

      Yes this was it. Using an TableView will to the job.

      1 Reply Last reply
      0
      • C Online
        C Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 3 Apr 2022, 10:46 last edited by
        #2

        The icon can be retrieved via QAbstractItemModel::data(index, Qt::DecorationRole).

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        1
        • H Offline
          H Offline
          Hans Hansen
          wrote on 3 Apr 2022, 11:47 last edited by
          #3

          This will be possible, but the QIcon class hast no function for an click state of it.
          The QIcon is set when the decoration role is requested in the data model.

          C 1 Reply Last reply 3 Apr 2022, 11:57
          0
          • H Hans Hansen
            3 Apr 2022, 11:47

            This will be possible, but the QIcon class hast no function for an click state of it.
            The QIcon is set when the decoration role is requested in the data model.

            C Online
            C Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 3 Apr 2022, 11:57 last edited by Christian Ehrlicher 4 Mar 2022, 11:58
            #4

            @Hans-Hansen said in Catch the click on the icon of an QListview entry.:

            This will be possible, but the QIcon class hast no function for an click state of it.

            What's wrong with the clicked() signal?
            If you want a special clicked on the icon (for whatever reason) you have to calculate t by yourself e.g. in a slot connected to clicked().

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0
            • H Offline
              H Offline
              Hans Hansen
              wrote on 3 Apr 2022, 12:11 last edited by
              #5

              I get only the index of model, but not if the signal was triggered by an click on the text part of the line or the QIcon.
              I think I need something like an rectangle and then check if the mouse was in it.

              C 1 Reply Last reply 3 Apr 2022, 12:47
              0
              • H Hans Hansen
                3 Apr 2022, 12:11

                I get only the index of model, but not if the signal was triggered by an click on the text part of the line or the QIcon.
                I think I need something like an rectangle and then check if the mouse was in it.

                C Online
                C Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 3 Apr 2022, 12:47 last edited by
                #6

                @Hans-Hansen said in Catch the click on the icon of an QListview entry.:

                I think I need something like an rectangle and then check if the mouse was in it.

                Correct. The mouse position is known, the rest can come from a customized QStyledItemDelegate where you can get the corresponding rects via QStyle::subElementRect().
                Still don't know why it's important to know where in the cell the click occurred.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                1
                • H Offline
                  H Offline
                  Hans Hansen
                  wrote on 3 Apr 2022, 13:13 last edited by
                  #7

                  The QStyledItemDelegate is accessible via itemDelegateForIndex from the view. But I can't see an subElement function for it:
                  https://doc.qt.io/qt-6/qstyleditemdelegate-members.html.
                  My idea is, that I have left of every text line in the view an cross icon. An when an click on the icon was done, then the line is removed form the model. The remove part itself is easy, only the code part for detecting if the click was on the text or the icon makes the problem.

                  C 1 Reply Last reply 3 Apr 2022, 13:29
                  0
                  • H Hans Hansen
                    3 Apr 2022, 13:13

                    The QStyledItemDelegate is accessible via itemDelegateForIndex from the view. But I can't see an subElement function for it:
                    https://doc.qt.io/qt-6/qstyleditemdelegate-members.html.
                    My idea is, that I have left of every text line in the view an cross icon. An when an click on the icon was done, then the line is removed form the model. The remove part itself is easy, only the code part for detecting if the click was on the text or the icon makes the problem.

                    C Online
                    C Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 3 Apr 2022, 13:29 last edited by
                    #8

                    @Hans-Hansen said in Catch the click on the icon of an QListview entry.:

                    But I can't see an subElement function for it:

                    Because there is non, read my post.

                    My idea is, that I have left of every text line in the view an cross icon.

                    Simply use a separate column

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    0
                    • H Offline
                      H Offline
                      Hans Hansen
                      wrote on 3 Apr 2022, 13:36 last edited by
                      #9

                      I will try it with in Tableview. One row for the icon an one for the text, then clicked(const QModelIndex &index) signal should give me automatic the "possiton".

                      1 Reply Last reply
                      0
                      • H Offline
                        H Offline
                        Hans Hansen
                        wrote on 3 Apr 2022, 15:24 last edited by
                        #10

                        Yes this was it. Using an TableView will to the job.

                        1 Reply Last reply
                        0

                        5/10

                        3 Apr 2022, 12:11

                        • Login

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