QTableView
model提供数据 view提供视图 view用来显示model的数据 必须将model绑定到某个view中才能显示
QStandardItemModel* model = new QStandardItemModel();
QStringList labels = QObject::trUtf8("频率,功率,误差").simplified().split(",");
model->setHorizontalHeaderLabels(labels); //定义item
QStandardItem* item = ;
for(int i = ;i < ;i++){
item = new QStandardItem(QString("%1").arg(i));
model->setItem(i,,item);
item = new QStandardItem(QString("%1").arg(i*));
model->setItem(i,,item);
item = new QStandardItem(QString("%1").arg(i*));
model->setItem(i,,item);
}
ui->tableView->setModel(model);
ui->tableView->show();
QTableWidget
//三列,只设置前两列宽度,剩下一列自适应到最大宽度
ui->tableWidget->setColumnWidth(, );
ui->tableWidget->setColumnWidth(, );
ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableWidget->setSelectionMode( QAbstractItemView::SingleSelection);