KNSCore::Question Class

class KNSCore::Question

A way to ask a user a question from inside a GUI-less library (like KNewStuffCore). More...

Header: #include <Question>
CMake: find_package(KF6 REQUIRED COMPONENTS NewStuff)
target_link_libraries(mytarget PRIVATE KF6::NewStuff)
Inherits: QObject

Public Types

enum QuestionType { YesNoQuestion, ContinueCancelQuestion, InputTextQuestion, SelectFromListQuestion, PasswordQuestion }
enum Response { InvalidResponse, YesResponse, NoResponse, ContinueResponse, CancelResponse, OKResponse }

Public Functions

KNSCore::Question::Response ask()
KNSCore::Entry entry() const
QStringList list() const
QString question() const
KNSCore::Question::QuestionType questionType() const
QString response() const
void setEntry(const KNSCore::Entry &entry)
void setList(const QStringList &newList)
void setQuestion(const QString &newQuestion)
void setQuestionType(KNSCore::Question::QuestionType newType = YesNoQuestion)
void setResponse(KNSCore::Question::Response response)
void setResponse(const QString &response)
void setTitle(const QString &newTitle)
QString title() const

Detailed Description

Rather than using a message box (which is a UI thing), when you want to ask your user a question, create an instance of this class and use that instead. The consuming library (in most cases KNewStuff or KNewStuffQuick) will listen to any question being asked, and act appropriately (that is, KNewStuff will show a dialog with an appropriate dialog box, and KNewStuffQuick will either request a question be asked if the developer is using the plugin directly, or ask the question using an appropriate method for Qt Quick based applications)

The following is an example of a question asking the user to select an item from a list.

QStringList choices() << "foo" << "bar";
Question question(Question::SelectFromListQuestion);
question.setTitle("Pick your option");
question.setQuestion("Please select which option you would like");
question.setList(choices);
question.setEntry(entry);
if (question.ask() == Question::OKResponse) {
    QString theChoice = question.response();
}

Member Type Documentation

enum Question::QuestionType

Defines the type of question to be presented to the user.

ConstantValueDescription
KNSCore::Question::YesNoQuestion0The question can be answered with either yes or no.
KNSCore::Question::ContinueCancelQuestion1The question can be answered with either continue or cancel.
KNSCore::Question::InputTextQuestion2Answering the question requires text input from the user.
KNSCore::Question::SelectFromListQuestion3Answering the question requires selecting from a list of choices.
KNSCore::Question::PasswordQuestion4Answering the question requires the user to input a password.

See also Response.

enum Question::Response

Defines how the user responded to the question.

ConstantValueDescription
KNSCore::Question::InvalidResponse0The user did not provide a valid response.
KNSCore::Question::YesResponse1The user selected yes.
KNSCore::Question::NoResponse2The user selected no.
KNSCore::Question::ContinueResponse3The user selected continue.
KNSCore::Question::CancelResponse4The user selected cancel.
KNSCore::Question::OKResponseYesResponseThe user selected OK.

See also QuestionType.

Member Function Documentation

KNSCore::Question::Response Question::ask()

Returns the user's reponse to the question.

See also setResponse.

KNSCore::Entry Question::entry() const

Returns the configured entry.

See also setEntry().

QStringList Question::list() const

Returns the list of choices to present to the user, if any.

See also setList().

QString Question::question() const

Returns the question message to be presented to the user.

See also setQuestion().

KNSCore::Question::QuestionType Question::questionType() const

Returns the configured question type.

See also setQuestionType().

QString Question::response() const

Returns the response data provided by the user, if any.

See also setResponse().

void Question::setEntry(const KNSCore::Entry &entry)

Sets the data entry container this question refers to.

See also entry().

void Question::setList(const QStringList &newList)

Sets the list of optional choices to present to the user to newList for a SelectFromListQuestion.

See also list().

void Question::setQuestion(const QString &newQuestion)

Sets the question to be presented to the user.

newQuestion is the question message

See also question().

void Question::setQuestionType(KNSCore::Question::QuestionType newType = YesNoQuestion)

Sets the type of question presented to the user.

newType is the question type

See also questionType().

void Question::setResponse(KNSCore::Question::Response response)

When the user makes a choice on a question, that is a response. This is the return value in ask().

response This will set the response, and mark the question as answered

See also response().

void Question::setResponse(const QString &response)

If the user has any way of inputting data to go along with the response above, consider this a part of the response. As such, you can set, and later get, that response as well. This does NOT mark the question as answered.

response This sets the string response for the question

See also setResponse.

void Question::setTitle(const QString &newTitle)

Sets the title of the UX element presented to the user.

newTitle is the title text

See also title().

QString Question::title() const

Returns the title of the UX element.

See also setTitle().