标签:行编辑 info lin column double sig size signal cli
#include <QStringListModel>
#include <QStandardItemModel>
#include <QModelIndex>
初始化
model = new QStandardItemModel(ui->tableView);
model->setColumnCount(6);
model->setHeaderData(0, Qt::Horizontal, "No.");
model->setHeaderData(1, Qt::Horizontal, "程序名");
model->setHeaderData(2, Qt::Horizontal, "是否自动开始");
model->setHeaderData(3, Qt::Horizontal, "结束操作");
model->setHeaderData(4, Qt::Horizontal, "转移数");
model->setHeaderData(5, Qt::Horizontal, "执行周期");
ui->tableView->setModel(model);
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);//
ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);//设置表格只读,不能进行编辑
int width = ui->tableView->width();
ui->tableView->setColumnWidth(0, 60);
ui->tableView->setColumnWidth(1, 100);
ui->tableView->setColumnWidth(2, 120);
ui->tableView->setColumnWidth(3, 120);
ui->tableView->setColumnWidth(4, 120);
ui->tableView->setColumnWidth(5, 120);
ui->tableView->verticalHeader()->setDefaultSectionSize(20);
ui->tableView->verticalHeader()->setHidden(true);
清除
model->removeRows(0, model->rowCount());
设置值
model->setItem(k, 0, new QStandardItem(CS2QS(str)));
model->setItem(k, 1, new QStandardItem(CS2QS(info->m_strName)));
取值
QModelIndex indexOne = ui->tableView->model()->index(ui->tableView->currentIndex().row(), 0);
strPrg = QS2CS(indexOne.data().toString());
双击事件
connect(ui->tableView, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(OnTabViewDoubleClicked(const QModelIndex &)));
void CDlgDevice::OnTabViewDoubleClicked(const QModelIndex &index)
{
QModelIndex indexOne = ui->tableView->model()->index(ui->tableView->currentIndex().row(), 0);
QModelIndex indexTwo = ui->tableView->model()->index(ui->tableView->currentIndex().row(), 1);
strPrg = QS2CS(indexOne.data().toString());
strName = QS2CS(indexTwo.data().toString());
}
标签:行编辑 info lin column double sig size signal cli
原文地址:https://www.cnblogs.com/ShiShouTHS/p/11365397.html