PyQt4: How do you iterate all items in a QListWidget
Currently, I use the following while loop in a class that inherits QtGui.QListWidget to iterate all items:
i = 0
while i < self.count():
item = self.item(i)
i += 1
I was hoping I could use:
for item in self.items():
but the items() method wants a QMimeData object which I don't know how to construct to return all items. Is there a cleaner approach than my while loop above?
python pyqt4 qlistwidget
add a comment |
Currently, I use the following while loop in a class that inherits QtGui.QListWidget to iterate all items:
i = 0
while i < self.count():
item = self.item(i)
i += 1
I was hoping I could use:
for item in self.items():
but the items() method wants a QMimeData object which I don't know how to construct to return all items. Is there a cleaner approach than my while loop above?
python pyqt4 qlistwidget
add a comment |
Currently, I use the following while loop in a class that inherits QtGui.QListWidget to iterate all items:
i = 0
while i < self.count():
item = self.item(i)
i += 1
I was hoping I could use:
for item in self.items():
but the items() method wants a QMimeData object which I don't know how to construct to return all items. Is there a cleaner approach than my while loop above?
python pyqt4 qlistwidget
Currently, I use the following while loop in a class that inherits QtGui.QListWidget to iterate all items:
i = 0
while i < self.count():
item = self.item(i)
i += 1
I was hoping I could use:
for item in self.items():
but the items() method wants a QMimeData object which I don't know how to construct to return all items. Is there a cleaner approach than my while loop above?
python pyqt4 qlistwidget
python pyqt4 qlistwidget
asked Jan 7 '11 at 19:45
Michael A. JacksonMichael A. Jackson
2,78132540
2,78132540
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
I don't know what's it with the MIME type either, and I couldn't find a convenience method either. But you could write a simple method like this and be done:
def iterAllItems(self):
for i in range(self.count()):
yield self.item(i)
It's even lazy (a generator).
Thanks! What are your thoughts on using xrange here.
– Michael A. Jackson
Jan 7 '11 at 21:10
2
@majgis: My all means, yes. In Python 2, use xrange whenever possible. It's just that I'm usually using Python 3, so I get tend to write Python 3 in my examples.
– user395760
Jan 7 '11 at 21:14
add a comment |
Just to add my 2 cents, as I was looking for this:
itemsTextList = [str(listWidget.item(i).text()) for i in range(listWidget.count())]
add a comment |
I know this is old but, I just found out a function findItems(text, Qt.MatchFlags) in QListWidget. So, to iterate all items:
#listWidget is a QListWidget full of items
all_items = listWidget.findItems('', QtCore.Qt.MatchRegExp)
for item in all_items:
print item
And do whatever you need with the item =)
2
You can also find the other Qt.MatchFlag parameters here: pyqt.sourceforge.net/Docs/PyQt4/qt.html#MatchFlag-enum
– J. Saw
Oct 19 '18 at 1:12
add a comment |
items =
for index in xrange(self.listWidget.count()):
items.append(self.listWidget.item(index))
add a comment |
Just as a note for others landing here seeking the same information about PyQt5, it's slightly different here.
As user Pythonic stated above for PyQt4, you can still directly index an item using:
item_at_index_n = list_widget.item(n)
But to acquire a list of items for iteration, using an empty string and the MatchRegExp flag doesn't seem to work any more.
One way to do this in PyQt5 is:
all_items = list_widget.findItems('*', PyQt5.Qt.MatchWildcard)
I'm still getting to grips with PyQt, so there may well be an easier / simpler / more elegant way that I simply haven't come across yet. But I hope this helps!
add a comment |
items =
for index in range(self.listWidget.count()):
items.append(self.listWidget.item(index))
If your wish to get the text of the items
for item in items:
print(item.text())
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4629584%2fpyqt4-how-do-you-iterate-all-items-in-a-qlistwidget%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
I don't know what's it with the MIME type either, and I couldn't find a convenience method either. But you could write a simple method like this and be done:
def iterAllItems(self):
for i in range(self.count()):
yield self.item(i)
It's even lazy (a generator).
Thanks! What are your thoughts on using xrange here.
– Michael A. Jackson
Jan 7 '11 at 21:10
2
@majgis: My all means, yes. In Python 2, use xrange whenever possible. It's just that I'm usually using Python 3, so I get tend to write Python 3 in my examples.
– user395760
Jan 7 '11 at 21:14
add a comment |
I don't know what's it with the MIME type either, and I couldn't find a convenience method either. But you could write a simple method like this and be done:
def iterAllItems(self):
for i in range(self.count()):
yield self.item(i)
It's even lazy (a generator).
Thanks! What are your thoughts on using xrange here.
– Michael A. Jackson
Jan 7 '11 at 21:10
2
@majgis: My all means, yes. In Python 2, use xrange whenever possible. It's just that I'm usually using Python 3, so I get tend to write Python 3 in my examples.
– user395760
Jan 7 '11 at 21:14
add a comment |
I don't know what's it with the MIME type either, and I couldn't find a convenience method either. But you could write a simple method like this and be done:
def iterAllItems(self):
for i in range(self.count()):
yield self.item(i)
It's even lazy (a generator).
I don't know what's it with the MIME type either, and I couldn't find a convenience method either. But you could write a simple method like this and be done:
def iterAllItems(self):
for i in range(self.count()):
yield self.item(i)
It's even lazy (a generator).
answered Jan 7 '11 at 20:05
user395760
Thanks! What are your thoughts on using xrange here.
– Michael A. Jackson
Jan 7 '11 at 21:10
2
@majgis: My all means, yes. In Python 2, use xrange whenever possible. It's just that I'm usually using Python 3, so I get tend to write Python 3 in my examples.
– user395760
Jan 7 '11 at 21:14
add a comment |
Thanks! What are your thoughts on using xrange here.
– Michael A. Jackson
Jan 7 '11 at 21:10
2
@majgis: My all means, yes. In Python 2, use xrange whenever possible. It's just that I'm usually using Python 3, so I get tend to write Python 3 in my examples.
– user395760
Jan 7 '11 at 21:14
Thanks! What are your thoughts on using xrange here.
– Michael A. Jackson
Jan 7 '11 at 21:10
Thanks! What are your thoughts on using xrange here.
– Michael A. Jackson
Jan 7 '11 at 21:10
2
2
@majgis: My all means, yes. In Python 2, use xrange whenever possible. It's just that I'm usually using Python 3, so I get tend to write Python 3 in my examples.
– user395760
Jan 7 '11 at 21:14
@majgis: My all means, yes. In Python 2, use xrange whenever possible. It's just that I'm usually using Python 3, so I get tend to write Python 3 in my examples.
– user395760
Jan 7 '11 at 21:14
add a comment |
Just to add my 2 cents, as I was looking for this:
itemsTextList = [str(listWidget.item(i).text()) for i in range(listWidget.count())]
add a comment |
Just to add my 2 cents, as I was looking for this:
itemsTextList = [str(listWidget.item(i).text()) for i in range(listWidget.count())]
add a comment |
Just to add my 2 cents, as I was looking for this:
itemsTextList = [str(listWidget.item(i).text()) for i in range(listWidget.count())]
Just to add my 2 cents, as I was looking for this:
itemsTextList = [str(listWidget.item(i).text()) for i in range(listWidget.count())]
edited Dec 27 '15 at 13:01
answered Dec 27 '15 at 10:24


