Reputation: 2777
I am new to qt i am using QStandardItemModel inside QTtableview.
Please suggest,
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
model= new QStandardItemModel(4, 4);
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));
model->setItem(row, column, item);
}
}
ui->tableView->setModel(model);
}
Upvotes: 4
Views: 7166
Reputation: 688
1- There multiple ways to enter headers to a view. I like to do it this way:
QStandardItem *infoItem = new QStandardItem("Info");
infoItem ->setFont(heading);
infoItem ->setToolTip("Scene Object Name and Type");
QStandardItem *fileStatus = new QStandardItem();
fileStatus->setIcon(QIcon( ":/icons/fileStatus" ));
fileStatus->setToolTip("File Status displayed by ...");
QStandardItem *fileDirectory = new QStandardItem();
fileDirectory->setIcon( QIcon( ":/icons/directory" ) );
fileDirectory->setToolTip("File directory");
m_model->setHorizontalHeaderItem( 0, infoItem );
m_model->setHorizontalHeaderItem( 1, fileStatus);
m_model->setHorizontalHeaderItem( 2, fileDirectory );
2- You can either set the stretch last section to true. This will make sure your last column stretches to fill the table every time you resize the table itself.
myView->horizontalHeader()->setStretchLastSection(true);
or you could set on of your desired columns to stretch enough to fill the table. To do this make sure setStretchLastSection is set to false.
myView->horizontalHeader()->setStretchLastSection(false);
myView->horizontalHeader()->setResizeMode(yourDesiredCol, QHeaderView::Stretch);
Upvotes: 2
Reputation: 4286
1 > model->setHorizontalHeaderItem(0, new QStandardItem(tr("Time")));
2 > Like this:
for (int row = 0; row < 4; ++row)
{
QList<QStandardItem *> rowData;
rowData << new QStandardItem(QString("row %1, column %2").arg(row).arg(0))
rowData << new QStandardItem(QString("row %1, column %2").arg(row).arg(1))
rowData << new QStandardItem(QString("row %1, column %2").arg(row).arg(2))
rowData << new QStandardItem(QString("row %1, column %2").arg(row).arg(3))
model->appendRow(rowData);
}
Upvotes: 4
Reputation: 6594
Use setHorizontalHeaderLabels() and setVerticalHeaderLabels() or setHorizontalHeaderItem() and setVerticalHeaderItem() methods.
The vertical headers are the column names and horizontal headers are the row names.
setVerticalHeaderItem is useful if you want to display more information than a simple text (like a icon or colored text).
Upvotes: 0