Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) QtMultipleChoiceQuestionDialog

 

QtMultipleChoiceQuestionDialog is a Qt dialog for MultipleChoiceQuestion.

 

 

 

 

 

qtmultiplechoicequestiondialog.h

 

//---------------------------------------------------------------------------
/*
QtMultipleChoiceQuestionDialog, Qt dialog for MultipleChoiceQuestion
Copyright (C) 2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program.If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/QtMultipleChoiceQuestionDialog.htm
//---------------------------------------------------------------------------
#ifndef QTMULTIPLECHOICEQUESTIONDIALOG_H
#define QTMULTIPLECHOICEQUESTIONDIALOG_H
//---------------------------------------------------------------------------
#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>
//---------------------------------------------------------------------------
#include <QDialog>
//---------------------------------------------------------------------------
#include "questiondialog.h"
#include "qtquestiondialog.h"
//---------------------------------------------------------------------------
namespace Ui {
  class QtMultipleChoiceQuestionDialog;
}
struct Question;
struct MultipleChoiceQuestion;
struct MultipleChoiceQuestionDialog;
//---------------------------------------------------------------------------
class QtMultipleChoiceQuestionDialog : public QtQuestionDialog
{
  Q_OBJECT

public:
  explicit QtMultipleChoiceQuestionDialog(
    QWidget *parent = 0);

  QtMultipleChoiceQuestionDialog(
    const boost::shared_ptr<QuestionDialog>& dialog,
    QWidget *parent = 0);

  ~QtMultipleChoiceQuestionDialog();

  ///Obtain the version of this class
  static const std::string GetVersion();

  ///Obtain the version history of this class
  static const std::vector<std::string> GetVersionHistory();

private slots:
  void on_button_submit_clicked();

private:
  Ui::QtMultipleChoiceQuestionDialog *ui;
  //boost::scoped_ptr<MultipleChoiceQuestionDialog> m_dialog;

  ///Set the Question
  void SetQuestion(
    const boost::shared_ptr<Question>& question
    //const boost::shared_ptr<MultipleChoiceQuestion>& question
    //const MultipleChoiceQuestion * const question
    );

};
//---------------------------------------------------------------------------
#endif // QTMULTIPLECHOICEQUESTIONDIALOG_H

 

 

 

 

 

qtmultiplechoicequestiondialog.cpp

 

//---------------------------------------------------------------------------
#include <boost/bind.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/numeric/conversion/cast.hpp>
//---------------------------------------------------------------------------
#include "multiplechoicequestion.h"
#include "multiplechoicequestiondialog.h"
#include "qtquestiondialog.h"
#include "qtmultiplechoicequestiondialog.h"
#include "questiondialog.h"
#include "ui_qtmultiplechoicequestiondialog.h"
//---------------------------------------------------------------------------
#include <QFile>
//---------------------------------------------------------------------------
QtMultipleChoiceQuestionDialog::QtMultipleChoiceQuestionDialog(
  QWidget *parent) :
  QtQuestionDialog(
    boost::shared_ptr<QuestionDialog>(
      new MultipleChoiceQuestionDialog(
        boost::shared_ptr<MultipleChoiceQuestion>(
          new MultipleChoiceQuestion(
            "*","1+1=","2",{"1","3","4"})))),
    parent),
  ui(new Ui::QtMultipleChoiceQuestionDialog)
{
  ui->setupUi(this);
}
//---------------------------------------------------------------------------
QtMultipleChoiceQuestionDialog::QtMultipleChoiceQuestionDialog(
  const boost::shared_ptr<QuestionDialog>& dialog,
  QWidget *parent) :
  QtQuestionDialog(dialog,parent),
  ui(new Ui::QtMultipleChoiceQuestionDialog)
{
  ui->setupUi(this);
  this->SetQuestion(dialog->GetQuestion());
}
//---------------------------------------------------------------------------
QtMultipleChoiceQuestionDialog::~QtMultipleChoiceQuestionDialog()
{
  delete ui;
}
//---------------------------------------------------------------------------
const std::string QtMultipleChoiceQuestionDialog::GetVersion()
{
  return "1.0";
}
//---------------------------------------------------------------------------
const std::vector<std::string> QtMultipleChoiceQuestionDialog::GetVersionHistory()
{
  std::vector<std::string> v;
  v.push_back("2011-06-28: version 1.0: initial version");
  return v;
}
//---------------------------------------------------------------------------
///Set the Question
void QtMultipleChoiceQuestionDialog::SetQuestion(
  const boost::shared_ptr<Question>& question)
{
  m_dialog->SetQuestion(question);

  if (QFile::exists(question->GetFilename().c_str()))
  {
    ui->image->setPixmap(QPixmap(question->GetFilename().c_str()));
  }

  const MultipleChoiceQuestion * const q
    = dynamic_cast<const MultipleChoiceQuestion *>(question.get());
  assert(q);

  ui->stackedWidget->setCurrentWidget(ui->page_question);
  ui->label_question->setText(question->GetQuestion().c_str());
  ui->label_question_again->setText(q->GetQuestion().c_str());
  ui->label_answer->setText(q->GetAnswer().c_str());


  const std::vector<QRadioButton* > buttons
    = { ui->radio_1, ui->radio_2, ui->radio_3, ui->radio_4, ui->radio_5, ui->radio_6 };
  const std::vector<std::string> options = q->GetOptions();
  const int sz = 6;
  assert(sz == boost::numeric_cast<int>(buttons.size()));
  assert(sz >= boost::numeric_cast<int>(options.size()));
  for (int i = 0; i!=sz; ++i)
  {
    if (i < boost::numeric_cast<int>(options.size()))
    {
      buttons[i]->setText(options[i].c_str());
    }
    else
    {
      buttons[i]->setVisible(false);
    }
  }


}
//---------------------------------------------------------------------------
void QtMultipleChoiceQuestionDialog::on_button_submit_clicked()
{
  assert(m_dialog->CanSubmit());
  const std::vector<const QRadioButton* > buttons
    = { ui->radio_1, ui->radio_2, ui->radio_3, ui->radio_4, ui->radio_5, ui->radio_6 };

  if (std::find_if(
    buttons.begin(), buttons.end(),
    boost::bind(&QRadioButton::isChecked,boost::lambda::_1)
      == true) == buttons.end()) return;

  const std::string s =
    (*std::find_if(
      buttons.begin(), buttons.end(),
      boost::bind(&QRadioButton::isChecked,boost::lambda::_1)
        == true))->text().toStdString();

  this->m_dialog->Submit(s);

  this->ui->stackedWidget->setCurrentWidget(m_dialog->IsAnswerCorrect()
    ? ui->page_correct
    : ui->page_incorrect);

  m_signal_submitted();
}
//---------------------------------------------------------------------------

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml