How to change the size of PyQt5 directory view in the main window?









up vote
3
down vote

favorite












I am working on a PyQt5 project, which needs a folder viewer by PyQt5 QTreeView. In order to put more stuff, I try to change the size of the tree view but in vain. Here is the code from Pythonspot:



import sys
from PyQt5.QtWidgets import QApplication, QFileSystemModel, QTreeView, QWidget, QVBoxLayout
from PyQt5.QtGui import QIcon

class App(QWidget):

def __init__(self):
super().__init__()
self.title = 'PyQt5 file system view - pythonspot.com'
self.left = 10
self.top = 10
self.width = 640
self.height = 480
self.initUI()

def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)

self.model = QFileSystemModel()
self.model.setRootPath('')
self.tree = QTreeView()
self.tree.setModel(self.model)

self.tree.setAnimated(False)
self.tree.setIndentation(20)
self.tree.setSortingEnabled(True)

self.tree.setWindowTitle("Dir View")
self.tree.resize(640, 200)

windowLayout = QVBoxLayout()
windowLayout.addWidget(self.tree)
self.setLayout(windowLayout)

self.show()

if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())


I change the tree view by



self.tree.resize(640, 200)


Why it does not function?










share|improve this question



























    up vote
    3
    down vote

    favorite












    I am working on a PyQt5 project, which needs a folder viewer by PyQt5 QTreeView. In order to put more stuff, I try to change the size of the tree view but in vain. Here is the code from Pythonspot:



    import sys
    from PyQt5.QtWidgets import QApplication, QFileSystemModel, QTreeView, QWidget, QVBoxLayout
    from PyQt5.QtGui import QIcon

    class App(QWidget):

    def __init__(self):
    super().__init__()
    self.title = 'PyQt5 file system view - pythonspot.com'
    self.left = 10
    self.top = 10
    self.width = 640
    self.height = 480
    self.initUI()

    def initUI(self):
    self.setWindowTitle(self.title)
    self.setGeometry(self.left, self.top, self.width, self.height)

    self.model = QFileSystemModel()
    self.model.setRootPath('')
    self.tree = QTreeView()
    self.tree.setModel(self.model)

    self.tree.setAnimated(False)
    self.tree.setIndentation(20)
    self.tree.setSortingEnabled(True)

    self.tree.setWindowTitle("Dir View")
    self.tree.resize(640, 200)

    windowLayout = QVBoxLayout()
    windowLayout.addWidget(self.tree)
    self.setLayout(windowLayout)

    self.show()

    if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())


    I change the tree view by



    self.tree.resize(640, 200)


    Why it does not function?










    share|improve this question

























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I am working on a PyQt5 project, which needs a folder viewer by PyQt5 QTreeView. In order to put more stuff, I try to change the size of the tree view but in vain. Here is the code from Pythonspot:



      import sys
      from PyQt5.QtWidgets import QApplication, QFileSystemModel, QTreeView, QWidget, QVBoxLayout
      from PyQt5.QtGui import QIcon

      class App(QWidget):

      def __init__(self):
      super().__init__()
      self.title = 'PyQt5 file system view - pythonspot.com'
      self.left = 10
      self.top = 10
      self.width = 640
      self.height = 480
      self.initUI()

      def initUI(self):
      self.setWindowTitle(self.title)
      self.setGeometry(self.left, self.top, self.width, self.height)

      self.model = QFileSystemModel()
      self.model.setRootPath('')
      self.tree = QTreeView()
      self.tree.setModel(self.model)

      self.tree.setAnimated(False)
      self.tree.setIndentation(20)
      self.tree.setSortingEnabled(True)

      self.tree.setWindowTitle("Dir View")
      self.tree.resize(640, 200)

      windowLayout = QVBoxLayout()
      windowLayout.addWidget(self.tree)
      self.setLayout(windowLayout)

      self.show()

      if __name__ == '__main__':
      app = QApplication(sys.argv)
      ex = App()
      sys.exit(app.exec_())


      I change the tree view by



      self.tree.resize(640, 200)


      Why it does not function?










      share|improve this question















      I am working on a PyQt5 project, which needs a folder viewer by PyQt5 QTreeView. In order to put more stuff, I try to change the size of the tree view but in vain. Here is the code from Pythonspot:



      import sys
      from PyQt5.QtWidgets import QApplication, QFileSystemModel, QTreeView, QWidget, QVBoxLayout
      from PyQt5.QtGui import QIcon

      class App(QWidget):

      def __init__(self):
      super().__init__()
      self.title = 'PyQt5 file system view - pythonspot.com'
      self.left = 10
      self.top = 10
      self.width = 640
      self.height = 480
      self.initUI()

      def initUI(self):
      self.setWindowTitle(self.title)
      self.setGeometry(self.left, self.top, self.width, self.height)

      self.model = QFileSystemModel()
      self.model.setRootPath('')
      self.tree = QTreeView()
      self.tree.setModel(self.model)

      self.tree.setAnimated(False)
      self.tree.setIndentation(20)
      self.tree.setSortingEnabled(True)

      self.tree.setWindowTitle("Dir View")
      self.tree.resize(640, 200)

      windowLayout = QVBoxLayout()
      windowLayout.addWidget(self.tree)
      self.setLayout(windowLayout)

      self.show()

      if __name__ == '__main__':
      app = QApplication(sys.argv)
      ex = App()
      sys.exit(app.exec_())


      I change the tree view by



      self.tree.resize(640, 200)


      Why it does not function?







      python python-3.x pyqt5 qtreeview






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 9 at 20:05









      eyllanesc

      68.9k93052




      68.9k93052










      asked Nov 8 at 20:03









      Tim

      238




      238






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          A layout is used to establish the position and size of the widget you are using, so in your case even if you use resize the size will not be changed, instead you should set a fixed size so the layout can not change the size of the QTreeView.



          import sys
          from PyQt5 import QtCore, QtGui, QtWidgets

          class App(QtWidgets.QWidget):
          def __init__(self):
          super().__init__()
          self.title = 'PyQt5 file system view - pythonspot.com'
          self.left, self.top, self.width, self.height = 10, 10, 640, 480
          self.initUI()

          def initUI(self):
          self.setWindowTitle(self.title)
          self.setGeometry(self.left, self.top, self.width, self.height)

          self.model = QtWidgets.QFileSystemModel()
          self.model.setRootPath('')
          self.tree = QtWidgets.QTreeView()
          self.tree.setModel(self.model)

          self.tree.setAnimated(False)
          self.tree.setIndentation(20)
          self.tree.setSortingEnabled(True)

          self.tree.setWindowTitle("Dir View")
          self.tree.setFixedSize(640, 200)

          windowLayout = QtWidgets.QVBoxLayout(self)
          windowLayout.addWidget(self.tree, alignment=QtCore.Qt.AlignTop)

          self.show()

          if __name__ == '__main__':
          app = QtWidgets.QApplication(sys.argv)
          ex = App()
          sys.exit(app.exec_())





          share|improve this answer




















          • Thanks. Now I understand
            – Tim
            Nov 8 at 20:47










          • If I want to move the tree view by the following line? self.tree.move(200,100)
            – Tim
            Nov 9 at 19:48






          • 1




            @TimChen then do not use a layout, for it eliminates: windowLayout = QtWidgets.QVBoxLayout(self) windowLayout.addWidget(self.tree, alignment=QtCore.Qt.AlignTop) and change self.tree = QtWidgets.QTreeView() to self.tree = QtWidgets.QTreeView(self) and change self.tree.setFixedSize(640, 200) to self.tree.setGeometry(200, 100, 640, 200)
            – eyllanesc
            Nov 9 at 19:53










          • Thanks for your help! And if I want to have a popout window once I double-click a file in the directory view, could I add one line: itemDoubleClicked.connect(self.buildExamplePopup) in initUI(self). And add a function below: def buildExamplePopup(self, item): exPopup = ExamplePopup(item.text(), self) exPopup.setGeometry(100, 200, 300, 300) exPopup.show()
            – Tim
            Nov 15 at 8:54











          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',
          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%2f53215329%2fhow-to-change-the-size-of-pyqt5-directory-view-in-the-main-window%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          A layout is used to establish the position and size of the widget you are using, so in your case even if you use resize the size will not be changed, instead you should set a fixed size so the layout can not change the size of the QTreeView.



          import sys
          from PyQt5 import QtCore, QtGui, QtWidgets

          class App(QtWidgets.QWidget):
          def __init__(self):
          super().__init__()
          self.title = 'PyQt5 file system view - pythonspot.com'
          self.left, self.top, self.width, self.height = 10, 10, 640, 480
          self.initUI()

          def initUI(self):
          self.setWindowTitle(self.title)
          self.setGeometry(self.left, self.top, self.width, self.height)

          self.model = QtWidgets.QFileSystemModel()
          self.model.setRootPath('')
          self.tree = QtWidgets.QTreeView()
          self.tree.setModel(self.model)

          self.tree.setAnimated(False)
          self.tree.setIndentation(20)
          self.tree.setSortingEnabled(True)

          self.tree.setWindowTitle("Dir View")
          self.tree.setFixedSize(640, 200)

          windowLayout = QtWidgets.QVBoxLayout(self)
          windowLayout.addWidget(self.tree, alignment=QtCore.Qt.AlignTop)

          self.show()

          if __name__ == '__main__':
          app = QtWidgets.QApplication(sys.argv)
          ex = App()
          sys.exit(app.exec_())





          share|improve this answer




















          • Thanks. Now I understand
            – Tim
            Nov 8 at 20:47










          • If I want to move the tree view by the following line? self.tree.move(200,100)
            – Tim
            Nov 9 at 19:48






          • 1




            @TimChen then do not use a layout, for it eliminates: windowLayout = QtWidgets.QVBoxLayout(self) windowLayout.addWidget(self.tree, alignment=QtCore.Qt.AlignTop) and change self.tree = QtWidgets.QTreeView() to self.tree = QtWidgets.QTreeView(self) and change self.tree.setFixedSize(640, 200) to self.tree.setGeometry(200, 100, 640, 200)
            – eyllanesc
            Nov 9 at 19:53










          • Thanks for your help! And if I want to have a popout window once I double-click a file in the directory view, could I add one line: itemDoubleClicked.connect(self.buildExamplePopup) in initUI(self). And add a function below: def buildExamplePopup(self, item): exPopup = ExamplePopup(item.text(), self) exPopup.setGeometry(100, 200, 300, 300) exPopup.show()
            – Tim
            Nov 15 at 8:54















          up vote
          1
          down vote



          accepted










          A layout is used to establish the position and size of the widget you are using, so in your case even if you use resize the size will not be changed, instead you should set a fixed size so the layout can not change the size of the QTreeView.



          import sys
          from PyQt5 import QtCore, QtGui, QtWidgets

          class App(QtWidgets.QWidget):
          def __init__(self):
          super().__init__()
          self.title = 'PyQt5 file system view - pythonspot.com'
          self.left, self.top, self.width, self.height = 10, 10, 640, 480
          self.initUI()

          def initUI(self):
          self.setWindowTitle(self.title)
          self.setGeometry(self.left, self.top, self.width, self.height)

          self.model = QtWidgets.QFileSystemModel()
          self.model.setRootPath('')
          self.tree = QtWidgets.QTreeView()
          self.tree.setModel(self.model)

          self.tree.setAnimated(False)
          self.tree.setIndentation(20)
          self.tree.setSortingEnabled(True)

          self.tree.setWindowTitle("Dir View")
          self.tree.setFixedSize(640, 200)

          windowLayout = QtWidgets.QVBoxLayout(self)
          windowLayout.addWidget(self.tree, alignment=QtCore.Qt.AlignTop)

          self.show()

          if __name__ == '__main__':
          app = QtWidgets.QApplication(sys.argv)
          ex = App()
          sys.exit(app.exec_())





          share|improve this answer




















          • Thanks. Now I understand
            – Tim
            Nov 8 at 20:47










          • If I want to move the tree view by the following line? self.tree.move(200,100)
            – Tim
            Nov 9 at 19:48






          • 1




            @TimChen then do not use a layout, for it eliminates: windowLayout = QtWidgets.QVBoxLayout(self) windowLayout.addWidget(self.tree, alignment=QtCore.Qt.AlignTop) and change self.tree = QtWidgets.QTreeView() to self.tree = QtWidgets.QTreeView(self) and change self.tree.setFixedSize(640, 200) to self.tree.setGeometry(200, 100, 640, 200)
            – eyllanesc
            Nov 9 at 19:53










          • Thanks for your help! And if I want to have a popout window once I double-click a file in the directory view, could I add one line: itemDoubleClicked.connect(self.buildExamplePopup) in initUI(self). And add a function below: def buildExamplePopup(self, item): exPopup = ExamplePopup(item.text(), self) exPopup.setGeometry(100, 200, 300, 300) exPopup.show()
            – Tim
            Nov 15 at 8:54













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          A layout is used to establish the position and size of the widget you are using, so in your case even if you use resize the size will not be changed, instead you should set a fixed size so the layout can not change the size of the QTreeView.



          import sys
          from PyQt5 import QtCore, QtGui, QtWidgets

          class App(QtWidgets.QWidget):
          def __init__(self):
          super().__init__()
          self.title = 'PyQt5 file system view - pythonspot.com'
          self.left, self.top, self.width, self.height = 10, 10, 640, 480
          self.initUI()

          def initUI(self):
          self.setWindowTitle(self.title)
          self.setGeometry(self.left, self.top, self.width, self.height)

          self.model = QtWidgets.QFileSystemModel()
          self.model.setRootPath('')
          self.tree = QtWidgets.QTreeView()
          self.tree.setModel(self.model)

          self.tree.setAnimated(False)
          self.tree.setIndentation(20)
          self.tree.setSortingEnabled(True)

          self.tree.setWindowTitle("Dir View")
          self.tree.setFixedSize(640, 200)

          windowLayout = QtWidgets.QVBoxLayout(self)
          windowLayout.addWidget(self.tree, alignment=QtCore.Qt.AlignTop)

          self.show()

          if __name__ == '__main__':
          app = QtWidgets.QApplication(sys.argv)
          ex = App()
          sys.exit(app.exec_())





          share|improve this answer












          A layout is used to establish the position and size of the widget you are using, so in your case even if you use resize the size will not be changed, instead you should set a fixed size so the layout can not change the size of the QTreeView.



          import sys
          from PyQt5 import QtCore, QtGui, QtWidgets

          class App(QtWidgets.QWidget):
          def __init__(self):
          super().__init__()
          self.title = 'PyQt5 file system view - pythonspot.com'
          self.left, self.top, self.width, self.height = 10, 10, 640, 480
          self.initUI()

          def initUI(self):
          self.setWindowTitle(self.title)
          self.setGeometry(self.left, self.top, self.width, self.height)

          self.model = QtWidgets.QFileSystemModel()
          self.model.setRootPath('')
          self.tree = QtWidgets.QTreeView()
          self.tree.setModel(self.model)

          self.tree.setAnimated(False)
          self.tree.setIndentation(20)
          self.tree.setSortingEnabled(True)

          self.tree.setWindowTitle("Dir View")
          self.tree.setFixedSize(640, 200)

          windowLayout = QtWidgets.QVBoxLayout(self)
          windowLayout.addWidget(self.tree, alignment=QtCore.Qt.AlignTop)

          self.show()

          if __name__ == '__main__':
          app = QtWidgets.QApplication(sys.argv)
          ex = App()
          sys.exit(app.exec_())






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 20:22









          eyllanesc

          68.9k93052




          68.9k93052











          • Thanks. Now I understand
            – Tim
            Nov 8 at 20:47










          • If I want to move the tree view by the following line? self.tree.move(200,100)
            – Tim
            Nov 9 at 19:48






          • 1




            @TimChen then do not use a layout, for it eliminates: windowLayout = QtWidgets.QVBoxLayout(self) windowLayout.addWidget(self.tree, alignment=QtCore.Qt.AlignTop) and change self.tree = QtWidgets.QTreeView() to self.tree = QtWidgets.QTreeView(self) and change self.tree.setFixedSize(640, 200) to self.tree.setGeometry(200, 100, 640, 200)
            – eyllanesc
            Nov 9 at 19:53










          • Thanks for your help! And if I want to have a popout window once I double-click a file in the directory view, could I add one line: itemDoubleClicked.connect(self.buildExamplePopup) in initUI(self). And add a function below: def buildExamplePopup(self, item): exPopup = ExamplePopup(item.text(), self) exPopup.setGeometry(100, 200, 300, 300) exPopup.show()
            – Tim
            Nov 15 at 8:54

















          • Thanks. Now I understand
            – Tim
            Nov 8 at 20:47










          • If I want to move the tree view by the following line? self.tree.move(200,100)
            – Tim
            Nov 9 at 19:48






          • 1




            @TimChen then do not use a layout, for it eliminates: windowLayout = QtWidgets.QVBoxLayout(self) windowLayout.addWidget(self.tree, alignment=QtCore.Qt.AlignTop) and change self.tree = QtWidgets.QTreeView() to self.tree = QtWidgets.QTreeView(self) and change self.tree.setFixedSize(640, 200) to self.tree.setGeometry(200, 100, 640, 200)
            – eyllanesc
            Nov 9 at 19:53










          • Thanks for your help! And if I want to have a popout window once I double-click a file in the directory view, could I add one line: itemDoubleClicked.connect(self.buildExamplePopup) in initUI(self). And add a function below: def buildExamplePopup(self, item): exPopup = ExamplePopup(item.text(), self) exPopup.setGeometry(100, 200, 300, 300) exPopup.show()
            – Tim
            Nov 15 at 8:54
















          Thanks. Now I understand
          – Tim
          Nov 8 at 20:47




          Thanks. Now I understand
          – Tim
          Nov 8 at 20:47












          If I want to move the tree view by the following line? self.tree.move(200,100)
          – Tim
          Nov 9 at 19:48




          If I want to move the tree view by the following line? self.tree.move(200,100)
          – Tim
          Nov 9 at 19:48




          1




          1




          @TimChen then do not use a layout, for it eliminates: windowLayout = QtWidgets.QVBoxLayout(self) windowLayout.addWidget(self.tree, alignment=QtCore.Qt.AlignTop) and change self.tree = QtWidgets.QTreeView() to self.tree = QtWidgets.QTreeView(self) and change self.tree.setFixedSize(640, 200) to self.tree.setGeometry(200, 100, 640, 200)
          – eyllanesc
          Nov 9 at 19:53




          @TimChen then do not use a layout, for it eliminates: windowLayout = QtWidgets.QVBoxLayout(self) windowLayout.addWidget(self.tree, alignment=QtCore.Qt.AlignTop) and change self.tree = QtWidgets.QTreeView() to self.tree = QtWidgets.QTreeView(self) and change self.tree.setFixedSize(640, 200) to self.tree.setGeometry(200, 100, 640, 200)
          – eyllanesc
          Nov 9 at 19:53












          Thanks for your help! And if I want to have a popout window once I double-click a file in the directory view, could I add one line: itemDoubleClicked.connect(self.buildExamplePopup) in initUI(self). And add a function below: def buildExamplePopup(self, item): exPopup = ExamplePopup(item.text(), self) exPopup.setGeometry(100, 200, 300, 300) exPopup.show()
          – Tim
          Nov 15 at 8:54





          Thanks for your help! And if I want to have a popout window once I double-click a file in the directory view, could I add one line: itemDoubleClicked.connect(self.buildExamplePopup) in initUI(self). And add a function below: def buildExamplePopup(self, item): exPopup = ExamplePopup(item.text(), self) exPopup.setGeometry(100, 200, 300, 300) exPopup.show()
          – Tim
          Nov 15 at 8:54


















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53215329%2fhow-to-change-the-size-of-pyqt5-directory-view-in-the-main-window%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

          𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

          How do I collapse sections of code in Visual Studio Code for Windows?

          ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