PythonicPythonic
1,0911127
1,0911127
add a comment |
add a comment |
I know this is old but, I just found out a function findItems(text, Qt.MatchFlags) in QListWidget. So, to iterate all items:
#listWidget is a QListWidget full of items
all_items = listWidget.findItems('', QtCore.Qt.MatchRegExp)
for item in all_items:
print item
And do whatever you need with the item =)
2
You can also find the other Qt.MatchFlag parameters here: pyqt.sourceforge.net/Docs/PyQt4/qt.html#MatchFlag-enum
– J. Saw
Oct 19 '18 at 1:12
add a comment |
I know this is old but, I just found out a function findItems(text, Qt.MatchFlags) in QListWidget. So, to iterate all items:
#listWidget is a QListWidget full of items
all_items = listWidget.findItems('', QtCore.Qt.MatchRegExp)
for item in all_items:
print item
And do whatever you need with the item =)
2
You can also find the other Qt.MatchFlag parameters here: pyqt.sourceforge.net/Docs/PyQt4/qt.html#MatchFlag-enum
– J. Saw
Oct 19 '18 at 1:12
add a comment |
I know this is old but, I just found out a function findItems(text, Qt.MatchFlags) in QListWidget. So, to iterate all items:
#listWidget is a QListWidget full of items
all_items = listWidget.findItems('', QtCore.Qt.MatchRegExp)
for item in all_items:
print item
And do whatever you need with the item =)
I know this is old but, I just found out a function findItems(text, Qt.MatchFlags) in QListWidget. So, to iterate all items:
#listWidget is a QListWidget full of items
all_items = listWidget.findItems('', QtCore.Qt.MatchRegExp)
for item in all_items:
print item
And do whatever you need with the item =)
edited Nov 25 '16 at 17:12
Anchmerama
6715
6715
answered Nov 8 '11 at 1:45
Diego PoncianoDiego Ponciano
1,0041417
1,0041417
2
You can also find the other Qt.MatchFlag parameters here: pyqt.sourceforge.net/Docs/PyQt4/qt.html#MatchFlag-enum
– J. Saw
Oct 19 '18 at 1:12
add a comment |
2
You can also find the other Qt.MatchFlag parameters here: pyqt.sourceforge.net/Docs/PyQt4/qt.html#MatchFlag-enum
– J. Saw
Oct 19 '18 at 1:12
2
2
You can also find the other Qt.MatchFlag parameters here: pyqt.sourceforge.net/Docs/PyQt4/qt.html#MatchFlag-enum
– J. Saw
Oct 19 '18 at 1:12
You can also find the other Qt.MatchFlag parameters here: pyqt.sourceforge.net/Docs/PyQt4/qt.html#MatchFlag-enum
– J. Saw
Oct 19 '18 at 1:12
add a comment |
items =
for index in xrange(self.listWidget.count()):
items.append(self.listWidget.item(index))
add a comment |
items =
for index in xrange(self.listWidget.count()):
items.append(self.listWidget.item(index))
add a comment |
items =
for index in xrange(self.listWidget.count()):
items.append(self.listWidget.item(index))
items =
for index in xrange(self.listWidget.count()):
items.append(self.listWidget.item(index))
answered Dec 2 '11 at 7:44
samsam
4,918155791
4,918155791
add a comment |
add a comment |
Just as a note for others landing here seeking the same information about PyQt5, it's slightly different here.
As user Pythonic stated above for PyQt4, you can still directly index an item using:
item_at_index_n = list_widget.item(n)
But to acquire a list of items for iteration, using an empty string and the MatchRegExp flag doesn't seem to work any more.
One way to do this in PyQt5 is:
all_items = list_widget.findItems('*', PyQt5.Qt.MatchWildcard)
I'm still getting to grips with PyQt, so there may well be an easier / simpler / more elegant way that I simply haven't come across yet. But I hope this helps!
add a comment |
Just as a note for others landing here seeking the same information about PyQt5, it's slightly different here.
As user Pythonic stated above for PyQt4, you can still directly index an item using:
item_at_index_n = list_widget.item(n)
But to acquire a list of items for iteration, using an empty string and the MatchRegExp flag doesn't seem to work any more.
One way to do this in PyQt5 is:
all_items = list_widget.findItems('*', PyQt5.Qt.MatchWildcard)
I'm still getting to grips with PyQt, so there may well be an easier / simpler / more elegant way that I simply haven't come across yet. But I hope this helps!
add a comment |
Just as a note for others landing here seeking the same information about PyQt5, it's slightly different here.
As user Pythonic stated above for PyQt4, you can still directly index an item using:
item_at_index_n = list_widget.item(n)
But to acquire a list of items for iteration, using an empty string and the MatchRegExp flag doesn't seem to work any more.
One way to do this in PyQt5 is:
all_items = list_widget.findItems('*', PyQt5.Qt.MatchWildcard)
I'm still getting to grips with PyQt, so there may well be an easier / simpler / more elegant way that I simply haven't come across yet. But I hope this helps!
Just as a note for others landing here seeking the same information about PyQt5, it's slightly different here.
As user Pythonic stated above for PyQt4, you can still directly index an item using:
item_at_index_n = list_widget.item(n)
But to acquire a list of items for iteration, using an empty string and the MatchRegExp flag doesn't seem to work any more.
One way to do this in PyQt5 is:
all_items = list_widget.findItems('*', PyQt5.Qt.MatchWildcard)
I'm still getting to grips with PyQt, so there may well be an easier / simpler / more elegant way that I simply haven't come across yet. But I hope this helps!
edited Nov 8 '18 at 8:16
answered Nov 8 '18 at 8:07


CosmicStressheadCosmicStresshead
13
13
add a comment |
add a comment |
items =
for index in range(self.listWidget.count()):
items.append(self.listWidget.item(index))
If your wish to get the text of the items
for item in items:
print(item.text())
add a comment |
items =
for index in range(self.listWidget.count()):
items.append(self.listWidget.item(index))
If your wish to get the text of the items
for item in items:
print(item.text())
add a comment |
items =
for index in range(self.listWidget.count()):
items.append(self.listWidget.item(index))
If your wish to get the text of the items
for item in items:
print(item.text())
items =
for index in range(self.listWidget.count()):
items.append(self.listWidget.item(index))
If your wish to get the text of the items
for item in items:
print(item.text())
answered Nov 19 '18 at 23:16
salafisalafi
13
13
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4629584%2fpyqt4-how-do-you-iterate-all-items-in-a-qlistwidget%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown