QStandardItemModel 是標准的以項數據為單位的基於M/V模型的一種標准數據管理方式,Model/View 是Qt中的一種數據編排結構,其中Model代表模型,View代表視圖,視圖是顯示和編輯數據的界面組件,而模型則是視圖與原始數據之間的接口,通常該類結構都是用在數據庫中較多,例如模型結構負責讀取或寫入數據庫,視圖結構則負責展示數據,其條理清晰,編寫代碼便於維護。
QStandardItemModel組件通常會配合TableView組件一起使用,當數據庫或文本中的記錄發生變化時會自動同步到組件中,首先繪制UI界面。

其次綁定頂部ToolBar菜單,分別對菜單增加對應的功能屬性的描述等。

初始化構造函數: 當程序運行時,我們需要對頁面中的控件逐一初始化,並將Table表格與模型通過調用ui->tableView->setModel(model)進行綁定。
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include <QLabel>
#include <QStandardItem>
#include <QItemSelectionModel>
#include <QFileDialog>
#include <QTextStream>
#include <QList>
// 默認構造函數
// https://www.cnblogs.com/lyshark
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 初始化部分
model = new QStandardItemModel(3,FixedColumnCount,this); // 數據模型初始化
selection = new QItemSelectionModel(model); // Item選擇模型
// 為TableView設置數據模型
ui->tableView->setModel(model); // 設置數據模型
ui->tableView->setSelectionModel(selection); // 設置選擇模型
// 默認禁用所有Action選項,只保留打開
ui->actionSave->setEnabled(false);
ui->actionView->setEnabled(false);
ui->actionAppend->setEnabled(false);
ui->actionDelete->setEnabled(false);
ui->actionInsert->setEnabled(false);
// 創建狀態欄組件,主要來顯示單元格位置
LabCurFile = new QLabel("當前文件:",this);
LabCurFile->setMinimumWidth(200);
LabCellPos = new QLabel("當前單元格:",this);
LabCellPos->setMinimumWidth(180);
LabCellPos->setAlignment(Qt::AlignHCenter);
LabCellText = new QLabel("單元格內容:",this);
LabCellText->setMinimumWidth(150);
ui->statusbar->addWidget(LabCurFile);
ui->statusbar->addWidget(LabCellPos);
ui->statusbar->addWidget(LabCellText);
//選擇當前單元格變化時的信號與槽
connect(selection,SIGNAL(currentChanged(QModelIndex,QModelIndex)),this,SLOT(on_currentChanged(QModelIndex,QModelIndex)));
}
MainWindow::~MainWindow()
{
delete ui;
}
初始化時同時需要綁定一個on_currentChanged(QModelIndex,QModelIndex)信號,當用戶選中指定單元格時相應用戶。
// 選擇單元格變化時的響應,通過在構造函數中綁定信號和槽函數實現觸發
// https://www.cnblogs.com/lyshark
void MainWindow::on_currentChanged(const QModelIndex ¤t, const QModelIndex &previous)
{
Q_UNUSED(previous);
if (current.isValid()) //當前模型索引有效
{
LabCellPos->setText(QString::asprintf("當前單元格:%d行,%d列",current.row(),current.column())); //顯示模型索引的行和列號
QStandardItem *aItem;
aItem=model->itemFromIndex(current); //從模型索引獲得Item
this->LabCellText->setText("單元格內容:"+aItem->text()); //顯示item的文字內容
}
}
當頁面被初始化時,默認界面如下:

打開並填充組件: 當工具欄中打開文件被點擊后則觸發,打開文件時通過aFile.open打開,循環讀入文件,並將文件中的內容逐行追加到QStringList fFileContent中,當追加完畢后,直接調用iniModelFromStringList(fFileContent);完成對頁面TableView組件的初始化,並設置其他控件狀態為可點擊。
void MainWindow::on_actionOpen_triggered()
{
QString curPath=QCoreApplication::applicationDirPath(); // 獲取應用程序的路徑
// 調用打開文件對話框打開一個文件
// https://www.cnblogs.com/lyshark
QString aFileName=QFileDialog::getOpenFileName(this,"打開一個文件",curPath,"數據文件(*.txt);;所有文件(*.*)");
if (aFileName.isEmpty())
{
return; // 如果未選擇文件則退出
}
QStringList fFileContent; // 文件內容字符串列表
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); // 添加到文本框顯示
fFileContent.append(str); // 添加到StringList
}
aFile.close(); // 關閉文件
iniModelFromStringList(fFileContent); // 從StringList的內容初始化數據模型
}
// 打開文件完成后,就可以將Action全部開啟了
ui->actionSave->setEnabled(true);
ui->actionView->setEnabled(true);
ui->actionAppend->setEnabled(true);
ui->actionDelete->setEnabled(true);
ui->actionInsert->setEnabled(true);
// 打開文件成功后,設置狀態欄當前文件列
this->LabCurFile->setText("當前文件:"+aFileName);//狀態欄顯示
}
如上iniModelFromStringList(fFileContent);函數是后期增加的,我們需要自己實現,該函數的作用是從傳入的StringList中獲取數據,並將數據初始化到TableView模型中,實現代碼如下。
void MainWindow::iniModelFromStringList(QStringList& aFileContent)
{
int rowCnt=aFileContent.count(); // 文本行數,第1行是標題
model->setRowCount(rowCnt-1); // 實際數據行數,要在標題上減去1
// 設置表頭
QString header=aFileContent.at(0); // 第1行是表頭
// 一個或多個空格、TAB等分隔符隔開的字符串、分解為一個StringList
// https://www.cnblogs.com/lyshark
QStringList headerList=header.split(QRegExp("\\s+"),QString::SkipEmptyParts);
model->setHorizontalHeaderLabels(headerList); // 設置表頭文字
// 設置表格中的數據
int x = 0,y = 0;
QStandardItem *Item;
// 有多少列數據就循環多少次
// https://www.cnblogs.com/lyshark
for(x=1; x < rowCnt; x++)
{
QString LineText = aFileContent.at(x); // 獲取數據區的一行
// 一個或多個空格、TAB等分隔符隔開的字符串、分解為一個StringList
QStringList tmpList=LineText.split(QRegExp("\\s+"),QString::SkipEmptyParts);
// 循環列數,也就是循環FixedColumnCount,其中tmpList中的內容也是.
for(y=0; y < FixedColumnCount-1; y++)
{
Item = new QStandardItem(tmpList.at(y)); // 創建item
model->setItem(x-1,y,Item); // 為模型的某個行列位置設置Item
}
// 最后一個數據需要取出來判斷,並單獨設置狀態
Item=new QStandardItem(headerList.at(y)); // 最后一列是Checkable,需要設置
Item->setCheckable(true); // 設置為Checkable
// 判斷最后一個數值是否為0
if (tmpList.at(y) == "0")
Item->setCheckState(Qt::Unchecked); // 根據數據設置check狀態
else
Item->setCheckState(Qt::Checked);
model->setItem(x-1,y,Item); //為模型的某個行列位置設置Item
}
}
初始化組件后效果如下:

實現添加一行數據: 為TableView添加一行數據,在文件末尾插入。
void MainWindow::on_actionAppend_triggered()
{
QList<QStandardItem *> ItemList; // 創建臨時容器
QStandardItem *Item;
// 模擬添加一列的數據
for(int x=0; x<FixedColumnCount-1; x++)
{
Item = new QStandardItem("測試(追加行)"); // 循環創建每一列
ItemList << Item; // 添加到鏈表中
}
// 創建最后一個列元素,由於是選擇框所以需要單獨創建
// https://www.cnblogs.com/lyshark
// 1.獲取到最后一列的表頭下標,最后下標為6
QString str = model->headerData(model->columnCount()-1,Qt::Horizontal,Qt::DisplayRole).toString();
Item=new QStandardItem(str); // 創建 "是否合格" 字段
Item->setCheckable(true); // 設置狀態為真
ItemList << Item; // 最后一個選項追加進去
model->insertRow(model->rowCount(),ItemList); // 插入一行,需要每個Cell的Item
QModelIndex curIndex=model->index(model->rowCount()-1,0); // 創建最后一行的ModelIndex
selection->clearSelection(); // 清空當前選中項
selection->setCurrentIndex(curIndex,QItemSelectionModel::Select); // 設置當前選中項為當前選擇行
}
插入代碼演示效果:

