如果选择了多个项目,如何在 QListWidget 中打印多个项目 [英] How to print more than one items in QListWidget if more than one items selected

查看:27
本文介绍了如果选择了多个项目,如何在 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 中打印多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