Current Index always give 0
Unsolved
General and Desktop
-
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) } } }
-
Hi,
You already asked similar question. a lot of times. Modify the rowCount method and to return a valid number of rows.
-
@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 } }