我在 Qt 中有一个问题,我正在运行代码以生成类对象的向量并将它们输出到 TableView。每当我运行代码时,它只会读取文件的第一行。我输入了相同的代码并将 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;
}
我在不同的文件上有类似的工作。我只是无法弄清楚这个问题是什么。这是为了家庭作业。
感谢所有的帮助