Initial width of column in QTableView via model

qtablewidget column width
qtableview stretch last column
qtableview selectionchanged
qheaderview example
qtableview header
qheaderview width

I have QTableView based on QAbstractTableModel. In QAbstractTableModel reimplemented method headerData() to set column name and width according to the model. But

switch( role )
    case Qt::SizeHintRole       : return QSize( 500, 0 );

has no effect. All columns in the table has the same width(). What I should to do to set initial column width properly.

P.S.: In this question was suggested to use delegates to solve the same issue, but I think headerData() should be used.

Initial width of column in QTableView via model, Because I only return the string from Model, I don't know when the framework will Default Re: set the width of the columns of QTableView. How about using the  QTableView subscribes at signal sectionHandleDoubleClicked of QHeaderView and resizes appropriate column based on cells content size and width of header section. The width of header section is the width returned by headerData with Qt::SizeHintRole if this role is processed otherwise it's calculated based on header section text (content).

After you've populated your model you can set individual policies on columns. This helped me where my table had 4 columns for which I wanted the first two to fill the view and the last two to fit the contents which was fairly narrow and while still having the view fully filled.


ui->tableView->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
ui->tableView->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
ui->tableView->horizontalHeader()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
ui->tableView->horizontalHeader()->setSectionResizeMode(3, QHeaderView::ResizeToContents);

set the width of the columns of QTableView, I ran across this: By default, the cells in a table do not expand to fill by KillerSmath 2 Jul 2018, 12:31. @mzimmers said in formatting a QTableView (​column width): column or row ? Which Type of Model are you using ? To set proportional column widths, I found it easiest to override the resizeEvent as shown, but by subclassing the QTableView in question, rather than the QMainWindow. – jtbr Oct 5 '17 at 2:08

You have a view problem and your are seeking into the model part of your program.

QTableView class has simple methods:

void QTableView::setColumnWidth(int column, int width)


void QTableView::setRowHeight(int row, int height)

formatting a QTableView (column width), The QTableView class provides a default model/view implementation of a table in the table can be found by using rowHeight(); similarly, the width of columns  The Proxy model's headerData() access this self.headerNames variable via source model: sourceModel=self.sourceModel() On if role==Qt.DisplayRole the Proxy retrieves and returns the name of the column to QTableView: return QVariant( sourceModel.headerNames[column] ) There is a right-click menu implemented on header-column right-click.

Add one or both lines for horizontal and/or vertical header:


QTableView Class, The QTableView class provides a default model/view implementation of a table view. similarly, the width of columns can be found using columnWidth(). From my model's data() I am returning a QString for each cell. The problem is that the QTableView is not adjusting the size of cells to show the entire contents of the cell. So how do I resize the column width so that text from each cell in that column is completely visible? Also is there something similar for QTableWidget? Thank You.

QTableView Class, QTableView class provides a default model/view implementation of a table view. rowHeight() ; similarly, the width of columns can be found using PySide.QtGui  How item is wrapped and elided is a function of a view, not model. For example QTableView (and thus QTableWidget) has a method that sets if the text should be wrapped or single line: setWordWrap(bool). To resize a column to fit the text you can either calculate the required size and resize that column via its header:

QTableView, The QTableView class provides a default model/view implementation of a table view. I am attempting to modify the size of the font used in the row and column I've tried this (without success): How to change the cell Using JavaFX UI  void QTableView:: setColumnHidden (int column, bool hide) If hide is true the given column will be hidden; otherwise it will be shown. See also isColumnHidden() and setRowHidden(). void QTableView:: setColumnWidth (int column, int width) Sets the width of the given column to be width. This function was introduced in Qt 4.1. See also columnWidth().

Qtableview size, There are two other Model Views available in Qt5 — QTableView and QTreeView With this arrangement when you index, you index first by row, then by column In numpy the dimensions of an array are available through .shape which  With JTable.AUTO_RESIZE_OFF, the table will not change the size of any of the columns for you, so it will take your preferred setting.If it is your goal to have the columns default to your preferred size, except to have the last column fill the rest of the pane, You have the option of using the JTable.AUTO_RESIZE_LAST_COLUMN autoResizeMode, but it might be most effective when used with

  • I was able to change the header height only.
  • try to double click section handler (space between sections) in the header. looks like it should resize to the size hint width in this case
  • @vahancho - the same to me
  • @Ezee - yes, it works as you say. But what about initial state? May be need to do something with view, to apply width from SizeHintRole?
  • I think that's the only usage of horizontal size hint. But you can inherit from QTableView and apply sizes from the model in setModel.
  • Using hedaerData() is more flexible solution, because I don't bind column width to the some column index. Model says to view what data should be displayed for particular column, and what width of the column should be. Moreover, headerData() works properly for header height, but not for width. May be I should tune some properties of the QTableView, or QHeaderVew, to apply sizeHint properly?