How do you set the column width on a QTreeView?
up vote
12
down vote
favorite
Bear with me, I'm still new to QT and am having trouble wrapping my brain around how it does things.
I've created and populated a QTreeView with two columns:
class AppForm(QMainWindow):
def __init__(self, parent = None):
super(AppForm, self).__init__(parent)
self.model = QStandardItemModel()
self.view = QTreeView()
self.view.setColumnWidth(0, 800)
self.view.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.view.setModel(self.model)
self.setCentralWidget(self.view)
Everything's working great, except the columns are extremely narrow. I hoped that setColumnWidth(0, 800) would widen the first column, but it doesn't seem to be having any effect. What's the proper method for setting column widths?
python pyqt pyqt4 qtreeview
add a comment |
up vote
12
down vote
favorite
Bear with me, I'm still new to QT and am having trouble wrapping my brain around how it does things.
I've created and populated a QTreeView with two columns:
class AppForm(QMainWindow):
def __init__(self, parent = None):
super(AppForm, self).__init__(parent)
self.model = QStandardItemModel()
self.view = QTreeView()
self.view.setColumnWidth(0, 800)
self.view.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.view.setModel(self.model)
self.setCentralWidget(self.view)
Everything's working great, except the columns are extremely narrow. I hoped that setColumnWidth(0, 800) would widen the first column, but it doesn't seem to be having any effect. What's the proper method for setting column widths?
python pyqt pyqt4 qtreeview
add a comment |
up vote
12
down vote
favorite
up vote
12
down vote
favorite
Bear with me, I'm still new to QT and am having trouble wrapping my brain around how it does things.
I've created and populated a QTreeView with two columns:
class AppForm(QMainWindow):
def __init__(self, parent = None):
super(AppForm, self).__init__(parent)
self.model = QStandardItemModel()
self.view = QTreeView()
self.view.setColumnWidth(0, 800)
self.view.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.view.setModel(self.model)
self.setCentralWidget(self.view)
Everything's working great, except the columns are extremely narrow. I hoped that setColumnWidth(0, 800) would widen the first column, but it doesn't seem to be having any effect. What's the proper method for setting column widths?
python pyqt pyqt4 qtreeview
Bear with me, I'm still new to QT and am having trouble wrapping my brain around how it does things.
I've created and populated a QTreeView with two columns:
class AppForm(QMainWindow):
def __init__(self, parent = None):
super(AppForm, self).__init__(parent)
self.model = QStandardItemModel()
self.view = QTreeView()
self.view.setColumnWidth(0, 800)
self.view.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.view.setModel(self.model)
self.setCentralWidget(self.view)
Everything's working great, except the columns are extremely narrow. I hoped that setColumnWidth(0, 800) would widen the first column, but it doesn't seem to be having any effect. What's the proper method for setting column widths?
python pyqt pyqt4 qtreeview
python pyqt pyqt4 qtreeview
edited Mar 16 '14 at 3:38
ekhumoro
75.5k15108180
75.5k15108180
asked Dec 2 '11 at 23:38
ashground
104229
104229
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
15
down vote
accepted
When you call setColumnWidth
, Qt will do the equivalent of:
self.view.header().resizeSection(column, width)
Then, when you call setModel
, Qt will (amongst other things) do the equivalent of:
self.view.header().setModel(model)
So the column width does get set - just not on the model the tree view ends up with.
tl;dr
: set the column width after you set the model.
EDIT
Here's a simple demo script based on your example:
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.model = QtGui.QStandardItemModel()
self.view = QtGui.QTreeView()
self.view.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
self.view.setModel(self.model)
self.setCentralWidget(self.view)
parent = self.model.invisibleRootItem()
for item in 'One Two Three Four'.split():
parent.appendRow([
QtGui.QStandardItem(item),
QtGui.QStandardItem(),
QtGui.QStandardItem(),
])
self.view.setColumnWidth(0, 800)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
I moved setColumnWidth below setModel, but it still doesn't seem to be having an effect. Is the problem that I haven't populated it or set the amount of columns yet?
– ashground
Dec 5 '11 at 18:32
@ashground. I've added a demo script to my answer that works for me.
– ekhumoro
Dec 5 '11 at 18:40
Awesome -- I moved setColumnWidth into a different function so that it's called after it populates the tree. Everything's working as expected now. Thanks for your help!
– ashground
Dec 5 '11 at 19:57
add a comment |
up vote
9
down vote
self.view.resizeColumnToContents(0)
This makes sure that given column's width and height are set to match with content.
QTreeView' object has no attribute 'resizeColumnsToContents'
– Sophus
May 2 '17 at 18:51
resizeColumnToContents(), not Columns
– Liao Zhuodi
May 7 '17 at 13:30
add a comment |
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
});
}
});
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%2f8364061%2fhow-do-you-set-the-column-width-on-a-qtreeview%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
15
down vote
accepted
When you call setColumnWidth
, Qt will do the equivalent of:
self.view.header().resizeSection(column, width)
Then, when you call setModel
, Qt will (amongst other things) do the equivalent of:
self.view.header().setModel(model)
So the column width does get set - just not on the model the tree view ends up with.
tl;dr
: set the column width after you set the model.
EDIT
Here's a simple demo script based on your example:
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.model = QtGui.QStandardItemModel()
self.view = QtGui.QTreeView()
self.view.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
self.view.setModel(self.model)
self.setCentralWidget(self.view)
parent = self.model.invisibleRootItem()
for item in 'One Two Three Four'.split():
parent.appendRow([
QtGui.QStandardItem(item),
QtGui.QStandardItem(),
QtGui.QStandardItem(),
])
self.view.setColumnWidth(0, 800)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
I moved setColumnWidth below setModel, but it still doesn't seem to be having an effect. Is the problem that I haven't populated it or set the amount of columns yet?
– ashground
Dec 5 '11 at 18:32
@ashground. I've added a demo script to my answer that works for me.
– ekhumoro
Dec 5 '11 at 18:40
Awesome -- I moved setColumnWidth into a different function so that it's called after it populates the tree. Everything's working as expected now. Thanks for your help!
– ashground
Dec 5 '11 at 19:57
add a comment |
up vote
15
down vote
accepted
When you call setColumnWidth
, Qt will do the equivalent of:
self.view.header().resizeSection(column, width)
Then, when you call setModel
, Qt will (amongst other things) do the equivalent of:
self.view.header().setModel(model)
So the column width does get set - just not on the model the tree view ends up with.
tl;dr
: set the column width after you set the model.
EDIT
Here's a simple demo script based on your example:
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.model = QtGui.QStandardItemModel()
self.view = QtGui.QTreeView()
self.view.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
self.view.setModel(self.model)
self.setCentralWidget(self.view)
parent = self.model.invisibleRootItem()
for item in 'One Two Three Four'.split():
parent.appendRow([
QtGui.QStandardItem(item),
QtGui.QStandardItem(),
QtGui.QStandardItem(),
])
self.view.setColumnWidth(0, 800)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
I moved setColumnWidth below setModel, but it still doesn't seem to be having an effect. Is the problem that I haven't populated it or set the amount of columns yet?
– ashground
Dec 5 '11 at 18:32
@ashground. I've added a demo script to my answer that works for me.
– ekhumoro
Dec 5 '11 at 18:40
Awesome -- I moved setColumnWidth into a different function so that it's called after it populates the tree. Everything's working as expected now. Thanks for your help!
– ashground
Dec 5 '11 at 19:57
add a comment |
up vote
15
down vote
accepted
up vote
15
down vote
accepted
When you call setColumnWidth
, Qt will do the equivalent of:
self.view.header().resizeSection(column, width)
Then, when you call setModel
, Qt will (amongst other things) do the equivalent of:
self.view.header().setModel(model)
So the column width does get set - just not on the model the tree view ends up with.
tl;dr
: set the column width after you set the model.
EDIT
Here's a simple demo script based on your example:
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.model = QtGui.QStandardItemModel()
self.view = QtGui.QTreeView()
self.view.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
self.view.setModel(self.model)
self.setCentralWidget(self.view)
parent = self.model.invisibleRootItem()
for item in 'One Two Three Four'.split():
parent.appendRow([
QtGui.QStandardItem(item),
QtGui.QStandardItem(),
QtGui.QStandardItem(),
])
self.view.setColumnWidth(0, 800)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
When you call setColumnWidth
, Qt will do the equivalent of:
self.view.header().resizeSection(column, width)
Then, when you call setModel
, Qt will (amongst other things) do the equivalent of:
self.view.header().setModel(model)
So the column width does get set - just not on the model the tree view ends up with.
tl;dr
: set the column width after you set the model.
EDIT
Here's a simple demo script based on your example:
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.model = QtGui.QStandardItemModel()
self.view = QtGui.QTreeView()
self.view.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
self.view.setModel(self.model)
self.setCentralWidget(self.view)
parent = self.model.invisibleRootItem()
for item in 'One Two Three Four'.split():
parent.appendRow([
QtGui.QStandardItem(item),
QtGui.QStandardItem(),
QtGui.QStandardItem(),
])
self.view.setColumnWidth(0, 800)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
edited Dec 5 '11 at 18:39
answered Dec 3 '11 at 0:57
ekhumoro
75.5k15108180
75.5k15108180
I moved setColumnWidth below setModel, but it still doesn't seem to be having an effect. Is the problem that I haven't populated it or set the amount of columns yet?
– ashground
Dec 5 '11 at 18:32
@ashground. I've added a demo script to my answer that works for me.
– ekhumoro
Dec 5 '11 at 18:40
Awesome -- I moved setColumnWidth into a different function so that it's called after it populates the tree. Everything's working as expected now. Thanks for your help!
– ashground
Dec 5 '11 at 19:57
add a comment |
I moved setColumnWidth below setModel, but it still doesn't seem to be having an effect. Is the problem that I haven't populated it or set the amount of columns yet?
– ashground
Dec 5 '11 at 18:32
@ashground. I've added a demo script to my answer that works for me.
– ekhumoro
Dec 5 '11 at 18:40
Awesome -- I moved setColumnWidth into a different function so that it's called after it populates the tree. Everything's working as expected now. Thanks for your help!
– ashground
Dec 5 '11 at 19:57
I moved setColumnWidth below setModel, but it still doesn't seem to be having an effect. Is the problem that I haven't populated it or set the amount of columns yet?
– ashground
Dec 5 '11 at 18:32
I moved setColumnWidth below setModel, but it still doesn't seem to be having an effect. Is the problem that I haven't populated it or set the amount of columns yet?
– ashground
Dec 5 '11 at 18:32
@ashground. I've added a demo script to my answer that works for me.
– ekhumoro
Dec 5 '11 at 18:40
@ashground. I've added a demo script to my answer that works for me.
– ekhumoro
Dec 5 '11 at 18:40
Awesome -- I moved setColumnWidth into a different function so that it's called after it populates the tree. Everything's working as expected now. Thanks for your help!
– ashground
Dec 5 '11 at 19:57
Awesome -- I moved setColumnWidth into a different function so that it's called after it populates the tree. Everything's working as expected now. Thanks for your help!
– ashground
Dec 5 '11 at 19:57
add a comment |
up vote
9
down vote
self.view.resizeColumnToContents(0)
This makes sure that given column's width and height are set to match with content.
QTreeView' object has no attribute 'resizeColumnsToContents'
– Sophus
May 2 '17 at 18:51
resizeColumnToContents(), not Columns
– Liao Zhuodi
May 7 '17 at 13:30
add a comment |
up vote
9
down vote
self.view.resizeColumnToContents(0)
This makes sure that given column's width and height are set to match with content.
QTreeView' object has no attribute 'resizeColumnsToContents'
– Sophus
May 2 '17 at 18:51
resizeColumnToContents(), not Columns
– Liao Zhuodi
May 7 '17 at 13:30
add a comment |
up vote
9
down vote
up vote
9
down vote
self.view.resizeColumnToContents(0)
This makes sure that given column's width and height are set to match with content.
self.view.resizeColumnToContents(0)
This makes sure that given column's width and height are set to match with content.
edited Nov 12 at 10:00
Bear
305317
305317
answered Jun 9 '12 at 8:34
galactor88
9112
9112
QTreeView' object has no attribute 'resizeColumnsToContents'
– Sophus
May 2 '17 at 18:51
resizeColumnToContents(), not Columns
– Liao Zhuodi
May 7 '17 at 13:30
add a comment |
QTreeView' object has no attribute 'resizeColumnsToContents'
– Sophus
May 2 '17 at 18:51
resizeColumnToContents(), not Columns
– Liao Zhuodi
May 7 '17 at 13:30
QTreeView' object has no attribute 'resizeColumnsToContents'
– Sophus
May 2 '17 at 18:51
QTreeView' object has no attribute 'resizeColumnsToContents'
– Sophus
May 2 '17 at 18:51
resizeColumnToContents(), not Columns
– Liao Zhuodi
May 7 '17 at 13:30
resizeColumnToContents(), not Columns
– Liao Zhuodi
May 7 '17 at 13:30
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
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%2f8364061%2fhow-do-you-set-the-column-width-on-a-qtreeview%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