Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
mitk::Forms::Question Class Referenceabstract

Abstract base class for all types of questions used in a Form. More...

#include <mitkQuestion.h>

Inheritance diagram for mitk::Forms::Question:

Public Member Functions

 Question ()
 Default constructor. More...
 
virtual ~Question ()
 Virtual destructor. More...
 
std::string GetQuestionText () const
 Get the literal question text. More...
 
void SetQuestionText (const std::string &question)
 Set the literal question text. More...
 
bool IsRequired () const
 Check whether a response to this question is required to complete a form. More...
 
void SetRequired (bool required=true)
 Set whether a response to this question is required to complete a form. More...
 
virtual std::string GetRequiredText () const
 Get the text that should be displayed to clearly mark a question as required. More...
 
virtual bool HasFileResponses () const
 Query whether the responses given to this question are file paths. More...
 
virtual std::vector< fs::path > SubmitFileResponses (const fs::path &basePath) const
 Attach response files to submission. More...
 
Pure virtual functions

Question is an abstract base class. Derive from this class to add a new type of question and override the following pure virtual functions. Please read the full documentation for all of these functions to fully understand implications and requirements.

Do not forget to register any new type of question by calling IQuestionFactory::Register() like it is done in this module's activator class.

See also
IQuestionFactory
virtual std::string GetType () const =0
 Return the type of a question as string, e.g. "Multiple choice" or "Drop-down". More...
 
virtual Question * CreateAnother () const =0
 Create a new instance of the derived question class type. More...
 
virtual std::vector< std::string > GetResponsesAsStrings () const =0
 Return the question's response(s) as strings. More...
 
virtual void ClearResponses ()=0
 Clear the/all response(s). More...
 
virtual bool IsComplete () const =0
 Check if a question is considered to be answered completely. More...
 
virtual void FromJSON (const nlohmann::ordered_json &j)=0
 Deserialize from JSON. More...
 
virtual void ToJSON (nlohmann::ordered_json &j) const =0
 Serialize to JSON. More...
 

Detailed Description

Abstract base class for all types of questions used in a Form.

Question defines the common interface shared by every question type in the Forms module. Concrete question types (e.g. ShortAnswerQuestion, CheckboxesQuestion) derive from this class and implement its pure virtual functions.

Please make sure to read the full documentation of the pure virtual functions in particular to fully understand implications and requirements.

See also
Form, IQuestionFactory, TextQuestion, QuestionWithOptions, LinearScaleQuestion, ScreenshotQuestion

Definition at line 37 of file mitkQuestion.h.

Constructor & Destructor Documentation

◆ Question()

mitk::Forms::Question::Question ( )

Default constructor.

Initializes the question as not required with an empty question text.

◆ ~Question()

virtual mitk::Forms::Question::~Question ( )
virtual

Virtual destructor.

Member Function Documentation

◆ ClearResponses()

virtual void mitk::Forms::Question::ClearResponses ( )
pure virtual

◆ CreateAnother()

virtual Question* mitk::Forms::Question::CreateAnother ( ) const
pure virtual

Create a new instance of the derived question class type.

This method is mainly used by IQuestionFactory to create new instances from registered prototype instances based on a type string.

Question* RhetoricalQuestion::CreateAnother() const
{
return new RhetoricalQuestion;
}
Question()
Default constructor.
See also
GetType()

Implemented in mitk::Forms::ShortAnswerQuestion, mitk::Forms::ScreenshotQuestion, mitk::Forms::ParagraphQuestion, mitk::Forms::MultipleChoiceQuestion, mitk::Forms::LinearScaleQuestion, mitk::Forms::DropdownQuestion, and mitk::Forms::CheckboxesQuestion.

◆ FromJSON()

virtual void mitk::Forms::Question::FromJSON ( const nlohmann::ordered_json &  j)
pure virtual

Deserialize from JSON.

Polymorphism and the use of base class pointers make it necessary to implement serialization through member functions. Using the pure native approach of the "JSON for Modern C++" library via free functions would lead to partially serialized instances of derived classes.

The actual implementation can and should still be located in a corresponding from_json() free function but we also need the indirection through this member function.

void RhetoricalQuestion::FromJSON(const nlohmann::ordered_json& j)
{
from_json(j, *this);
}
MITKFORMS_EXPORT void from_json(const nlohmann::ordered_json &j, CheckboxesQuestion &q)
Deserialize a CheckboxesQuestion from JSON.
MITKCORE_EXPORT void FromJSON(const nlohmann::json &j, AffineTransform3D::Pointer transform)
Deserialize an AffineTransform3D from a JSON array with 16 elements (row-major 4x4 matrix).
Parameters
[in]jThe JSON object to deserialize from.
See also
from_json(const nlohmann::ordered_json& j, Question& q)

Implemented in mitk::Forms::ShortAnswerQuestion, mitk::Forms::ScreenshotQuestion, mitk::Forms::ParagraphQuestion, mitk::Forms::MultipleChoiceQuestion, mitk::Forms::LinearScaleQuestion, mitk::Forms::DropdownQuestion, and mitk::Forms::CheckboxesQuestion.

◆ GetQuestionText()

std::string mitk::Forms::Question::GetQuestionText ( ) const

Get the literal question text.

Returns
The question text string.

◆ GetRequiredText()

virtual std::string mitk::Forms::Question::GetRequiredText ( ) const
virtual

Get the text that should be displayed to clearly mark a question as required.

This text is typically displayed as soon as a response to a required question is missing. The method can be overridden to customize the default text, e.g. for more complex types of questions, where specific guidance is beneficial.

The default implementation returns "This is a required question".

Returns
A human-readable string describing the requirement.
See also
IsComplete()

◆ GetResponsesAsStrings()

virtual std::vector<std::string> mitk::Forms::Question::GetResponsesAsStrings ( ) const
pure virtual

Return the question's response(s) as strings.

This is the single common generic interface for retrieving responses from all types of questions. It is typically used to store responses in text-based formats like CSV.

It is implemented as vector since certain types of questions like a checkboxes question may support multiple responses. Otherwise, return a vector with a single element.

std::vector<std::string> RhetoricalQuestion::GetResponsesAsStrings() const
{
return { m_Response };
}
Returns
A vector of response strings.

Implemented in mitk::Forms::TextQuestion, mitk::Forms::ScreenshotQuestion, mitk::Forms::QuestionWithOtherOption, mitk::Forms::QuestionWithOptions, and mitk::Forms::LinearScaleQuestion.

◆ GetType()

virtual std::string mitk::Forms::Question::GetType ( ) const
pure virtual

Return the type of a question as string, e.g. "Multiple choice" or "Drop-down".

This method is essential for the deserialization of questions into their correct type, resp. derived class. The type string is used by IQuestionFactory to look up a registered prototype instance of a certain type to create another instance of it.

The type string does not have to match the class name or follow any other convention except for it must be unique amongst all question types. Prefer natural language like in the examples above in case it is used in a user interface to display a question's type.

std::string RhetoricalQuestion::GetType() const
{
return "Rhetorical";
}
See also
CreateAnother()

Implemented in mitk::Forms::ShortAnswerQuestion, mitk::Forms::ScreenshotQuestion, mitk::Forms::ParagraphQuestion, mitk::Forms::MultipleChoiceQuestion, mitk::Forms::LinearScaleQuestion, mitk::Forms::DropdownQuestion, and mitk::Forms::CheckboxesQuestion.

◆ HasFileResponses()

virtual bool mitk::Forms::Question::HasFileResponses ( ) const
virtual

Query whether the responses given to this question are file paths.

Always returns false - override this method only if necessary. Typically used during submission to determine if any files should be attached.

Returns
true if responses represent file paths, false otherwise.
See also
SubmitFileResponses()

Reimplemented in mitk::Forms::ScreenshotQuestion.

◆ IsComplete()

virtual bool mitk::Forms::Question::IsComplete ( ) const
pure virtual

Check if a question is considered to be answered completely.

This method is typically called when IsRequired() returns true to determine whether the requirements are fulfilled.

Returns
true if the question has a complete response, false otherwise.

Implemented in mitk::Forms::TextQuestion, mitk::Forms::ScreenshotQuestion, mitk::Forms::QuestionWithOtherOption, mitk::Forms::QuestionWithOptions, and mitk::Forms::LinearScaleQuestion.

◆ IsRequired()

bool mitk::Forms::Question::IsRequired ( ) const

Check whether a response to this question is required to complete a form.

Returns
true if the question is required, false otherwise.

◆ SetQuestionText()

void mitk::Forms::Question::SetQuestionText ( const std::string &  question)

Set the literal question text.

Parameters
[in]questionThe question text to display.

◆ SetRequired()

void mitk::Forms::Question::SetRequired ( bool  required = true)

Set whether a response to this question is required to complete a form.

A question is not required by default.

Parameters
[in]requiredPass true to mark the question as required, false to mark it as optional.

◆ SubmitFileResponses()

virtual std::vector<fs::path> mitk::Forms::Question::SubmitFileResponses ( const fs::path &  basePath) const
virtual

Attach response files to submission.

This method is supposed to be called during submission only if HasFileResponses() returns true. The given base path should then be used to copy any files. The returned paths should be relative to the base path and are used for submission instead of the return value of GetResponsesAsStrings().

The default implementation returns an empty vector.

Parameters
[in]basePathThe directory to which response files are copied.
Returns
A vector of file paths relative to basePath.
See also
HasFileResponses()

Reimplemented in mitk::Forms::ScreenshotQuestion.

◆ ToJSON()

virtual void mitk::Forms::Question::ToJSON ( nlohmann::ordered_json &  j) const
pure virtual

Serialize to JSON.

Polymorphism and the use of base class pointers make it necessary to implement serialization through member functions. Using the pure native approach of the "JSON for Modern C++" library via free functions would lead to partially serialized instances of derived classes.

The actual implementation can and should still be located in a corresponding to_json() free function but we also need the indirection through this member function.

void RhetoricalQuestion::ToJSON(nlohmann::ordered_json& j) const
{
to_json(j, *this);
}
MITKFORMS_EXPORT void to_json(nlohmann::ordered_json &j, const CheckboxesQuestion &q)
Serialize a CheckboxesQuestion to JSON.
MITKCORE_EXPORT void ToJSON(nlohmann::json &j, AffineTransform3D::ConstPointer transform)
Serialize an AffineTransform3D to a JSON array with 16 elements (row-major 4x4 matrix).
Parameters
[out]jThe JSON object to serialize into.
See also
to_json(nlohmann::ordered_json& j, const Question& q)

Implemented in mitk::Forms::ShortAnswerQuestion, mitk::Forms::ScreenshotQuestion, mitk::Forms::ParagraphQuestion, mitk::Forms::MultipleChoiceQuestion, mitk::Forms::LinearScaleQuestion, mitk::Forms::DropdownQuestion, and mitk::Forms::CheckboxesQuestion.


The documentation for this class was generated from the following file: