クラスオブジェクトのベクトルを生成してTableViewに出力するコードを実行しているQtに問題があります。コードを実行するたびに、ファイルの最初の行のみが読み取られます。同じコードを入れて、TableView 部分を標準の cout に変更すると、すべて正常に動作します。
showReservations::showReservations(QWidget *parent) :
QDialog(parent),
ui(new Ui::showReservations)
{
ui->setupUi(this);
std::vector<reservation> currReservations;
//Initalize the vector set
//Runs the ReadFlightSchedule with the vector set and returns a 1 if there is a
//failure and 0 if all is good
if (ReadReservations(currReservations) == 1)
{
//This sets a label to red showing the user an error reading the file
ui->label->setText("<font color = 'red'>Error Reading File! Oops</font>");
}
else {
//hides the error label since the file read good.
ui->label->hide();
//Generates the column header names
QStandardItemModel *model = new QStandardItemModel(currReservations.size(), 5, this);
model->setHorizontalHeaderItem(0, new QStandardItem(QString("Flier Name")));
model->setHorizontalHeaderItem(1, new QStandardItem(QString("Flight Date")));
model->setHorizontalHeaderItem(2, new QStandardItem(QString("Flight Number")));
model->setHorizontalHeaderItem(3, new QStandardItem(QString("Seat Designation")));
model->setHorizontalHeaderItem(4, new QStandardItem(QString("Reservation ID")));
//Sets the values from the vector sets to the appropriate column and row sets.
for (int i = 0; i < currReservations.size(); i++)
{
model->setItem(i, 0, new QStandardItem(QString::fromStdString(currReservations[i].getName())));
model->setItem(i, 1, new QStandardItem(QString::fromStdString(currReservations[i].getDate())));
model->setItem(i, 2, new QStandardItem(QString::fromStdString(currReservations[i].getFlightNum())));
model->setItem(i, 3, new QStandardItem(QString::fromStdString(currReservations[i].getSeatDesg())));
model->setItem(i, 4, new QStandardItem(QString::fromStdString(currReservations[i].getReservationID())));
}
ui->tableView->setModel(model);
ui->tableView->resizeColumnsToContents();
}
}
showReservations::~showReservations()
{
delete ui;
}
//User Generated Functions
int showReservations::ReadReservations(std::vector<reservation>& Res)
{
std::ifstream flightFile;
std::string fName, lName, date, seatDes, flightNum;
int error = 0;
flightFile.open("reservations.txt");
if(!flightFile)
{
//returns a error value if there is a problem reading the file
error = 1;
return error;
}
else
{
//Start reading files and sticks them into a class object and sticks the object into the vector set
while (flightFile >> fName >> lName >> date >> flightNum >> seatDes)
{
reservation newRes(fName, lName, date, flightNum, seatDes);
Res.push_back(newRes);
}
}
flightFile.close();
return error;
}
ファイルの内容: Jane Doe 04202013 602 1A Nick Deal 05012013 604 2B John Smith 05012013 604 2A
非 Qt コードの作業:
int ReadReservations(std::vector<reservation>& Res);
int main(int argc, char **argv)
{
std::ifstream flightFile;
std::string name, date, seatDes, flightNum, line;
std::vector<reservation> Res;
flightFile.open("reservations.txt");
ReadReservations(Res);
for (int i = 0; i < Res.size(); i++)
{
std::cout << Res[i].getName() << std::endl << Res[i].getDate() << std::endl << Res[i].getFlightNum() << std::endl << Res[i].getSeatDesg() << std::endl;
}
return 0;
}
int ReadReservations(std::vector<reservation>& Res)
{
std::ifstream flightFile;
std::string fName, lName, date, seatDes, flightNum;
int error = 0;
flightFile.open("reservations.txt");
if(!flightFile)
{
//returns a error value if there is a problem reading the file
error = 1;
return error;
}
else
{
//Start reading files and sticks them into a class object and sticks the object into the vector set
while (flightFile >> fName >> lName >> date >> flightNum >> seatDes)
{
//flightFile >> fName >> lName >> date >> flightNum >> seatDes;
reservation newRes(fName, lName, date, flightNum, seatDes);
Res.push_back(newRes);
}
}
flightFile.close();
return error;
}
私は別のファイルで同様の作業をしています。この問題が何であるかを理解することはできません。これは宿題です。
助けてくれてありがとう