(158) 理论讲解 :
++给出这些类的继承关系 :
++本例题的搭建效果 :
(159)本节课,老师不再具体搭建界面。制作 QAction 的过程略。随后给出程序运行的效果图。
++给出本程序的构造函数 :
++太长,给出构造函数的代码版 :
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
//QStandardItemModel(int rows, int columns, QObject * parent = nullptr); 6列
m_model = new QStandardItemModel(2, FixedColumnCount, this); //创建数据模型
m_selection = new QItemSelectionModel(m_model, this); //创建选择模型
//QItemSelectionModel(QAbstractItemModel * model, QObject * parent);
//为tableView设置数据模型
ui->tableView->setModel(m_model); //关联模型与视图
ui->tableView->setSelectionModel(m_selection); //设置选择模型
//void QAbstractItemView::setSelectionMode(QAbstractItemView::SelectionMode mode);
ui->tableView->setSelectionMode(QAbstractItemView::ExtendedSelection);
//支持 shift多选与 ctrl多选。
//void QAbstractItemView::setSelectionBehavior(//单选,还是列选,行选。
// QAbstractItemView::SelectionBehavior behavior);
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectItems);
setCentralWidget(ui->splitter); //这一行可以不要
//创建状态栏组件
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(m_selection, &QItemSelectionModel::currentChanged,
this , &MainWindow::do_currentChanged);
//connect(theSelection,SIGNAL(currentChanged(QModelIndex,QModelIndex)),
// this,SLOT(do_currentChanged(QModelIndex,QModelIndex)));
} // 完结 MainWindow(QWidget * parent) : QMainWindow(parent), ui(new Ui::MainWindow);
++如何响应视图里的条目变化呢 :
++实现对表格中条目进行响应的信号函数 :
(160) 以下开始实现工具栏里的 QAction 的信号与槽函数 :
++ 打开文件按钮QAction :
++调用了下面的函数 :
++又调用了下面的函数 :
++给出上面的填充表格函数的代码版 :
//用形参里的数据来填充表格模型
void MainWindow::iniModelData(QStringList & aFileContent) //左值引用
{ int rowCnt = aFileContent.size(); //文本行数,第1行是标题
m_model->setRowCount(rowCnt - 1); //设置表格里的数据行数
QString header = aFileContent.at(0); //准备设置表格的表头 //第1行是表头
//\\s+:\s匹配任意空白字符,\\转义,+表前面的元素(这里是 \s)可以出现一次或多次。
//老师说。也可以用原生正则表达式, raw: R"(\s+)", 好处是不用转义了。
QStringList headerList = header.split( QRegularExpression("\\s+"),
Qt::SkipEmptyParts );
//QStringList QString::split(const QRegularExpression & sep, //正则表达式
// Qt::SplitBehavior behavior = Qt::KeepEmptyParts ) const;
m_model->setHorizontalHeaderLabels(headerList); //设置水平表头的文字
//void QStandardItemModel::setHorizontalHeaderLabels(QStringList & labels);
//实际表头:测深(m) 垂深(m) 方位(°) 总位移(m) 固井质量 测井取样(此项是数字需转换为复选文本)
int j; QStandardItem * aItem; //按行设置表格里的每个数据
for (int i = 1; i < rowCnt; i++) //宏 FixedColumnCount = 6列。
{ QString aLineText = aFileContent.at(i); //获取源数据中的一行
QStringList tmpList = aLineText.split( QRegularExpression(R"(\s+)"),
Qt::SkipEmptyParts );
for (j = 0; j < FixedColumnCount - 1; j++) //不包含最后一列
{ aItem = new QStandardItem( tmpList.at(j) ); //创建item
m_model->setItem(i-1, j, aItem); //设置Item
}
//void QStandardItemModel::setItem(int row, int column, QStandardItem * item);
aItem = new QStandardItem( headerList.at(j) ); //注意是读取的水平表头里的文本
aItem->setCheckable(true); //最后一列成为了复选框
//void QStandardItem::setBackground(QBrush & abrush);
//QBrush(Qt::GlobalColor color, Qt::BrushStyle bs = Qt::SolidPattern);
aItem->setBackground(QBrush(Qt::yellow)); //为最后一列的复选框设置单独的背景色
if ( tmpList.at(j) == "0" )
aItem->setCheckState(Qt::Unchecked ); //根据文本数据设置check状态
else
aItem->setCheckState(Qt::Checked );
m_model->setItem(i-1, j , aItem); //设置为表格中的Item
}
}
++至此,模型数据从文件里建立成功了,如图 :
++应该是把文本文件和可执行程序放在同一个目录里就可以 :
(161)
(162)
谢谢