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. Current Index always give 0
Forum Updated to NodeBB v4.3 + New Features

Current Index always give 0

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 515 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
    Subuday
    wrote on last edited by
    #1

    When I click mentorsList.current index always gives 0.

    #include "mentorlistmodel.h"
    
    MentorListModel::MentorListModel(QObject *parent):
        QAbstractListModel(parent)
    {
    
    }
    
    
    int MentorListModel::rowCount(const QModelIndex &parent) const
    {
        if (parent.isValid()) {
            return 0;
        }
    
        return m_data.size();
    }
    
    QVariant MentorListModel::data(const QModelIndex &index, int role) const
    {
        if (!index.isValid()) {
            return QVariant();
        }
    
        switch (role) {
        case IDRole:
            return m_data.at(index.row()).id;
        case NameRole:
            return m_data.at(index.row()).firstName + " " + m_data.at(index.row()).lastName;
        case AboutRole:
            return m_data.at(index.row()).about;
        case LangRole: {
            QString langs;
            QStringList::const_iterator it;
            for (it = m_data.at(index.row()).langs.begin(); it != m_data.at(index.row()).langs.end(); ++it) {
                langs += *it + " ";
            }
            return langs;
        }
        default:
            return QVariant();
        }
    }
    
    QHash<int, QByteArray> MentorListModel::roleNames() const
    {
        QHash<int, QByteArray> roles = QAbstractListModel::roleNames();
        roles[IDRole] = "userID";
        roles[NameRole] = "name";
        roles[AboutRole] = "about";
        roles[LangRole] = "langs";
    
        return roles;
    }
    
    void MentorListModel::add(const Mentor &unit)
    {
        beginInsertRows(QModelIndex(), m_data.size(), m_data.size());
        m_data.append(unit);
        endInsertRows();
    }
    
    void MentorListModel::clear()
    {
        beginRemoveRows(QModelIndex(), 0, m_data.size());
        endRemoveRows();
        m_data.clear();
    }
    
        Rectangle {
            id: mentors
            height: parent.height - searchField.height - 20
            width: searchField.width
            anchors {
               top: searchField.bottom
               left: parent.left
               topMargin: 8
               leftMargin: 8
            }
    
          ListView {
             id: mentorsList
             model: mentorListModel
             footer: footerAndHeader
             anchors.fill: parent
             spacing: 2
             cacheBuffer: 2
             clip: true
    
            delegate: Rectangle {
                 id: mentorInfo
                 height: 130
                 width: mentors.width
                 color: "#ffffff"
                 antialiasing: true
                 border {
                     width: 1
                     color: "#F9F9F9"
                 }
                 layer.enabled: true
                 layer.effect: DropShadow {
                     transparentBorder: true
                     horizontalOffset: 1
                     verticalOffset: 1
                     color: "#F2F2F2"
                 }
    
                 Image {
                     id: avatar
                     source: "image://avatar/" + userID
                     height: 80
                     width: 80
                     anchors {
                         top: parent.top
                         left: parent.left
                         topMargin: 20
                         leftMargin: 8
                     }
                     fillMode: Image.PreserveAspectCrop
                     layer.enabled: true
                     layer.effect: OpacityMask {
                         maskSource: mask
                     }
                 }
    
                 Rectangle {
                     id: mask
                     height: 80
                     width: 80
                     radius: 50
                     visible: false
                 }
    
                 Text {
                     id: nameTxt
                     text: name
                     font.family: "Roboto"
                     font.pointSize: 12
                     font.weight: Font.DemiBold
                     color: "#5caa15"
                     anchors {
                         top: parent.top
                         left: avatar.right
                         topMargin: 30
                         leftMargin: 8
                     }
                 }
    
                 Rectangle {
                     id: underline
                     height: 3
                     width: nameTxt.width
                     anchors {
                         top: nameTxt.bottom
                         left: avatar.right
                         leftMargin: 10
                     }
                     color: "#5caa15"
                     visible: false
                 }
    
                 Text {
                     id: aboutTxt
                     text: about
                     font.family: "Roboto"
                     font.pointSize: 10
                     anchors {
                         top: underline.bottom
                         left: avatar.right
                         topMargin: 8
                         leftMargin: 8
                     }
                 }
    
                 Rectangle {
                     height: 20
                     width: langsTxt.width
                     color: "#E6E6E6"
    
                     anchors {
                         top: aboutTxt.bottom
                         left: avatar.right
                         topMargin: 8
                         leftMargin: 8
                     }
    
                     Text {
                         id: langsTxt
                         text: langs
                         font.family: "Roboto"
                         font.pointSize: 10
                         font.weight: Font.ExtraLight
                         anchors.horizontalCenter: parent.horizontalCenters
                     }
                 }
                 
    
                 MouseArea {
                     anchors.fill: parent
                     hoverEnabled: true
                     cursorShape: Qt.PointingHandCursor
    
                     onEntered: underline.visible = true
    
                     onExited: underline.visible = false
    
                     onClicked: {
                         console.log(mentorsList.currentIndex)
                     }
                 }
              }
    
    KroMignonK 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi,

      You already asked similar question. a lot of times. Modify the rowCount method and to return a valid number of rows.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • F Offline
        F Offline
        flowery
        wrote on last edited by
        #3

        CurrentIndex won't get updated automatically .To update the same index should be assigned to currentIndex onClicked event.

        1 Reply Last reply
        0
        • S Subuday

          When I click mentorsList.current index always gives 0.

          #include "mentorlistmodel.h"
          
          MentorListModel::MentorListModel(QObject *parent):
              QAbstractListModel(parent)
          {
          
          }
          
          
          int MentorListModel::rowCount(const QModelIndex &parent) const
          {
              if (parent.isValid()) {
                  return 0;
              }
          
              return m_data.size();
          }
          
          QVariant MentorListModel::data(const QModelIndex &index, int role) const
          {
              if (!index.isValid()) {
                  return QVariant();
              }
          
              switch (role) {
              case IDRole:
                  return m_data.at(index.row()).id;
              case NameRole:
                  return m_data.at(index.row()).firstName + " " + m_data.at(index.row()).lastName;
              case AboutRole:
                  return m_data.at(index.row()).about;
              case LangRole: {
                  QString langs;
                  QStringList::const_iterator it;
                  for (it = m_data.at(index.row()).langs.begin(); it != m_data.at(index.row()).langs.end(); ++it) {
                      langs += *it + " ";
                  }
                  return langs;
              }
              default:
                  return QVariant();
              }
          }
          
          QHash<int, QByteArray> MentorListModel::roleNames() const
          {
              QHash<int, QByteArray> roles = QAbstractListModel::roleNames();
              roles[IDRole] = "userID";
              roles[NameRole] = "name";
              roles[AboutRole] = "about";
              roles[LangRole] = "langs";
          
              return roles;
          }
          
          void MentorListModel::add(const Mentor &unit)
          {
              beginInsertRows(QModelIndex(), m_data.size(), m_data.size());
              m_data.append(unit);
              endInsertRows();
          }
          
          void MentorListModel::clear()
          {
              beginRemoveRows(QModelIndex(), 0, m_data.size());
              endRemoveRows();
              m_data.clear();
          }
          
              Rectangle {
                  id: mentors
                  height: parent.height - searchField.height - 20
                  width: searchField.width
                  anchors {
                     top: searchField.bottom
                     left: parent.left
                     topMargin: 8
                     leftMargin: 8
                  }
          
                ListView {
                   id: mentorsList
                   model: mentorListModel
                   footer: footerAndHeader
                   anchors.fill: parent
                   spacing: 2
                   cacheBuffer: 2
                   clip: true
          
                  delegate: Rectangle {
                       id: mentorInfo
                       height: 130
                       width: mentors.width
                       color: "#ffffff"
                       antialiasing: true
                       border {
                           width: 1
                           color: "#F9F9F9"
                       }
                       layer.enabled: true
                       layer.effect: DropShadow {
                           transparentBorder: true
                           horizontalOffset: 1
                           verticalOffset: 1
                           color: "#F2F2F2"
                       }
          
                       Image {
                           id: avatar
                           source: "image://avatar/" + userID
                           height: 80
                           width: 80
                           anchors {
                               top: parent.top
                               left: parent.left
                               topMargin: 20
                               leftMargin: 8
                           }
                           fillMode: Image.PreserveAspectCrop
                           layer.enabled: true
                           layer.effect: OpacityMask {
                               maskSource: mask
                           }
                       }
          
                       Rectangle {
                           id: mask
                           height: 80
                           width: 80
                           radius: 50
                           visible: false
                       }
          
                       Text {
                           id: nameTxt
                           text: name
                           font.family: "Roboto"
                           font.pointSize: 12
                           font.weight: Font.DemiBold
                           color: "#5caa15"
                           anchors {
                               top: parent.top
                               left: avatar.right
                               topMargin: 30
                               leftMargin: 8
                           }
                       }
          
                       Rectangle {
                           id: underline
                           height: 3
                           width: nameTxt.width
                           anchors {
                               top: nameTxt.bottom
                               left: avatar.right
                               leftMargin: 10
                           }
                           color: "#5caa15"
                           visible: false
                       }
          
                       Text {
                           id: aboutTxt
                           text: about
                           font.family: "Roboto"
                           font.pointSize: 10
                           anchors {
                               top: underline.bottom
                               left: avatar.right
                               topMargin: 8
                               leftMargin: 8
                           }
                       }
          
                       Rectangle {
                           height: 20
                           width: langsTxt.width
                           color: "#E6E6E6"
          
                           anchors {
                               top: aboutTxt.bottom
                               left: avatar.right
                               topMargin: 8
                               leftMargin: 8
                           }
          
                           Text {
                               id: langsTxt
                               text: langs
                               font.family: "Roboto"
                               font.pointSize: 10
                               font.weight: Font.ExtraLight
                               anchors.horizontalCenter: parent.horizontalCenters
                           }
                       }
                       
          
                       MouseArea {
                           anchors.fill: parent
                           hoverEnabled: true
                           cursorShape: Qt.PointingHandCursor
          
                           onEntered: underline.visible = true
          
                           onExited: underline.visible = false
          
                           onClicked: {
                               console.log(mentorsList.currentIndex)
                           }
                       }
                    }
          
          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #4

          @Subuday said in Current Index always give 0:

          When I click mentorsList.current index always gives 0.

          As @SGaist already has told you, if you don't change currentIndex, his value will not change. And that is better!

          You have to change you onClicked code as follow:

          MouseArea {
              onClicked : {
                  console.log("Clicked on item number " + index) //
                  // select this item is ListView
                  mentorsList.currentIndex = index
              }
          }
          

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          4

          • Login

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