PyQt5 QListView 高亮顯示某一條目的案例
正在做的項(xiàng)目遇到的問(wèn)題 , 在縮略圖列表中選擇某一圖,同時(shí)關(guān)聯(lián)到圖片list里高亮顯示這個(gè)圖片名字。
一開始我直接用setCurrentIndex(int) 來(lái)設(shè)置
if msg == "CAM1_Label_1": self.showCamOnTopScreen(0) self.device_listView.setCurrentIndex(0)
結(jié)果報(bào)錯(cuò),提示
“setCurrentIndex(self, QModelIndex): argument 1 has unexpected type 'int'”
后來(lái)發(fā)現(xiàn)此處不能直接用int , 而應(yīng)該跟用初始化時(shí)的model.index() 來(lái)設(shè)置。
修改如下:
if msg == "CAM1_Label_1": self.showCamOnTopScreen(0) idx = self.devicelistModel.index(0) self.device_listView.setCurrentIndex(idx)
補(bǔ)充:pyqt5 Qlistiew指定index顯示
要求:
根據(jù)實(shí)驗(yàn)步驟, 指定顯示當(dāng)前的流程在哪個(gè)步驟。記錄一下
# QListView使用 from PyQt5.QtWidgets import QMessageBox, QListView, QStatusBar, QMenuBar, QMenu, QAction, QLineEdit, QStyle, \ QFormLayout, QVBoxLayout, QWidget, QApplication, QHBoxLayout, QPushButton, QMainWindow, QGridLayout, QLabel from PyQt5.QtGui import QIcon, QPixmap, QStandardItem, QStandardItemModel from PyQt5.QtCore import QStringListModel, QAbstractListModel, QModelIndex, QSize import sys class WindowClass(QMainWindow): def __init__(self, parent=None): super(WindowClass, self).__init__(parent) self.layout = QVBoxLayout() self.resize(200, 300) listModel = QStringListModel() listView = QListView() items = ["step0", "step1", "step2", "step3"] listModel.setStringList(items) listView.setModel(listModel) # 修改index的參數(shù) ,即可指定當(dāng)前的那個(gè)索引被選中 listViewindex = listModel.index(1) listView.setCurrentIndex(listViewindex) listView.clicked.connect(self.checkItem) self.layout.addWidget(listView) widget = QWidget() widget.setLayout(self.layout) self.setCentralWidget(widget) def checkItem(self, index): QMessageBox.information(self, "ListView", "選擇項(xiàng)是:%d" % (index.row())) if __name__ == "__main__": app = QApplication(sys.argv) win = WindowClass() win.show() sys.exit(app.exec_())
在listViewindex = listModel.index(3)
和在listViewindex = listModel.index(1)
的情況下 的情況下
要注意判斷輸入的index的范圍,
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
玩轉(zhuǎn)python爬蟲之URLError異常處理
這篇文章主要介紹了python爬蟲的URLError異常處理,詳細(xì)探尋一下URL\HTTP異常處理的相關(guān)內(nèi)容,通過(guò)一些具體的實(shí)例來(lái)分析一下,非常的簡(jiǎn)單,但是卻很實(shí)用,感興趣的小伙伴們可以參考一下2016-02-02python實(shí)現(xiàn)微信小程序用戶登錄、模板推送
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)微信小程序用戶登錄、模板推送,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08python Elasticsearch索引建立和數(shù)據(jù)的上傳詳解
在本篇文章里小編給大家整理的是關(guān)于基于python的Elasticsearch索引的建立和數(shù)據(jù)的上傳的知識(shí)點(diǎn)內(nèi)容,需要的朋友們參考下。2019-08-08Python 3中print函數(shù)的使用方法總結(jié)
這篇文章主要給大家總結(jié)介紹了關(guān)于Python 3中print函數(shù)的使用方法,python3中的print函數(shù)和之前版本的用法相差很多,本文通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-08-08python發(fā)送郵件示例(支持中文郵件標(biāo)題)
python發(fā)送中文郵件示例,支持中文郵件標(biāo)題和中文郵件內(nèi)容。支持多附件。根據(jù)用戶名推測(cè)郵件服務(wù)器提供商2014-02-02python 基于卡方值分箱算法的實(shí)現(xiàn)示例
這篇文章主要介紹了python 基于卡方值分箱算法的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07python代碼檢查工具pylint 讓你的python更規(guī)范
遇到一個(gè)新的問(wèn)題,總是離不開3W原則(What,Why,hoW),下面是對(duì)python代碼靜態(tài)檢測(cè)工具pylint的學(xué)習(xí)2012-09-09Python+Pytest實(shí)現(xiàn)壓力測(cè)試詳解
在現(xiàn)代Web應(yīng)用程序中,性能是至關(guān)重要的。為了確保應(yīng)用程序能夠在高負(fù)載下正常運(yùn)行,我們需要進(jìn)行性能測(cè)試。本文就來(lái)用Pytest進(jìn)行壓力測(cè)試,希望對(duì)大家有所幫助2023-03-03