用户登录
用户注册

分享至

如果选择了多个项目,如何在 QListWidget 中打印多个项目

  • 作者: 1320383944
  • 来源: 51数据库
  • 2022-12-23

问题描述

我有 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())
软件
前端设计
程序设计
Java相关