Qt第十二天

QStandardItemModel的使用

实现功能:
打开一个文本文件,该文件为二维数据文件,通过字符串处理获取表头和各行各列数据,导入到QStandardItemModel数据模型
编辑修改数据模型的数据
设置数据模型中某项的不同角色的数据
通过QItemSelectionModel获取视图组件上当前单元格
将数据模型中的数据显示到QPlainTextEdit组件中
将修改后的数据模型另存为一个文本文件

mainwindow.h文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H


#include <QMainWindow>
#include <QLabel>
#include <QStandardItemModel>
#include <QItemSelectionModel>


namespace Ui {
class MainWindow;
}




#define FixedColumCount   6  //固定列数为6
class MainWindow : public QMainWindow
{
    Q_OBJECT


public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();


private:
    Ui::MainWindow *ui;




    QLabel *LabCurFile;//表示当前文件
    QLabel *LabCellPos;//当前单元格行列号
    QLabel *LabCellText;//当前单元格内容
    QStandardItemModel  *theModel;//加入头文件<QStandardItemModel>
                                  //数据模型
    QItemSelectionModel *theSelection;//加入头文件<QItemSelectionModel>
                                      //选择数据模型
    void iniModelFromStringList(QStringList&);//从StringList初始化数据模型


private slots:
    //自定义槽函数
    void on_currentChanged(const QModelIndex &current,const QModelIndex &previous);
    void on_actOpen_triggered();
    void on_actAppend_triggered();
    void on_actInsert_triggered();
    void on_actDelete_triggered();
    void on_actAlignLeft_triggered();
    void on_actAlignCenter_triggered();
    void on_actAlignRight_triggered();
    void on_actFontBold_triggered();
    void on_actFontBold_triggered(bool checked);
    void on_actModelData_triggered();
    void on_actSave_triggered();
};


#endif // MAINWINDOW_H

mainwindow.cpp文件

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTableView>
#include <QFileDialog>
#include <QTextStream>




MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //进行界面初始化,数据模型和选择模型的创建,信号与槽的关联
    setCentralWidget(ui->splitter);
    theModel=new QStandardItemModel(2,FixedColumCount,this);//2*6的数据模型
    theSelection=new QItemSelectionModel(theModel);//选择模型
    connect(theSelection,SIGNAL(currentChanged(QModelIndex,QModelIndex)),this,SLOT(on_currentChanged(QModelIndex,QModelIndex)));
    //Selection的currentChanged信号()与on_currentChanged()关联
    ui->tableView->setModel(theModel);
    ui->tableView->setSelectionModel(theSelection);
    ui->tableView->setSelectionMode(QAbstractItemView::ExtendedSelection);//允许选中多行
    ui->ta
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值