PyQt學習隨筆:ListView控件獲取當前選擇項的方法


通過currentIndex()可以獲取listView控件的當前選擇元素,如果選擇了多個,則可以通過selectedIndexes()來獲取選擇的元素,不過這兩個函數返回的是元素數據,而不是索引編號,如果要取到索引順序號,需要通過row()函數。

案例(m_ListView為listView控件名,displaySelected為一個TextBrowser控件名,itemmodel為列表項的數據存儲名):

    def DisplayItem(self):
        selected = self.m_ListView.selectedIndexes()
        for i in selected:
            item = i.row()
            inf = f"Pos:{item + 1},data: {self.itemmodel.stringList()[item]}"
            self.displaySelected.setText(inf)
       

也可以這樣:

    def DisplayItem(self):
        selected = self.m_ListView.currentIndex()
        item = selected .row()
            inf = f"Pos:{item + 1},data: {self.itemmodel.stringList()[item]}"
            self.displaySelected.setText(inf)
       

老猿Python,跟老猿學Python!
博客地址:https://blog.csdn.net/LaoYuanPython

老猿Python博客文章目錄:https://blog.csdn.net/LaoYuanPython/article/details/98245036
請大家多多支持,點贊、評論和加關注!謝謝!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM