formatting a QTableView (column width)
-
Hi all -
I need to make some formatting changes to a QTableView. The table displays 2 columns of data; I'd like to have these columns equally fill the space allocated for the table.
I ran across this:
By default, the cells in a table do not expand to fill the available space. You can make the cells fill the available space by stretching the last header section. Access the relevant header using horizontalHeader() or verticalHeader() and set the header's stretchLastSection property. To distribute the available space according to the space requirement of each column or row, call the view's resizeColumnsToContents() or resizeRowsToContents() functions.
I don't want to resize to content, so that section doesn't apply. If I use the setStretchLastSection() method, I do fill the space, but not equally. What's the best way to do this?
-
Have you tried to set the ResizeMode of horizontalHeader to Stretch mode ?
https://doc.qt.io/qt-5.11/qheaderview.html#ResizeMode-enum
tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
this mode will prevent the resize of columns and correct fill the space for each column.
-
Beautiful...thanks.
Slightly off topic, but I also need to format the cells (not the header). There appears to be a bunch of ways to do that, but I want to keep it simple.
I thought this (just an example) might work, but it doesn't:
m_model->setData(m_model->index(1, 1), QColor(Qt::red), Qt::BackgroundRole);
Any suggestions?
-
@mzimmers said in formatting a QTableView (column width):
m_model->setData(m_model->index(1, 1), QColor(Qt::red), Qt::BackgroundRole);
- Do you need to set this data just for one specific element or for every data of a specific column or row ?
- Which Type of Model are you using ? (QAbstractItemModel, QSqlQueryModel, QSqlTableModel)
-
@KillerSmath said in formatting a QTableView (column width):
Some of the formatting will apply to all cells; others, perhaps alternating rows. I just thought I'd get it working on one cell and then extrapolate.
I'm using QAbstractItemModel, constructed from QStandardItemModel.
Thanks...
-
This setting is working well:
// from http://doc.qt.io/archives/qt-4.8/qstandarditemmodel.html QStandardItemModel *m_model = new QStandardItemModel(4, 4, this); for (int row = 0; row < 4; ++row) { for (int column = 0; column < 4; ++column) { QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column)); m_model->setItem(row, column, item); } } // set background color m_model->setData(m_model->index(1, 1), QColor(Qt::red), Qt::BackgroundRole);
But you could reimplement the data function of Model to return the Red Color by specific row like
row%2 == 0
(0, 2, 4 , 6) -
-
Hi,
Depending on how you want to format your data a QIdentityProxyModel might be interesting.
-
@mzimmers
To avoid these disnecessary calls of setData, you can return the custom color directly of model using a proxy like @SGaist said or creating a custom model derived of QStandardItemModel and just reimplement the data function to return the color how you want :)Example:
QVariant CustomModel::data(const QModelIndex &index, int role) const { if ((index.row()%2 == 0) && role == Qt::BackgroundRole) // return red background for pair rows return QColor(Qt::red); return QStandardItemModel::data(index, role); }
-
OK, I think I'm starting to get this. Something that doesn't make sense yet, though -- my understanding of the model/view paradigm was that the model contained the data, and the view controlled the presentation of the data. Why, then, am I setting a background color on the model and not on the view?
-
OK, I think I'm starting to get this. Something that doesn't make sense yet, though -- my understanding of the model/view paradigm was that the model contained the data, and the view controlled the presentation of the data. Why, then, am I setting a background color on the model and not on the view?
@mzimmers said in formatting a QTableView (column width):
OK, I think I'm starting to get this. Something that doesn't make sense yet, though -- my understanding of the model/view paradigm was that the model contained the data, and the view controlled the presentation of the data. Why, then, am I setting a background color on the model and not on the view?
This works as the below image:
from Model/View Programming:Delegate controls how the data is showed to user but the delegate receives the information from model.