0

I'm working on a small plugin for QGIS 3.12+ and regardless of what i try i'm unable to perform the simple task of retrieving the number of items in a QList that's returned by QListWidget.selectedItems()

I'm not well versed in python, but have read through the docs and tried each of the below with all of them failing.

What am i missing?

#Returns items as i can loop though them with: for item in selected:
selected = self.dlg.qListWidgetLayers.selectedItems()

#TypeError: count() takes exactly one argument (0 given)
#Docs claim there's a count that doesn't need the parameter...
intTotal = selected.count()

#AttributeError: 'list' object has no attribute 'size'
intTotal = selected.size()

#AttributeError: 'list' object has no attribute 'length'
intTotal = selected.length()

#AttributeError: 'list' object has no attribute 'len'
intTotal = selected.len() #<- This attempt is incorrect, should actually be len(selected)

Reahreic
  • 596
  • 2
  • 7
  • 26
  • Does this answer your question? [How do I get the number of elements in a list?](https://stackoverflow.com/questions/1712227/how-do-i-get-the-number-of-elements-in-a-list) – Jongware May 27 '20 at 18:37
  • Sadly not, I've already tested len() to no aval. (The last line in the example code) It's frustrating because the docs plainly identify that i should be able to just call count(), or length(), or evet size() Edit: On re-read it appears that when using len it's len(myList) and not myList.len(). Still doesn't explain why QList.Count() fails, but it's at least forward progress – Reahreic May 27 '20 at 18:53
  • Please read that link, `len` is not a *function* of `list`. You are trying to apply C++ documentation to Python, that's why you are failing. Read some Python documentation first – the [official tutorial](https://docs.python.org/3/tutorial/index.html) covers basic syntax. – Jongware May 27 '20 at 18:55
  • Just edited my comment to append myself re-reading, it'll keep forward momentum, but doesn't explain why the built-in QList features fail. (at least forward progress, if i could use C# for this tiny internal utility i'd be much happier) – Reahreic May 27 '20 at 18:59
  • As the error message says, the result you get is a standard Python list. ...I must admit I cannot find any concrete reference for that right away. – Jongware May 27 '20 at 19:55

0 Answers0