2014-06-26 135 views
6

我有一個QListWidget,它使用PyQt在Python中顯示一個名稱列表。我如何獲得給定名稱的QListWidgetItem如何按名稱獲取QListWidget項目?

例如,如果我有以下QListWidget 4個項目,我如何獲得包含文本=丹的項目?

QListWidget

回答

9

蟒蛇相當於vahancho的回答是:

items = self.listWidgetName.findItems("dan",Qt.MatchExactly) 

if len(items) > 0: 

    for item in items: 
     print "row number of found item =",self.listWidgetName.row(item) 
     print "text of found item =",item.text() 
+0

謝謝,這對我有幫助。 – answerSeeker

5

可以使用QListWidget::findItems()功能。例如:

QList<QListWidgetItem *> items = listWidget->findItems("dan", Qt::MatchExactly); 
if (items.size() > 0) { 
    // An item found 
} 
+1

工作@panofish發佈一個新的答案,如果您要添加,而Python邏輯。這是vahancho的答案,你不應該編輯與OP的含義不一致的東西,他只是發佈上面的例子。只是做一個新的答案:) – secretformula

+0

我是原始的海報,我要求的是Python邏輯。我很感謝vhanhancho引導我朝着正確的方向發展,但是python就是我一直在尋找的東西。如果我發佈了一個新答案,那麼我將不得不將自己的答案標記爲正確,併爲vahanchos做好工作。 – panofish

+1

@panofish多數民衆贊成在SO的工作方式,它不適合編輯別人超出他們的意圖的答案。 Vahancho需要自己編輯。請參閱[this](http://meta.stackoverflow.com/a/261224/897794)。爲什麼不只是upvote,如果答案是**有用**。 – secretformula

0
items = self.listWidgetName.findItems("dan",Qt.MatchContains); 

這個工作最好與QListWidget項目