model/view思想简单的说:model存储数据,view显示模型中的数据。
现在实现QTableView 加载文本数据
构造函数内的实现:
m_model = new QStandardItemModel(2,FixedColumnCount,this); //创建数据模型
m_selection = new QItemSelectionModel(m_model, this); //创建选择模型
//为tableView设置数据模型
ui->tableView->setModel(m_model); //设置数据模型
ui->tableView->setSelectionModel(m_selection); //设置选择模型
ui->tableView->setSelectionMode(QAbstractItemView::ExtendedSelection);
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectItems);
打开并加载文本数据:
void MainWindow::on_actOpen_triggered()
{
QString curPath=QCoreApplication::applicationDirPath(); //获取应用程序的路径
//调用打开文件对话框打开一个文件
QString aFileName=QFileDialog::getOpenFileName(this,"打开一个文件",curPath,
"井数据文件(*.txt);;所有文件(*.*)");
if (aFileName.isEmpty())
return; //如果未选择文件,退出
QFile aFile(aFileName); //以文件方式读出
if (aFile.open(QIODevice::ReadOnly | QIODevice::Text)) //以只读文本方式打开文件
{
QTextStream aStream(&aFile); //用文本流读取文件
ui->plainTextEdit->clear(); //清空
while (!aStream.atEnd())
{
QString str=aStream.readLine(); //读取文件的一行
ui->plainTextEdit->appendPlainText(str); //添加到文本框显示
aFileContent.append(str); //添加到 StringList
}
aFile.close(); //关闭文件
int rowCnt=aFileContent.count(); //文本行数,第1行是标题
m_model->setRowCount(rowCnt-1); //实际数据行数
//设置表头
QString header=aFileContent.at(0); //第1行是表头
//一个或多个空格、TAB等分隔符隔开的字符串, 分解为一个StringList
QStringList headerList=header.split(QRegExp("\\s+"),QString::SkipEmptyParts);
m_model->setHorizontalHeaderLabels(headerList); //设置表头文字
//设置表格数据
int j;
QStandardItem *aItem;
for (int i=1;i<rowCnt;i++)
{
QString aLineText=aFileContent.at(i); //获取 数据区 的一行
//一个或多个空格、TAB等分隔符隔开的字符串, 分解为一个StringList
QStringList tmpList=aLineText.split(QRegExp("\\s+"),QString::SkipEmptyParts);
for (j=0;j<FixedColumnCount-1;j++) //tmpList的行数等于FixedColumnCount, 固定的
{ //不包含最后一列
aItem=new QStandardItem(tmpList.at(j)); //创建item
m_model->setItem(i-1,j,aItem); //为模型的某个行列位置设置Item
}
aItem=new QStandardItem(headerList.at(j)); //最后一列是Checkable,需要设置
aItem->setCheckable(true); //设置为Checkable
aItem->setBackground(QBrush(Qt::yellow));
自定义代理功能:
新建一个类
鼠标放到类名上右键——>refactor——>Insert Virtual Functions .....选择需要重构的接口
开始重构接口
QWidget *TComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
Q_UNUSED(option);
Q_UNUSED(index);
QComboBox *editor = new QComboBox(parent);
editor->setEditable(m_editable); //是否可编辑
for (int i=0;i<m_itemList.count();i++) //从字符串列表初始下拉列表
editor->addItem(m_itemList.at(i));
return editor;
}
void TComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QString str = index.model()->data(index, Qt::EditRole).toString();
QComboBox *comboBox = static_cast<QComboBox*>(editor);
comboBox->setCurrentText(str);
}
void TComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QComboBox *comboBox = static_cast<QComboBox*>(editor);
QString str = comboBox->currentText();
model->setData(index, str, Qt::EditRole);
}
void TComboBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
Q_UNUSED(index);
editor->setGeometry(option.rect);
}
在构造函数上调用此类
comboDelegate = new TComboBoxDelegate(this);
QStringList strList;
strList<<"优"<<"良"<<"一般"<<"不合格";
comboDelegate->setItems(strList,false);
ui->tableView->setItemDelegateForColumn(4, comboDelegate);
代理功能实现。