PyQt4: How do you iterate all items in a QListWidget












19















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?










share|improve this question



























    19















    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?










    share|improve this question

























      19












      19








      19


      3






      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?










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 7 '11 at 19:45









      Michael A. JacksonMichael A. Jackson

      2,78132540




      2,78132540
























          6 Answers
          6






          active

          oldest

          votes


















          18














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






          share|improve this answer
























          • 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



















          10














          Just to add my 2 cents, as I was looking for this:



          itemsTextList =  [str(listWidget.item(i).text()) for i in range(listWidget.count())]





          share|improve this answer

































            9














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






            share|improve this answer





















            • 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



















            7














            items = 
            for index in xrange(self.listWidget.count()):
            items.append(self.listWidget.item(index))





            share|improve this answer































              0














              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!






              share|improve this answer

































                0














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





                share|improve this answer























                  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
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  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









                  18














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






                  share|improve this answer
























                  • 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
















                  18














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






                  share|improve this answer
























                  • 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














                  18












                  18








                  18







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






                  share|improve this answer













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







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  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



















                  • 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













                  10














                  Just to add my 2 cents, as I was looking for this:



                  itemsTextList =  [str(listWidget.item(i).text()) for i in range(listWidget.count())]





                  share|improve this answer






























                    10














                    Just to add my 2 cents, as I was looking for this:



                    itemsTextList =  [str(listWidget.item(i).text()) for i in range(listWidget.count())]





                    share|improve this answer




























                      10












                      10








                      10







                      Just to add my 2 cents, as I was looking for this:



                      itemsTextList =  [str(listWidget.item(i).text()) for i in range(listWidget.count())]





                      share|improve this answer















                      Just to add my 2 cents, as I was looking for this:



                      itemsTextList =  [str(listWidget.item(i).text()) for i in range(listWidget.count())]






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 27 '15 at 13:01

























                      answered Dec 27 '15 at 10:24









                      PythonicPythonic

                      1,0911127




                      1,0911127























                          9














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






                          share|improve this answer





















                          • 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
















                          9














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






                          share|improve this answer





















                          • 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














                          9












                          9








                          9







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






                          share|improve this answer















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







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          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














                          • 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











                          7














                          items = 
                          for index in xrange(self.listWidget.count()):
                          items.append(self.listWidget.item(index))





                          share|improve this answer




























                            7














                            items = 
                            for index in xrange(self.listWidget.count()):
                            items.append(self.listWidget.item(index))





                            share|improve this answer


























                              7












                              7








                              7







                              items = 
                              for index in xrange(self.listWidget.count()):
                              items.append(self.listWidget.item(index))





                              share|improve this answer













                              items = 
                              for index in xrange(self.listWidget.count()):
                              items.append(self.listWidget.item(index))






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Dec 2 '11 at 7:44









                              samsam

                              4,918155791




                              4,918155791























                                  0














                                  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!






                                  share|improve this answer






























                                    0














                                    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!






                                    share|improve this answer




























                                      0












                                      0








                                      0







                                      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!






                                      share|improve this answer















                                      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!







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Nov 8 '18 at 8:16

























                                      answered Nov 8 '18 at 8:07









                                      CosmicStressheadCosmicStresshead

                                      13




                                      13























                                          0














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





                                          share|improve this answer




























                                            0














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





                                            share|improve this answer


























                                              0












                                              0








                                              0







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





                                              share|improve this answer













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






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Nov 19 '18 at 23:16









                                              salafisalafi

                                              13




                                              13






























                                                  draft saved

                                                  draft discarded




















































                                                  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.




                                                  draft saved


                                                  draft discarded














                                                  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





















































                                                  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







                                                  Popular posts from this blog

                                                  MongoDB - Not Authorized To Execute Command

                                                  How to fix TextFormField cause rebuild widget in Flutter

                                                  in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith