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.
  • robbiecooperR Offline
    robbiecooperR Offline
    robbiecooper
    wrote on 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

    jsulmJ 2 Replies Last reply
    0
    • robbiecooperR robbiecooper

      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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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
      • robbiecooperR robbiecooper

        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

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by jsulm
        #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
        • robbiecooperR Offline
          robbiecooperR Offline
          robbiecooper
          wrote on last edited by
          #4

          Thanks @jsulm!! That sorted it!

          1 Reply Last reply
          0

          • Login

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