简体   繁体   English

如何在 QListWidget 中按文本设置所选项目?

[英]How can I set selected item by text in QListWidget?

I am trying to create a function that opens the file location from its shortcut into my own file explorer and highlight the file from the file explorer.我正在尝试创建一个 function ,它将文件位置从其快捷方式打开到我自己的文件资源管理器中,并从文件资源管理器中突出显示文件。

def openFileLocation():
    self.Directory = os.path.dirname(self.favoritesListWidget.currentItem().data(QtCore.Qt.UserRole)[2])
    ProjectOSP.setCurrentIndex(1)
    self.fileExplorerListWidget.setCurrentItem(self.favoritesListWidget.currentItem().text())
    reloadListWidget()

I have solved it by using QtWidgets.QListWidget.findItems() method by using the MatchExactly flag and the string from my shortcut's name extracted from the path of that shortcut then reloading the fileExplorer then iterating through the results which should be one, and setting the current item to that exact item.我已经通过使用 QtWidgets.QListWidget.findItems() 方法解决了这个问题,方法是使用 MatchExactly 标志和从该快捷方式的路径中提取的快捷方式名称中的字符串,然后重新加载 fileExplorer,然后遍历应该是一个的结果,并设置当前项目到该确切项目。

def openFileLocation():
    data = self.favoritesListWidget.currentItem().data(QtCore.Qt.UserRole)
    self.Directory = os.path.dirname(data[2])
    ProjectOSP.setCurrentIndex(1)
    reloadListWidget()
    [self.fileExplorerListWidget.setCurrentItem(x) for x in self.fileExplorerListWidget.findItems(data[0]+'.'+data[1], QtCore.Qt.MatchExactly)]

You can find more about findItems here您可以在此处找到有关 findItems 的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM