Barry Michael Doyle Barry Michael Doyle - 1 year ago 76
C++ Question

Qt: Can't set and display a QTabelView Horizontal Header

I'm developing a Qt Application using Qt Creator and I've placed a

QTableView
on a
QDialog
.

My
QTableView
objectName is
tvStock
.

I would like
tvStock
to have three columns with the following horizontal headers:
Stock ID
,
Description
and
Price
.

I have no idea how to add these horizontal headers. Any help would be much appreciated!

Additional Notes:

I'm not sure how to use
QTableView::setHorizontalHeader
.

This is as far as I get with that:

ui->tvStock->setHorizontalHeader(/* I'm lost here */);

Answer Source

Since it's the model that's supplying the data it should really be the model that provides the column labels.

Assuming the model isn't read only you should be able to use QAbstractItemModel::setHeaderData with something like...

my_model.setHeaderData(0, Qt::Horizontal, "Stock ID", Qt::DisplayRole);
my_model.setHeaderData(1, Qt::Horizontal, "Description", Qt::DisplayRole);
my_model.setHeaderData(2, Qt::Horizontal, "Price", Qt::DisplayRole);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download