class CustomTableModel : public QAbstractTableModel{ Q_OBJECT QList<QStringList> fileDataList;public: CustomTableModel(QObject *parent = NULL):QAbstractTableModel(parent) {} int rowCount(const QModelIndex &parent = QModelIndex()) const { return fileDataList.size(); } int columnCount(const QModelIndex &parent = QModelIndex()) const { if(!fileDataList.isEmpty()) return fileDataList.at(0).size(); return -1; } QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const { if(!index.isValid()) return QVariant(); if(index.row() >= fileDataList.size() || index.row() < 0) return QVariant(); if(role == Qt::DisplayRole || role == Qt::EditRole) return fileDataList.at(index.row()).at(index.column()); else return QVariant(); } bool setData(const QModelIndex &index, const QVariant &value, int role) { if(index.isValid() && role == Qt::EditRole) { fileDataList[index.row()].replace(index.column(), value.toString()); emit dataChanged(index, index); return true; } return false; } bool insertRows(int row, int count, const QModelIndex &parent) { beginInsertRows(QModelIndex(), row, row + count); endInsertRows(); }};class Reader : public QObject{ Q_OBJECTpublic: Reader(QObject *parent = NULL); void readFile(const QString&);signals: void fileData(QStringList&);};Reader::Reader(QObject *parent):QObject(parent){}void Reader::readFile(const QString &path){ QFile file(path); if(file.open(QIODevice::ReadOnly)) { while(!file.atEnd()) { QString str(file.readLine()); QStringList strList(str.split(" ", QString::SkipEmptyParts)); emit fileData(strList); } file.close(); }}class MainWindow : public QMainWindow{ Q_OBJECT QAction *pActionOpen; QAction *pActionDraw; QAction *pActionClear; QToolBar *pToolBar; QStatusBar *pStatusBar; QVBoxLayout *pVLayout; QHBoxLayout *pHLayout; QWidget *pMainWidget; QTableView *pTableView; Reader *pReader; CustomTableModel *pModel; CustomHeader *pCustomHeader; int rCount;public: MainWindow(QWidget *parent = NULL);public slots: void openFileDlg(); void clearTable(){} void drawCurve(){} void setFileData(QStringList&);};MainWindow::MainWindow(QWidget *parent):QMainWindow(parent), pActionOpen(NULL), pActionDraw(NULL), pActionClear(NULL), pToolBar(new QToolBar("Linker")), pStatusBar(new QStatusBar), pVLayout(new QVBoxLayout), pHLayout(new QHBoxLayout), pMainWidget(new QWidget), pTableView(new QTableView), pReader(new Reader), pModel(new CustomTableModel), pCustomHeader(new CustomHeader), rCount(0){ QRect screen = QApplication::desktop()->screenGeometry(QDesktopWidget().screenNumber(this)); setGeometry(screen.width()/4, screen.height()/4, screen.width()/2, screen.height()/2); setWindowTitle("Telemetry"); setWindowIcon(QIcon("../img/table_gear.png")); setStatusBar(pStatusBar); setCentralWidget(new QTableWidget);//only for start window view addToolBar(Qt::TopToolBarArea, pToolBar); pActionOpen = pToolBar->addAction(QIcon("../img/folder_explore.png"), "Open telemetry file...", this, SLOT(openFileDlg())); pActionOpen->setShortcut(QKeySequence(tr("Ctrl+o"))); pActionClear = pToolBar->addAction(QIcon("../img/cross.png"), "Clear table", this, SLOT(clearTable())); pActionDraw = pToolBar->addAction(QIcon("../img/chart_curve.png"), "Draw data from checked columns...", this, SLOT(drawCurve())); pActionDraw->setShortcut(QKeySequence(tr("Ctrl+d"))); connect(pReader, SIGNAL(fileData(QStringList&)), this, SLOT(setFileData(QStringList&))); //connect(pReader, SIGNAL(fileData(QStringList&)), pModel, SLOT(insertFileDataList(QStringList&)));}void MainWindow::openFileDlg(){ QString sourceFileName = QFileDialog::getOpenFileName(this, tr("Open file..."), QString(), tr(";; All files(*.*)") ); if(sourceFileName.contains("_TMF")) { qApp->processEvents(); pReader->readFile(sourceFileName); } else if(sourceFileName.isEmpty()) return; else { QMessageBox box(QMessageBox::Information, "File open message", "Wrong file format", QMessageBox::Ok); box.exec(); }}void MainWindow::setFileData(QStringList &strList){ QStringList list = strList; qDebug()<<"new QStringList:\n"; qDebug()<<"row insert flag: " << pModel->insertRow(rCount, QModelIndex()); qDebug()<<"columns insert flag: " <<pModel->insertColumns(0, list.size(), QModelIndex()); for(int i=0;i<list.size();++i) { qDebug()<<"column insert flag: " << pModel->insertColumn(i, QModelIndex()); pModel->setData(pModel->index(rCount, i, QModelIndex()), "123", Qt::EditRole); qDebug()<<"QVariant: " << pModel->data(pModel->index(rCount, i, QModelIndex()), Qt::DisplayRole).toString(); } qDebug()<<"\n"; ++rCount;}