You are on page 1of 22

Лекция 6

Визуальный интерфейс в Qt
Был 1958 год. Мои дядя с тетей – которые даже тогда казались мне старыми – взяли меня
на экскурсию в Хэворт-хаус, старый дом, где некогда проживало семейство Бронте. В школе
я как раз изучала Уильяма Теккерея, и, поскольку сестры Бронте были его современницами,
такая поездка позволила бы мне узнать много нового. Дядю Майкрофта пригласили
прочитать в Брэдфордском университете лекцию по его замечательной математической
работе, касавшейся теории игр (применяя ее на практике, дядя никогда не проигрывал в
китайские шахматы). Брэдфорд расположен неподалеку от Хэворта, так что совместить обе
поездки показалось нам хорошей идеей.
Дж. ФФОРДЕ, Дело Джен, или Эйра немилосердия, Гл. 6 «Джен Эйр» – краткий экскурс в
роман
Signals and Slots in Qt
Syntax
#include <QObject>
class Counter : public QObject
{ Q_OBJECT
public:
Counter() { m_value = 0; }
int value() const { return m_value; }

public slots: void setValue(int value);


signals: void valueChanged(int newValue);
private: int m_value;
};
Signals and Slots in Qt
void Counter::setValue(int value)
{
if (value != m_value) {
m_value = value;
emit valueChanged(value);
}
}
Signals and Slots in Qt
Counter a, b;
QObject::connect(&a, &Counter::valueChanged,
&b, &Counter::setValue);

a.setValue(12); // a.value() == 12, b.value() == 12


b.setValue(48); // a.value() == 12, b.value() == 48
Signals and Slots in Qt
Counter a, b, c, d;
QObject::connect(&a, &Counter::valueChanged,
&b, &Counter::setValue);
QObject::connect(&a, &Counter::valueChanged,
&c, &Counter::setValue);
QObject::connect(&b, &Counter::valueChanged,
&d, &Counter::setValue);

a.setValue(12); // a==12, b==12, c==12, d==12


b.setValue(48); // a==12, b==48, c==12, d==48
Signals and Slots in Qt
“+”
The only signal can “awake” more than one slots.
Using just one initiator we can make informed
several subscribers.

“-”
Debugging is pain.
Signals and Slots in Qt
class QButton
{…
public slots: void clicked();
}

void MainWindow::on_pushButton_13_clicked()

QObject::connect(button_13, clicked, myWindow,


on_pushButton_13_clicked)
And now
Some practice.
Дэн, ай нид хэлп
Model-View-Controller
Model – stores data.
View – shows data.
Controllers – allow
view have access to
data.

This divides entities


and makes them
independent.
Let us develop a game
(or why MVC-paradigm is a useful concept)

Railroad on a square grid. Steam engines are


delivering good among stations, good are produced
and consumed at mines, factories, and houses.
Young Logistic
Storing the map as a two-dimentional array of
structures. The structure stores: if there is a building,
type of building, what does the building produces
and consumes, environmental resources, if there is a
railroad, direction of the railroad, relief, if there is a
train.
The map stores objects, creates and destructs
objects, produces and consumes resources.
The maps draws objects and reacts user actions.
Young Logistic
Storing the map as a two-dimentional array of
structures. The structure stores: if there is a building,
type of building, what does the building produces
and consumes, environmental resources, if there is a
railroad, direction of the railroad, relief, if there is a
train.
The map stores objects, creates and destructs
objects, produces and consumes resources.
The maps draws objects and reacts user actions.
Young Logistic 2.0
The map class stores relief, roads, list of buildings
and their positions. Railroad terminal stores
resources stocked on the terminal, links to facility
buildings, manages resources among consuming
buildings.
Any building contains information on consumed and
produced resources.
Any train contains information on the rout, its
position, delivered resources.
Interface shows maps, buildings, and trains using
theirs information, inputs information using control
elements.
Young Logistic 2.0
(multiplayer mode)

Adding property belonging an entity to a user.


Young Logistic 2.2
(network mode)

Instead of visual controllers we can use objects


interacting a server. Instead of visualizing, the object
serializes information to a network socket. One of the
instance of the game operates as a server.
Young Logistic 2.4
(artificial intelligence)

Implementation of an AI-object, receiving and


sending information from/to an information channel
as a regular user.
Здесь был показ возможностей
визуального интерфейса Qt
И теперь вы готовы к тому, чтобы
начать выполнять лабораторную
работу номер три
Дедлайн пойдет от обновления
методички
Завершающая (не) шутка
You need the willingness to fail all the time.
You have to generate many ideas and then
you have to work very hard only to discover
that they don’t work. And you keep doing
that over and over until you find one that
does work.

John Backus,
inventor of the Fortran programming language