iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >PyQt5 QListView 高亮显示某一条目的案例
  • 220
分享到

PyQt5 QListView 高亮显示某一条目的案例

2024-04-02 19:04:59 220人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

正在做的项目遇到的问题 , 在缩略图列表中选择某一图,同时关联到图片list里高亮显示这个图片名字。 一开始我直接用setCurrentIndex(int) 来设置 if msg

正在做的项目遇到的问题 , 在缩略图列表中选择某一图,同时关联到图片list里高亮显示这个图片名字。

一开始我直接用setCurrentIndex(int) 来设置


if msg == "CAM1_Label_1":
  self.showCamOnTopScreen(0)
  self.device_listView.setCurrentIndex(0)

结果报错,提示

“setCurrentIndex(self, QModelIndex): argument 1 has unexpected type 'int'”

后来发现此处不能直接用int , 而应该跟用初始化时的model.index() 来设置。

修改如下:


if msg == "CAM1_Label_1":
  self.showCamOnTopScreen(0)
  idx = self.devicelistModel.index(0)
  self.device_listView.setCurrentIndex(idx)

补充:pyqt5 Qlistiew指定index显示

要求:

根据实验步骤, 指定显示当前的流程在哪个步骤。记录一下


# 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的参数 ,即可指定当前的那个索引被选中
    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", "选择项是:%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的范围,

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。如有错误或未考虑完全的地方,望不吝赐教。

--结束END--

本文标题: PyQt5 QListView 高亮显示某一条目的案例

本文链接: https://www.lsjlt.com/news/121871.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作