實現插入一行數據: 為TableView插入一行數據(在文件任意位置插入數據)
// https://www.cnblogs.com/lyshark
void MainWindow::on_actionInsert_triggered()
{
QList<QStandardItem*> ItemList; // QStandardItem的列表類
QStandardItem *Item;
// 模擬插入前五列數據
for(int i=0;i<FixedColumnCount-1;i++)
{
Item= new QStandardItem("測試(插入行)"); // 新建一個QStandardItem
ItemList << Item; // 添加到列表類
}
QString str; // 獲取表頭文字
str=model->headerData(model->columnCount()-1,Qt::Horizontal,Qt::DisplayRole).toString();
Item=new QStandardItem(str); // 創建Item
Item->setCheckable(true); // 設置為可使用CheckBox
ItemList<<Item; // 添加到列表類
QModelIndex curIndex=selection->currentIndex(); // 獲取當前選中項的索引
model->insertRow(curIndex.row(),ItemList); // 在當前行的前面插入一行
selection->clearSelection(); // 清除當前選中項
selection->setCurrentIndex(curIndex,QItemSelectionModel::Select); // 設置當前選中項為當前選擇行
}
插入代碼演示效果:

實現刪除一行數據: 刪除數據之前需要通過selection->currentIndex()確定當前選中行,並通過model->removeRow()移除即可。
// https://www.cnblogs.com/lyshark
void MainWindow::on_actionDelete_triggered()
{
QModelIndex curIndex = selection->currentIndex(); // 獲取當前選擇單元格的模型索引
// 先判斷是不是最后一行
if (curIndex.row()==model->rowCount()-1)
{
model->removeRow(curIndex.row()); //刪除最后一行
}
else
{
model->removeRow(curIndex.row());//刪除一行,並重新設置當前選擇行
selection->setCurrentIndex(curIndex,QItemSelectionModel::Select);
}
}
刪除代碼效果演示:

實現字體數據對齊: 表格中的字體可以實現多種對其方式,對齊方式分為 居中對齊,左對齊,右對齊 三種。
// 設置表格居中對齊
void MainWindow::on_pushButton_clicked()
{
if (!selection->hasSelection())
return;
QModelIndexList selectedIndex=selection->selectedIndexes();
QModelIndex Index;
QStandardItem *Item;
for (int i=0; i<selectedIndex.count(); i++)
{
Index=selectedIndex.at(i);
Item=model->itemFromIndex(Index);
Item->setTextAlignment(Qt::AlignHCenter);
}
}
// 設置表格左對齊
// https://www.cnblogs.com/lyshark
void MainWindow::on_pushButton_2_clicked()
{
if (!selection->hasSelection()) //沒有選擇的項
return;
//獲取選擇的單元格的模型索引列表,可以是多選
QModelIndexList selectedIndex=selection->selectedIndexes();
for (int i=0;i<selectedIndex.count();i++)
{
QModelIndex aIndex=selectedIndex.at(i); //獲取其中的一個模型索引
QStandardItem* aItem=model->itemFromIndex(aIndex);//獲取一個單元格的項數據對象
aItem->setTextAlignment(Qt::AlignLeft);//設置文字對齊方式
}
}
// 設置表格右對齊
void MainWindow::on_pushButton_3_clicked()
{
if (!selection->hasSelection())
return;
QModelIndexList selectedIndex=selection->selectedIndexes();
QModelIndex aIndex;
QStandardItem *aItem;
for (int i=0;i<selectedIndex.count();i++)
{
aIndex=selectedIndex.at(i);
aItem=model->itemFromIndex(aIndex);
aItem->setTextAlignment(Qt::AlignRight);
}
}
對齊代碼效果演示:

實現字體數據加粗: 將選中行的字體進行加粗顯示。
// 設置字體加粗顯示
// https://www.cnblogs.com/lyshark
void MainWindow::on_pushButton_4_clicked()
{
if (!selection->hasSelection())
return;
//獲取選擇單元格的模型索引列表
QModelIndexList selectedIndex=selection->selectedIndexes();
for (int i=0;i<selectedIndex.count();i++)
{
QModelIndex aIndex=selectedIndex.at(i); //獲取一個模型索引
QStandardItem* aItem=model->itemFromIndex(aIndex);//獲取項數據
QFont font=aItem->font(); //獲取字體
font.setBold(true); //設置字體是否粗體
aItem->setFont(font); //重新設置字體
}
}
加粗代碼效果演示:

實現保存文件: 當保存文件被點擊后觸發,通過便利TableWidget模型組件中的數據,並將數據通過aStream << str << "\n";寫出到記事本中。
// https://www.cnblogs.com/lyshark
// 【保存文件】
void MainWindow::on_actionSave_triggered()
{
QString curPath=QCoreApplication::applicationDirPath(); // 獲取應用程序的路徑
// 調用打開文件對話框選擇一個文件
QString aFileName=QFileDialog::getSaveFileName(this,tr("選擇一個文件"),curPath,"數據文件(*.txt);;所有文件(*.*)");
if (aFileName.isEmpty()) // 未選擇文件則直接退出
return;
QFile aFile(aFileName);
// 以讀寫、覆蓋原有內容方式打開文件
if (!(aFile.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate)))
return;
QTextStream aStream(&aFile); // 用文本流讀取文件
QStandardItem *Item;
QString str;
int x = 0,y = 0;
ui->plainTextEdit->clear();
// 獲取表頭文字
for (x=0; x<model->columnCount(); x++)
{
Item=model->horizontalHeaderItem(x); // 獲取表頭的項數據
str= str + Item->text() + "\t\t"; // 以TAB制表符隔開
}
aStream << str << "\n"; // 文件里需要加入換行符\n
ui->plainTextEdit->appendPlainText(str);
// 獲取數據區文字
for ( x=0; x < model->rowCount(); x++)
{
str = "";
for( y=0; y < model->columnCount()-1; y++)
{
Item=model->item(x,y);
str=str + Item->text() + QString::asprintf("\t\t");
}
// 對最后一列需要轉換一下,如果判斷為選中則寫1否則寫0
Item=model->item(x,y);
if (Item->checkState()==Qt::Checked)
str= str + "1";
else
str= str + "0";
ui->plainTextEdit->appendPlainText(str);
aStream << str << "\n";
}
}
// 【導出Txt文件】:將TableView中的數據導出到PlainTextEdit顯示
void MainWindow::on_actionView_triggered()
{
ui->plainTextEdit->clear();
QStandardItem *Item;
QString str;
//獲取表頭文字
int x=0,y=0;
for (x=0; x<model->columnCount(); x++)
{ //
Item=model->horizontalHeaderItem(x);
str= str + Item->text() + "\t";
}
ui->plainTextEdit->appendPlainText(str);
//獲取數據區的每行
for (x=0; x<model->rowCount(); x++)
{
str="";
for(y=0; y<model->columnCount()-1; y++)
{
Item=model->item(x,y);
str= str + Item->text() + QString::asprintf("\t");
}
Item=model->item(x,y);
if (Item->checkState()==Qt::Checked)
str= str + "1";
else
str= str + "0";
ui->plainTextEdit->appendPlainText(str);
}
}
文件保存后如下:

