#include <QtGui>#include <QTextStream>#include <QTableWidget>#include <QStandardItemModel>//#include <QTableWidgetItem>#include <QStringList>//class MainForm : public QMainWindow, public Ui::MainWindow{Q_OBJECTpublic: MainForm( QWidget * parent = 0, Qt::WFlags f = 0 ); QStringList lst; QStandardItemModel* model;private slots: void OpenFile();};#endif
#include "mainform.h"//MainForm::MainForm( QWidget * parent, Qt::WFlags f) : QMainWindow(parent, f){ setupUi(this); connect(pushButton, SIGNAL(clicked()), this, SLOT(OpenFile())); model=new QStandardItemModel(this); view->setModel(model);}//void MainForm::OpenFile(){QString fileName = QFileDialog::getOpenFileName(this);QFile file (fileName); if (file.open(QIODevice::ReadOnly)) { QTextStream stream(&file); stream.setCodec("CP-866"); int siz=0, size_file=0, f=0, row=0, col=0; QString str; QString fff, ggg; QList<QString> gg; QStringList fields; // dlya sozdaniya okna progressa siz = file.size(); size_file = siz/190; // razmer % dlya progressDialog QProgressDialog* pprd = new QProgressDialog(tr("Progressing the data..."), tr("&Cancel"), 0, size_file,this); pprd->setModal(true); // ustanovka modalnosti okna pprd->setMinimumDuration(0); pprd->setWindowTitle("Please Wait"); model->clear(); model->setColumnCount(f); view->setUpdatesEnabled(false); QTime timer(0,0,0,0); QTime tim(0,0,0,0); timer.start(); //char buf[256]; while (!stream.atEnd()) { str = stream.readLine(); ///qint64 lineLength = file.readLine(buf, sizeof(buf)); ///str = buf; //str = stream.readLine(); //chtenie stroki str = str.simplified(); //ubiraet pustie probeli v stroke if (row<=3) fields = str.split(","); if (row>3) fields = str.split(' '); model->setRowCount(row+1); gg = fields; for (col=0; col<=(fields.size()-1); ++col) { fff = gg.at(col); QStandardItem *item = new QStandardItem(QString(fff)); model->setItem(row, col, item); } pprd->setValue(row) ; // progress qApp->processEvents(); if (pprd->wasCanceled()) break; ++row; } if (stream.status() != QTextStream::Ok) { qDebug() << "╨Ю╤И╨╕╨▒╨║╨░ ╤З╤В╨╡╨╜╨╕╤П ╤Д╨░╨╣╨╗╨░"; } tim = tim.addMSecs(timer.elapsed()); this->setWindowTitle(QString(tim.toString("m:s:z"))); delete pprd; //unichtozhenie okna QProgressDialogview->setUpdatesEnabled(true);file.close(); }
C++ (Qt)void MainWindow::OpenFile() {QString fileName = QFileDialog::getOpenFileName(this); QFile file (fileName);if (file.open(QIODevice::ReadOnly)) { QTextStream stream(&file); stream.setCodec("CP-866");int RowC=0; // kol-vo strok while (!stream.atEnd()) { stream.readLine(); RowC++; } stream.seek(0);//Go to begin of file QTableWidget* tbl = new QTableWidget; QStringList fields; QString str1 = stream.readLine(); //chtenie stroki QString str2 = stream.readLine(); //chtenie stroki QString str3 = stream.readLine(); //chtenie stroki QString str4 = stream.readLine(); //chtenie stroki fields = str4.split(","); int f = fields.size(); tbl->setColumnCount(f); tbl->setRowCount(RowC-4); tbl->setHorizontalHeaderLabels(fields); QProgressDialog* pprd = new QProgressDialog(tr("Progressing the data..."), tr("&Cancel"), 0, RowC,this); pprd->setModal(true); // ustanovka modalnosti okna pprd->setMinimumDuration(0); pprd->setWindowTitle("Please Wait"); QTime timer(0,0,0,0); QTime tim(0,0,0,0); timer.start(); RowC = 0; while (!stream.atEnd()) { QString str = stream.readLine(); //chtenie stroki str = str.simplified(); //ubiraet pustie probeli v stroke fields = str.split(' '); for (int i=0; i<16; ++i) { QString fff = fields.at(i); QTableWidgetItem *item = new QTableWidgetItem; item->setText(fff); tbl->setItem(RowC, i, item); } ++RowC; pprd->setValue(RowC) ; // progress if (pprd->wasCanceled()) break; qApp->processEvents(); // chtobi prilozhenie ne zavisalo} if (stream.status() != QTextStream::Ok) { qDebug() << "Ошибка чтения файла"; } file.close(); tim = tim.addMSecs(timer.elapsed()); this->setWindowTitle(QString(tim.toString("m:s:z"))); delete pprd; //unichtozhenie okna QProgressDialog tbl->show(); } // if fileopen }