Consider the following definition and implementation
of FortuneCookie to answer the questions that follow:
//definition
class FortuneCookie: public QDialog {
Q_OBJECT
private:
QStringList messages;
public:
FortuneCookie();
public slots:
void showFortune();
};
//implementation
FortuneCookie::FortuneCookie(){
messages << "Express yourself : Don't
hold back" << "For
success today look first at yourself"
<< "Whenever
possible, keep it simple";
QPushButton *pb = new QPushButton("Open a
fortune
cookie");
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(pb);
this->setLayout(layout);
connect(pb, SIGNAL (clicked()), this,
SLOT(showFortune()));
}
void FortuneCookie::showFortune(){
QString m = messages[rand()% 3];
QMessageBox::information(this, "Message",
m);
}
1.1 What does the code above
do? (2)
1.2 Explain the purpose of signals and
slots using the code above. (2)
1.3 Why is messages declared as
a data member as opposed to the button and the layout that are
declared in the constructor? (2)
1.4 Why is the Q_OBJECT macro
required in this class definition? (2)
1.5 In the constructor, two objects are
allocated on the heap but they are not de-allocated. Is that a
problem in this class? Explain. (2)
1.6 Illustrate the parent-child object
tree after an object of FortuneCookie is created. (2)
1.7 Assume that the above class needs to
be changed so that the messages are displayed at regular intervals.
In particular, the user should be able to:
- specify the time interval on the graphical user interface
- click on a start button for the program to read the
time entered as well as to start displaying the messages at regular
intervals
Write down the modified code of FortuneCookie (both
definition and implementation) to achieve the above
requirements. No pre-processor directives and no additional
features are required in your answer. (8)
1.8 Study the MyMainWindow class
definition below to answer the question that follows:
class MyMainWindow: public QMainWindow{
Q_OBJECT
public:
MyMainWindow();
public slots:
void showFortuneCookie();
};
Implement the constructor of MyMainWindow so that a
menu named applications and a menu item
named Fortune Cookie will be displayed on the main window
and so that clicking on the menu item Fortune
Cookie executes the slot showFortuneCookie() defined
in this class. (5)
Consider the following definition and implementation of FortuneCookie to answer the questions that follow: //definition
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am