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?
python python-3.x pyqt5 qtreeview
add a comment |
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?
python python-3.x pyqt5 qtreeview
add a comment |
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?
python python-3.x pyqt5 qtreeview
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
python python-3.x pyqt5 qtreeview
edited Nov 9 at 20:05
eyllanesc
68.9k93052
68.9k93052
asked Nov 8 at 20:03
Tim
238
238
add a comment |
add a comment |
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_())
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 changeself.tree = QtWidgets.QTreeView()toself.tree = QtWidgets.QTreeView(self)and changeself.tree.setFixedSize(640, 200)toself.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
add a comment |
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_())
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 changeself.tree = QtWidgets.QTreeView()toself.tree = QtWidgets.QTreeView(self)and changeself.tree.setFixedSize(640, 200)toself.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
add a comment |
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_())
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 changeself.tree = QtWidgets.QTreeView()toself.tree = QtWidgets.QTreeView(self)and changeself.tree.setFixedSize(640, 200)toself.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
add a comment |
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_())
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_())
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 changeself.tree = QtWidgets.QTreeView()toself.tree = QtWidgets.QTreeView(self)and changeself.tree.setFixedSize(640, 200)toself.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
add a comment |
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 changeself.tree = QtWidgets.QTreeView()toself.tree = QtWidgets.QTreeView(self)and changeself.tree.setFixedSize(640, 200)toself.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
add a comment |
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%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
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