You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Saturday, May 5th 2012, 7:03pm

develop quiz

Hello everyone,

I trying to make a simple quiz with qt. I'm not that experienced with qt but I know how to program object oriented.

My idea is to created a qwidged with the question and two choices of answers. The questions and choices would be picked from a text file. But the problem is that the questions must be in random order and they can't appear twice in one game. So I think that the whole text file must be imported in an object. But I have no idea how to implement this and wich functions from qt could be handy. Does anybody know some handy classes to import the questions and the associated answers?

Thanks

2

Saturday, May 5th 2012, 9:32pm

Take a look at QFile for opening files
QDomDocument for reading XML files.

For the random question generating, you could do something like this:

In the class defination:

Source code

1
2
3
4
5
6
7
8
struct question{
  QString question;
  QStringList alternatives;
  int rightAlternative;
}
question getNextQuestion();
QList<question> questions;
QList<int> usedQuestions;


In the source file:

Source code

1
2
3
4
5
6
7
8
9
question Class::getNextQuestion(){
  while(1){
    int num = qRand() % questions.size();
    if(usedQuestions.contains(num))
      continue;
    usedQuestions.append(num);
    return questions.at(num);
  }
}


At some point in your code you will have to fill the questions list.

Jan

3

Saturday, May 5th 2012, 10:46pm

that getNextQuestion() is going to hang when all questions have been got.

http://qt-project.org/doc/qt-4.8/qtextstream.html is useful as well
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

This post has been edited 1 times, last edit by "Amleto" (May 5th 2012, 11:04pm)


4

Sunday, May 6th 2012, 7:47am

Yeah, I forgot that, it was just a quick write down.

5

Sunday, May 6th 2012, 1:20pm

Thanks for the suggestion. This is an excellent way to implement it in my program.

This post has been edited 1 times, last edit by "kevindebb" (May 6th 2012, 5:21pm)


6

Sunday, May 6th 2012, 5:27pm

I always get an error when I try to import a string into my Qlist. I don't know what I'm doing wrong.

This is the error: ASSERT failure in QList<T>::operator[]: "index out of range", file ..\..\..\..\QtSDK\Desktop\Qt\4.8.1\mingw\include/QtCore/qlist.h, line 477

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// variable for question
 struct questions{
 QString question;
 QString answerone;
 QString answertwo;
 QString answerthree;
 int rightAnswer;
 };

 QList<questions> questions_pack;
 QList<int> usedQuestions;


	line= file.readLine();
 questions_pack[0].answerone.append(line); <-- Is this the right way to import a string into my struct. 
 qDebug()<< questions_pack[0].answerone;
 line= file.readLine();
 questions_pack[0].answertwo.append(line);
 qDebug()<< questions_pack[0].answertwo;

7

Sunday, May 6th 2012, 8:22pm

questions_pack doesn't have any data yet, so you would need something like this:

Source code

1
2
3
4
questions q;
q.anserone = line;
......
questions_pack.append(q);


I think the function QList::reserve could do the job, but I have no experience with that one.

Jan

8

Sunday, May 6th 2012, 8:32pm

reserve is something different.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

9

Sunday, May 6th 2012, 8:38pm

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct question{
 QString question;
 QString answerone;
 QString answertwo;
 QString answerthree;
 int rightAnswer;
 };

 QList<question> questions;
 QList<int> usedQuestions;

question q;
q.question = // whatever ...
q.answerone = //watever you have to get this from file somehow

questions << q;



honestly I would make a istream operator>>(istream&, question), though.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.