Question

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).

Was it helpful?

Solution 4

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.

OTHER TIPS

You are trying to push objects of class type Question into a QVector<int>, which obviously is expecting an int instead. You should change it to QVector<Question> to begin with.

What I highly suggest, though, is that you read a good book on C++ before going any further, or your experience with it will just get more and more complicated.

Copy pasting code from forums on the internet is not programming and will get you in troubles soon.

I think what you want is this: QVector vectorQuiz; vectorQuiz.reserve(spinBoxNumberOfQuestion->value()); // reserve correct amount of space in vector for performance (not required). I don't know exactly how you are going to use vectorQuiz, but maybe you should use pointers to the questions i.e. QVector and insert questions using new Question()

/**
 * @brief MenuQuestionnary::assembleQuiz
 * Assemble the bundle of question that will be used in Quiz class
 */
void MenuQuestionnary::assembleQuiz(){
    int iVectorSize = spinBoxNumberOfQuestion->value();
    QVector<Question> vectorQuiz;
    vectorQuiz.reserve(iVectorSize ); 

    for(int i = 0; i <= iVectorSize ; ++i){
        vectorQuiz.append(Question(i));
    }

}

Your object (vectorQuiz) declared as vector of integers. If you want to add some integer value to it you should write something:

vectorQuiz.append( someIntegerValue );

or

vectorQuiz.push_back( someIntegerValue );

For vector of another type (i.e. Question), write code like this:

QVector<Question> vectorQuiz;
// ...
for(int i = 0; i <= spinBoxNumberOfQuestion->value(); ++i){
    vectorQuiz.append(Question(i)); // without [i] after vector object
}

And possible your should use strict inequality < instead of <= in for-cycle (but I'm not sure).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top