2013-04-10 44 views
0

我在Qt中遇到問題,我正在運行代碼來生成類對象的向量並將它們輸出到TableView。每當我運行代碼時,它只會讀取文件的第一行。我把相同的代碼,並將TableView部分更改爲標準的cout,一切正常。 李四04202013 602 1A 尼克新政05012013 604 2B 約翰·史密斯05012013 604 2A使用Qt和向量讀取文件行問題

工作的非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; 

} 

我有事

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; 

} 

文件內容類似的工作在不同的文件上。我無法弄清楚這個問題是什麼。這是作業。

感謝所有幫助

+0

試''模型 - > appendRow()'':http://qt-project.org/doc/ qt-4.8/qstandarditemmodel.html#appendRow – gongzhitaao 2013-04-10 22:02:47

+0

問題是,我有幾乎完全相同的工作正常工作的不同形式。我看着appendRow(),如果我無法弄清楚我當前的代碼是怎麼回事,我可以沿着這條路線走下去或嘗試。 – NickDeal 2013-04-10 22:21:42

回答

1

您可以使用appendRow這樣的:

for (int i = 0; i < currReservations.size(); i++) 
{ 
    QList<QStandardItem*> row; 
    row << new QStandardItem(QString::fromStdString(currReservations[i].getName()))); 
    row << new QStandardItem(QString::fromStdString(currReservations[i].getDate()))); 
    row << new QStandardItem(QString::fromStdString(currReservations[i].getFlightNum()))); 
    row << new QStandardItem(QString::fromStdString(currReservations[i].getSeatDesg()))); 
    row << new QStandardItem(QString::fromStdString(currReservations[i].getReservationID()))); 

    model->appendRow(row); 
}