簡體   English   中英

如何在python中使用Pyqt5在qlistwidget中獲取項目的行號

[英]how to get the row number of an item in the qlistwidget using Pyqt5 in python

如何獲取QlistWidget中每個項目的行號,從而能夠獲取項目的文本,但不能獲取行號。

預期結果:

  • 當前行==> 1
  • checkpath顯示的路徑=> C:/用戶/測試/下載\\ cv.docx

我不知道如何返回行號,但嘗試返回索引,但返回錯誤的結果。

我認為這是必須更改的行:

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

其中index用於表示enumarate

碼:

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()

返回類型:PySide.QtCore.int

此屬性保存當前項目的行。

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)) # +++

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM