因此,当我尝试更改_studentsQuantity时,程序会崩溃。但是如果我在构造函数中赋值变量都可以。 首先,我尝试从QTextStream中分配_studentsQuantity,但它不起作用。 这是从文件加载数据的功能:
bool journalManager::loadFile(const QString& fileName)
{
QFile file(fileName);
if(file.open(QIODevice::ReadOnly) )
{
QTextStream fin(&file);
_studentsQuantity = 6; //fin.readLine().toInt();
file.close();
return true;
}
return false;
}
这个类实现:
class journalManager
{
public:
journalManager();
bool loadFile(const QString& fileName);
void writeToFile(QString fileName) const;
void updateTableMarks(int subjectNumber);
void updateTableStudents();
void updateComboBoxSubjects();
void setTableMarks(QTableWidget *tableMarks) { _tableMarks = tableMarks; }
void setTableStudents(QTableWidget *tableStudents) { _tableStudents = tableStudents; }
void setComboBoxSubjects(QComboBox *comboBoxSubjects) { _comboBoxSubjects = comboBoxSubjects; }
private:
typedef struct studentStruct
{
QString _surname;
QString _name;
QString data() { return _surname + _name; }
} student;
QVector<student> _students;
QVector<QString> _subjects;
QVector<QVector<QVector<int> > > _marks;
int _marksQuantity;
int _subjectsQuantity;
int _studentsQuantity;
QTableWidget *_tableMarks;
QTableWidget *_tableStudents;
QComboBox *_comboBoxSubjects;
};
此外,我尝试调试并收到有分段错误的消息
这就是我使用journalManager
类型的对象的方法void MainWindow::open()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open file"), "../ElelectronicJournal", tr("Text files (*.txt)") );
if(!fileName.isNull() )
{
if(manager->loadFile(fileName) )
statusBar()->showMessage(tr("File opened"));
}
}