本文介紹了如果選擇了多個項目,如何在 QListWidget 中打印多個項目的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有 QListWidget 并且那里有字符串,當我選擇一個字符串時,我想顯示它的索引號和文本.但問題是,如果我選擇超過 1 個項目,它不會顯示所有索引.它只顯示一個.
I have QListWidget and there are strings there, when I select a string, I wanted to display the index number and text of that. But the problem is, if I select more than 1 items, it doesn't display all of the indexes. It displays only one.
from PyQt5.QtWidgets import *
import sys
class Pencere(QWidget):
def __init__(self):
super().__init__()
self.layout = QVBoxLayout(self)
self.listwidget = QListWidget(self)
self.listwidget.addItems(["Python","Ruby","Go","Perl"])
self.listwidget.setSelectionMode(QAbstractItemView.MultiSelection)
self.buton = QPushButton(self)
self.buton.setText("Ok")
self.buton.clicked.connect(self.but)
self.layout.addWidget(self.listwidget)
self.layout.addWidget(self.buton)
def but(self):
print (self.listwidget.currentRow()+1)
uygulama = QApplication(sys.argv)
pencere = Pencere()
pencere.show()
uygulama.exec_()
如果我選擇超過 1 個項目,如何顯示所有項目名稱和索引?
How can I display all of the items names and indexes if I select more than 1 items?
推薦答案
我用這個解決了
def but(self):
x = self.listwidget.selectedItems()
for y in x:
print (y.text())
這篇關于如果選擇了多個項目,如何在 QListWidget 中打印多個項目的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!