一下代码纯手打,不能保证每个单词敲对。希望对你有帮助!
1,QML鼠标点击事件
MouseArea {
anchors.fill: parent;
onClicked: {
console.log(“seach by fist char char = “, ch);
//点击调用C++借口
listview.positionviewAtIndex(cpp_business.getFistIndex(ch), listView.Beginning);
}
}
2,C++实现getFistIndex()接口
(1)getFistIndex()声明
/*********************************************
文 件 名:mainWindow.h
描 述 : 主窗口
*********************************************/
Q_INVOKABLE unsigned int getFirstIndex(QString ch);
(2)getFistIndex()实现
/*********************************************
文 件 名:mainWindow.cpp
描 述 : 主窗口
*********************************************/
unsigned int MainWindow::getFirstIndex(QString ch)
{
DEBUG_PARAM(“ch:”, ch);
return (m_contactListModel->getSearchResultIndex(ch)-1);
}
(3)getSearchResult所在头文件
/*********************************************
文 件 名:phonebooklistitem.h
描 述 : 电话簿列表
*********************************************/
//声明
unsigned int getSearchResultIndex(QString ch);
//私有变量
private:
QList m_contactItems;
(4)getSearchResult所在源文件
/*********************************************
文 件 名:phonebooklistitem.pp
描 述 : 电话簿列表
*********************************************/
//实现部分
unsigned int PhonebookListModel:getSearchResult(QString ch)
{
for(unsigned int i = 0; i < m_contactItems .count(); i++)
{
if(ch == m_contactItems .at(i).nameGroup()) //判断首字母
{
return i;
break;
}
}
}
QVariant PhonebookListModel::data(const QModelIndex & index, int role) const
{
if (index.row() < 0 || index.row() >= m_contactItems.count())
return QVariant();
const PhonebookListItem &contactItem = m_contactItems[index.row()];
if(role == IDRole)
return contactItem.ID();
else if (role == NameRole)
return contactItem.name();
else if (role == NumberRole)
return contactItem.number();
else if (role == TypeRole)
return contactItem.type();
else if (role == NameSZMRole)
return contactItem.nameSZM();
else if (role == NameGroupRole)
return contactItem.nameGroup();
else
return QVariant();
}
QHash

被折叠的 条评论
为什么被折叠?



