12

I have a QListWidget which displays a list of names using PyQt in Python. How can I get the QListWidgetItem for a given name?

For example, if I have the following QListWidget with 4 items, how can I get the item which contains text = dan?

QListWidget

panofish
  • 7,578
  • 13
  • 55
  • 96

3 Answers3

19

The python equivalent to vahancho's answer:

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() 
panofish
  • 7,578
  • 13
  • 55
  • 96
  • Thanks, this has helped me. – answerSeeker Feb 21 '17 at 03:48
  • 2
    Qt.MatchExactly is in QtCore, so QtCore.Qt.MatchExactly (if one has not imported all of QtCore) – nspo Aug 12 '19 at 15:35
  • Hello may I ask for the opposite of `Qt.MatchExactly`? like for example you are finding "danny" but still there is "dan" on it so it would be still valid... I think Qt has it but I don't know how tho.. have you just in case appliedi t? Thanks – Ice Bear Dec 30 '20 at 15:23
7

You can use QListWidget::findItems() function. For example:

QList<QListWidgetItem *> items = listWidget->findItems("dan", Qt::MatchExactly);
if (items.size() > 0) {
    // An item found
}
secretformula
  • 6,414
  • 3
  • 33
  • 56
vahancho
  • 20,808
  • 3
  • 47
  • 55
  • 1
    @panofish post a new answer if you are going to add python logic. This is vahancho answer you should not edit in things that aren't in line with the OP's meaning which he was only posting the example above. Just make a new answer :) – secretformula Jun 26 '14 at 14:59
  • 1
    I am the original poster and I was asking for python logic. I am thankful to vahancho for guiding me in the right direction, but python is what I was looking for. If I post a new answer, then I would have to mark my own answer as correct and take away for vahanchos good work. – panofish Jun 26 '14 at 15:01
  • 1
    @panofish thats the way SO works, its not appropriate to edit someone elses answer beyond their intent. Vahancho would need to make that edit himself. See [this](http://meta.stackoverflow.com/a/261224/897794). Why not just upvote if the answer was **useful**. – secretformula Jun 26 '14 at 15:06
  • @ secretformula, OK, thanks for educating me. That clears it up. I will post another answer. Thanks – panofish Jun 26 '14 at 15:10
  • I down-voted this answer as it's not python code. Some of us have absolutely no idea how to code in c++. – answerSeeker Feb 21 '17 at 03:42
3
items = self.listWidgetName.findItems("dan",Qt.MatchContains);

This works best while working with QListWidget item

Marcello B.
  • 4,177
  • 11
  • 45
  • 65
Atri Dave
  • 152
  • 2
  • 9