setColumnWidth() doesn't work for QTableView

5,398 views
Skip to first unread message

rulfzid

unread,
Feb 24, 2011, 7:07:30 PM2/24/11
to pys...@googlegroups.com
I'm finding I can't programmatically resize a column in a QTableView with setColumnCount(). 

The function does seem to work with QTreeViews though, which is weird.

I've found a workaround, using tableview.horizontalHeader().resizeSection(col, pixels), but just wondering if this a bug somewhere.

Simple code to see the behavior:

********************
#!/usr/bin/python

from PySide.QtCore import *
from PySide.QtGui import *
import sys

app = QApplication(sys.argv)

model = QStandardItemModel()

model.setHeaderData(0, Qt.Horizontal, "Date")
model.setHeaderData(1, Qt.Horizontal, "Time")
model.setHeaderData(2, Qt.Horizontal, "Proc")

model.setItem(0, 0, QStandardItem("First"))
model.setItem(0, 1, QStandardItem("Second"))
model.setItem(0, 2, QStandardItem("Third"))

table = QTableView()
table.setModel(model)
# table.setColumnWidth(0, 150) # <--- this doesn't work!
table.horizontalHeader().resizeSection(0, 150)


vbox = QVBoxLayout()
vbox.addWidget(table)

dialog = QDialog()
dialog.setLayout(vbox)
dialog.resize(400, 400)
dialog.show()

sys.exit(app.exec_())
********************

Aaron Richiger

unread,
Feb 26, 2011, 6:47:02 AM2/26/11
to pys...@srvrec006.openbossa.org
Hello!

I tried your code and with my OS (Ubuntu 10.10), it works great, both ways give the same result!
Instead you can also try the .resizeColumnsToContents() functions. This is a nice way to adjust the column width to the needed width.

Have a nice day!
Aaron
_______________________________________________ PySide mailing list PyS...@lists.openbossa.org http://lists.openbossa.org/listinfo/pyside

rulfzid

unread,
Feb 26, 2011, 2:15:15 PM2/26/11
to pys...@googlegroups.com, pys...@srvrec006.openbossa.org
On Saturday, February 26, 2011 5:47:02 AM UTC-6, Aaron Richiger wrote:
Hello!

I tried your code and with my OS (Ubuntu 10.10), it works great, both ways give the same result!
Instead you can also try the .resizeColumnsToContents() functions. This is a nice way to adjust the column width to the needed width.

Have a nice day!
Aaron

That's weird, because I'm on Ubuntu 10.10 as well. Glad it's working for you, though. :)
Reply all
Reply to author
Forward
0 new messages