#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFile>
#include <QFileDialog>
#include <QTextStream>
#include <QStandardItemModel>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow{
Q_OBJECT
public:
Ui::MainWindow *ui;
QList<QStringList> csv;
QStandardItemModel *model;
QList<QStandardItem*> standardItemList;
MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_action_Open_triggered();
void checkString(QString &temp, QChar ch = 0);
};
#endif // MAINWINDOW_H
maindwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{
ui->setupUi(this);
QObject::connect(ui->Open, SIGNAL(clicked()),
this,SLOT(on_action_Open_triggered()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_action_Open_triggered()
{
model = new QStandardItemModel(this);
ui->tableView->setModel(model);
QString fileName = QFileDialog::getOpenFileName(this, "Open CSV file",QDir::currentPath(), "csv (*.csv)");
QFile file(fileName);
if (file.open(QIODevice::ReadOnly))
{
QString data = file.readAll();
data.remove(QRegExp("\r")); // remove all carriage return characters
QString temp;
QChar ch;
QTextStream instream (&data);
while (!instream.atEnd())
{
instream >> ch;
if (ch == ',')
checkString(temp, ch);
else if (ch == '\n')
checkString(temp, ch);
else if (instream.atEnd())
{
temp.append(ch);
checkString(temp);
} else
temp.append(ch);
}
}
}
void MainWindow::checkString(QString &temp, QChar ch)
{
if (temp.count("\"")%2 == 0)
{
if (temp.startsWith(QChar('\"')) && temp.endsWith(QChar('\"')))
{
temp.remove(QRegExp("^\""));
temp.remove(QRegExp("\"$"));
}
temp.replace("\"\"", "\"");
QStandardItem *item = new QStandardItem(temp);
standardItemList.append(item);
if (ch != QChar(','))
{
}
temp.clear();
} else
temp.append(ch);
}
void something()
{
qDebug() << standardItemList;
}
我被告知standardItemList,在something函数中未声明的标识符(为什么不在checkstring函数中?)。但是我已经包含了mainwindow.h,它包含了standardItemList的定义。它是否与包含* item的standardItemList有关,并且在checkString
中声明答案 0 :(得分:0)
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFile>
#include <QFileDialog>
#include <QTextStream>
#include <QStandardItemModel>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow{
Q_OBJECT
public:
Ui::MainWindow *ui;
QList<QStringList> csv;
QStandardItemModel *model;
QList<QStandardItem*> standardItemList;
MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_action_Open_triggered();
void checkString(QString &temp, QChar ch = 0);
void something();
};
void MainWindow::something()
{
qDebug() << standardItemList;
}
checkString
是私人会员,因此您无法在课外调用它,因此未定义standardItemList
。