I'm trying to use the QVector
class from Qt
to work (for me :P). What I want to do is to put multiple instances of the object Question
in a QVector
.
I went on multiple forums, but they're all too complicated for me as I am a beginner. This one post was perfect but I did not find a way to resolve my problem.
So I'm turning to you to help me!
Here's the function that I want to work :
The part that create the bundle/ the vector
/**
* @brief MenuQuestionnary::assembleQuiz
* Assemble the bundle of question that will be used in Quiz class
*/
void MenuQuestionnary::assembleQuiz(){
QVector<Question> vectorQuiz;
vectorQuiz.reserve(spinBoxNumberOfQuestion->value());
for(int i = 0; i <= spinBoxNumberOfQuestion->value(); i++){
vectorQuiz.append(Question((qrand()% maximumNumberOfQuestionAvailable)));
}
}
Here's my Question constructor :
Question::Question(int id)
{
this->questionId = id;
//TODO: Actually get it from DB
this->questionText = "2+2?";
this->explanation = "Addition mechanics";
this->creatorId = 1;
}
What i expect to do here is to put the selected number of the Question
object in a vector
. After that i can pass it to another class. From there i should be able to extract the text from them(questionText
and questionExplanation
).
I solved it by trying few things Here's the explanation of what i did
I've split my function in two. The first one put the element in a QList
and the second one shuffles it.
/**
* @brief MenuQuestionnary::assembleQuiz
* @param list
* Asseble a quiz in the Qlist and then ask shuffle to shuffle it...
*/
void MenuQuestionnary::assembleQuiz(QList<Question> &list){
for(int i = 0; i < spinBoxNumberOfQuestion->value(); ++i){
int rand = qrand() * maximumNumberOfQuestionAvailable;
Question newQuestion(rand);
list.append(newQuestion);
}
shuffleQuiz(list);
}
/**
* Method Shuffle
* equivalent to shuffling a deck of cards: we take a random one, move it to be the last one,
* then do it again enough times to have statistically touched every card.
*/
void MenuQuestionnary::shuffleQuiz(QList<Question> &list){
int iters = list.size() * list.size();
for (int i = 0; i < iters; ++i){
int rand = qrand() * list.size();
list.append(list[rand]);
list.removeAt(rand);
}
}
Thanks for the help though.