python - 如何在python中使用Pyqt5获取qlistwidget中项目的行号

标签 python pyqt5 row-number

如何获取 QlistWidget 中每个项目的行号我能够获取项目的文本而不是行号。

预期结果:

  • 当前行 ==> 1
  • checkpath 显示路径 => C:/Users/test/Downloads\cv.docx

我不知道如何返回行号我尝试了索引但它返回了错误的结果。

我认为这是必须更改的行:

                print("current row ==> {} \n checkpath  Displayed Path => {}".format(index,self.fullPath))

索引用于枚举

代码:

def checkPath(self,folder):         # Funtion to check the given path for the wanted extension (Files)

        try:
            directory=folder

            whichChecked=""
            for root,dirs,files in os.walk(directory):

                for index,filename in enumerate(files):
                    if len(self.lineEdit_Ext.text())>0:
                        self.lineEdit_Ext.setStyleSheet("background-color:white")
                        self.lineEdit_Ext.setPlaceholderText("Enter The Filetype Extention Here")

                        if filename.endswith(self.lineEdit_Ext.text()):
                            fullPath=os.path.join(root,filename)
                            print(fullPath)
                            self.fileList.append(fullPath)

                    elif self.rdBtn_docx.isChecked() and filename.endswith("docx") or filename.endswith("doc") :
                        self.fullPath=os.path.join(root,filename)

                        index = +1


                        print("current row ==> {} \n checkpath  Displayed Path => {}".format(index,self.fullPath))

                        print("=========================================")
                        self.fileList.append(self.fullPath)

                        whichChecked="docx - doc Ext was Selected"

                    if len(self.fileList) > 0:
                        self.lineEdit_Ext.setStyleSheet("bacground-color:white;")
                        self.lineEdit_Ext.setPlaceholderText("{0}".format(whichChecked))
                    else:
                        self.lineEdit_Ext.setStyleSheet("background-color:Red")
                        self.lineEdit_Ext.setPlaceholderText("No Ext is Specified")                            


            self.ListFilesInViewer(self.fileList)           # add the list into the  listWidgetPDFlist 


            return folder

        except Exception as e:
            print("this error occure {0}".format(e))

最佳答案

QListWidget.currentRow()

Return type: PySide.QtCore.int

This property holds the row of the current item..

. . .

def FileListSelected(self): 
    """ Function to select the desired file from the list in the left pane """

    itemNumber = self.listWidgetPDFlist.currentRow()           # +++
    Item = self.listWidgetPDFlist.currentItem().text()
    print("this is the SELECTED file==>{}".format(Item))
    print("this is the itemNumber   ==>{}".format(itemNumber)) # +++

. . .

enter image description here

关于python - 如何在python中使用Pyqt5获取qlistwidget中项目的行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51205071/

相关文章:

python - 删除与另一个列表中的任何条目匹配的列表条目

python - 在 pandas 数据帧上使用 boolean 过滤器时出现 KeyError

python - 在 PyQtGraph 中移除/删除图例

mysql - 连接两个表并删除重复项

python - 对包含字符串和数字的 ndarray 进行 Numpy 运算

python - celery 停止执行链

python - 将 QSlider 与上面包含 QPixmap 的 QLabel 对齐

python - 调整大小后 QRubberBand 在 QGraphicsView 上移动

sql - 需要窗口函数来对空值和非空值进行排序

sql - ORACLE SQL 在没有子查询或派生表的情况下检索 n 行