void EngineerExam::parseQuestions(QString filename) { QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(filename); if (!db.open()) { qCritical() << trUtf8("Nie można otworzyć bazy danych!"); } QSqlTableModel question; question.setTable("Question"); question.select(); QSqlTableModel answers; answers.setTable("Answer"); answers.select(); QStringList qsl; int i = 0; while (question.canFetchMore()) { question.fetchMore(); for (; i < question.rowCount(); ++i) { qsl.clear(); quint32 id = question.record(i).value("Id").toUInt(); QString text = question.record(i).value("Text").toString(); Question q; q.setId(id); q.setQuestion(text); answers.setFilter(QString("`id_Question` = %1").arg(id)); QString a1 = answers.record(0).value("Text").toString(); QString a2 = answers.record(1).value("Text").toString(); QString a3 = answers.record(2).value("Text").toString(); qsl << a1 << a2 << a3; bool correct1 = answers.record(0).value("IsTrue").toBool(); bool correct2 = answers.record(1).value("IsTrue").toBool(); bool correct3 = answers.record(2).value("IsTrue").toBool(); if (correct1) { q.setCorrectAnswer(0); } else if (correct2) { q.setCorrectAnswer(1); } else if (correct3) { q.setCorrectAnswer(2); } q.setAnswers(qsl); tmpList.append(q); } } }
Question Theme::selectQuestion( const QSqlQuery &query ) const { // reject into new method. Question makeQuestion(const QSqlQueru &query) using namespace Tables::Question::Field; Question issue; QString id = query.value( query.record().indexOf( QUESTION_ID ) ).toString(), text = query.value( query.record().indexOf( TEXT ) ).toString(), type = query.value( query.record().indexOf( QUESTION_TYPE ) ).toString(); bool caseSens = query.value( query.record().indexOf( CASE_SENS ) ).toBool(), stripSpace = query.value( query.record().indexOf( STRIP_SPACE ) ).toBool(); QTime time = query.value( query.record().indexOf( RECOMENDED_TIME ) ).toTime(); issue.setId( id ); issue.setText( text ); issue.setType( type ); issue.setTime( time ); issue.setCaseSensitivity( caseSens ); issue.setStripSpaces( stripSpace ); return issue; }