Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Click on an element in GridView and invoke a function

Click on an element in GridView and invoke a function

Scheduled Pinned Locked Moved Solved QML and Qt Quick
gridviewonclickedqabstractitemmo
2 Posts 2 Posters 2.3k 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.
  • M Offline
    M Offline
    MoaMoaK
    wrote on 14 Aug 2017, 11:35 last edited by
    #1

    Hi everyone,
    I have a class (PLModel) subclassing QAbstractItemModel and passed to a QML GridView through the context. The display works fine. The problem is that i want to be able to click on one of those item and invoke a function that acts differently according to the element that has been clicked.
    Here is the code i've come up with so far :

    plmodel.hpp (simplified)

    class PLModel : public QAbstractItemModel
    {
    public :
        // Constructor
        PLModel()
    
       // The functions to subclass QAbstractItemModel
       // index, parent, rowCount, columnCount, data, hasChildren, setData, flags, roleNames
    
       // The method I want to invoke
       Q_INVOKABLE virtual void activateItem( const QModelIndex &index ) Q_DECL_OVERRIDE;
    
        // Other irrelevant stuff
        ...
    };
    

    plmodel.cpp (simplified)

    PLModel::PLModel() { }
    // The imlementation of the functions to subclass QAbstractItemModel
    
    // The method I want to invoke
    void PLModel::activateItem( const QModelIndex &index )
    {
        // For debug
        log_debug (index.row())
        log_debug (index.column())
    
        // Do some other stuff according to the selected item
        // item = getItem ( index )
        ...
    }
    

    ui.qml

    import QtQuick 2.0
    
    Item {
        width: 1000
        height: 1000
    
        GridView {
            id: gridView
            anchors.fill: parent
            cellWidth: 150
            cellHeight: 150
            model: m        // This variable comes from context (= instance of PLModel)
            delegate: Item {
                x: 5
                height: 50
                Image {
                    anchors.horizontalCenter: parent.horizontalCenter
                    id: image
                    width: 100
                    height: 100
                    source: model.image
                    MouseArea {
                        anchors.fill: image
                        onClicked: m.displayInfo(index)   // Where i call the function
                    }
                }
            }
    
        }
    }
    

    Then when i click on an element, the function is called correctly but the 'index' is not correct : index.row = -1 & index.column = -1 (not matter which element i pick). Thus i can't decide what to do because i don't know which element was clicked. I don't understand why i get this index. Am i doing something wrong ? Is there another way to do what i want to do ?

    Thx,
    MoaMoaK

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 14 Aug 2017, 11:56 last edited by
      #2

      Please do not double-post. I'm closing this one.

      (Z(:^

      1 Reply Last reply
      1

      2/2

      14 Aug 2017, 11:56

      • Login

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