Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Passing list to class init, pyQt
Forum Updated to NodeBB v4.3 + New Features

Passing list to class init, pyQt

Scheduled Pinned Locked Moved Solved General and Desktop
python3pyqt5
4 Posts 2 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    robbiecooper
    wrote on 1 Jun 2017, 09:44 last edited by
    #1

    Hi All

    I'm new to Python and pyQt. I've got a class for a QDialog, and am passing a list of strings to this class in the init;

    class Remove_Source(QtWidgets.QDialog, Ui_R_Source):
        def __init__(self, items=None):
            super(Remove_Source , self).__init__()
            self.ui = Ui_R_Source()
            self.ui.setupUi(self)
            self.setWindowTitle("Remove Source Folders")
            self.items = items or []
            for i in range(self.items.count()):
                self.ui.keep_window.addItem(self.items(i))
            self.exec()
    

    The list is inaccessible within the class. Anyone know what I'm doing wrong? Thanks

    J 2 Replies Last reply 1 Jun 2017, 11:26
    0
    • R robbiecooper
      1 Jun 2017, 09:44

      Hi All

      I'm new to Python and pyQt. I've got a class for a QDialog, and am passing a list of strings to this class in the init;

      class Remove_Source(QtWidgets.QDialog, Ui_R_Source):
          def __init__(self, items=None):
              super(Remove_Source , self).__init__()
              self.ui = Ui_R_Source()
              self.ui.setupUi(self)
              self.setWindowTitle("Remove Source Folders")
              self.items = items or []
              for i in range(self.items.count()):
                  self.ui.keep_window.addItem(self.items(i))
              self.exec()
      

      The list is inaccessible within the class. Anyone know what I'm doing wrong? Thanks

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 1 Jun 2017, 11:26 last edited by
      #2

      @robbiecooper said in Passing list to class init, pyQt:

      inaccessible

      What do you mean by "inaccessible"?
      Also, how do you pass the list to the constructor?
      And this is wrong:

      self.items(i)
      

      must be

      self.items[i]
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • R robbiecooper
        1 Jun 2017, 09:44

        Hi All

        I'm new to Python and pyQt. I've got a class for a QDialog, and am passing a list of strings to this class in the init;

        class Remove_Source(QtWidgets.QDialog, Ui_R_Source):
            def __init__(self, items=None):
                super(Remove_Source , self).__init__()
                self.ui = Ui_R_Source()
                self.ui.setupUi(self)
                self.setWindowTitle("Remove Source Folders")
                self.items = items or []
                for i in range(self.items.count()):
                    self.ui.keep_window.addItem(self.items(i))
                self.exec()
        

        The list is inaccessible within the class. Anyone know what I'm doing wrong? Thanks

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 1 Jun 2017, 11:29 last edited by jsulm 6 Jan 2017, 11:29
        #3

        @robbiecooper In your code there is even more wrong:

        self.items.count()
        

        does not do what you think it does and will cause you app to crash (count() counts how often an element, which is passed as parameter, occurs in the list).
        It should be

        len(self.items)

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • R Offline
          R Offline
          robbiecooper
          wrote on 1 Jun 2017, 13:07 last edited by
          #4

          Thanks @jsulm!! That sorted it!

          1 Reply Last reply
          0

          1/4

          1 Jun 2017, 09:44

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved