Escolar Documentos
Profissional Documentos
Cultura Documentos
http://liveblue.wordpress.com
Objetivos
Pr-requisitos
Necessrios:
Desejveis:
Especiais:
Metodologia
Durao: 60 horas
Referncias:
Livros
Qt Reference
Documentation
Fruns
(QtCentre etc)
Qt Quarterly
Desenvolvimento Cross-Platform com C++ e Qt - Sandro Santos Andrade - Live Blue
Metodologia
Durao: 60 horas
Referncias:
Livros
Qt Reference
Documentation
Fruns
(QtCentre etc)
Qt Quarterly
Desenvolvimento Cross-Platform com C++ e Qt - Sandro Santos Andrade - Live Blue
Sobre o instrutor
Sandro Andrade
Viso Geral
X11
Motif
Tcl / Tk
Fltk
WxWidgets
GTK / Qt
Viso Geral
Mdulos e ferramentas
Viso Geral
Widgets
10
Viso Geral
11
Viso Geral
12
Viso Geral
13
Viso Geral
Grficos 2D
14
Viso Geral
Grficos 2D
15
Viso Geral
Grficos 2D
16
Viso Geral
Grficos 2D
17
Viso Geral
OpenGL
18
Viso Geral
OpenGL
19
Viso Geral
OpenGL
20
Viso Geral
Scripting
21
Viso Geral
Interfaces animadas
22
Viso Geral
Model View
23
Viso Geral
Banco de Dados
24
Viso Geral
Networking
25
Viso Geral
XML
26
Viso Geral
27
Viso Geral
28
Viso Geral
29
Viso Geral
Qt Mobile
30
Viso Geral
Qt Mobile
31
Viso Geral
Qt Mobile
32
Histria do Qt
33
Histria do Qt
34
Histria do Qt
35
Histria do Qt
36
Histria do Qt
37
Porque o Qt ?
Quem usa o Qt ?
38
Instalando o Qt 4.6
Linux:
Bibliotecas e headers
Ferramentas (Designer, Linguist e Assistant)
Demos
Documentao
Qt Creator (IDE)
Desenvolvimento Cross-Platform com C++ e Qt - Sandro Santos Andrade - Live Blue
39
Instalando o Qt 4.6
Windows:
40
Lab1: Hello Qt
1
2
3
4
5
6
7
8
9
10
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel *label = new QLabel("Hello Qt!");
label->show();
return app.exec();
}
Executar:
$ qmake -project
$ qmake
$ make
Desenvolvimento Cross-Platform com C++ e Qt - Sandro Santos Andrade - Live Blue
41
Lab1: Hello Qt
O qmake:
42
Lab1: Hello Qt
O qmake:
43
Lab1: Hello Qt
O qmake:
1
2
3
4
5
6
7
8
9
10
11
###################################################################
# Automatically generated by qmake (2.01a) qua jul 21 22:43:25 2010
###################################################################
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
# Input
SOURCES += main.cpp
44
Lab1: Hello Qt
45
Lab1: Hello Qt
46
Lab1: Hello Qt
Algumas consideraes:
47
Lab1: Hello Qt
Algumas consideraes:
48
Modelo de Objetos do Qt
49
Signals / Slots
50
Signals / Slots
1
2
3
4
5
bool QObject::connect(senderqobjptr,
SIGNAL(signalname(argtypelist)),
receiverqobjptr,
SLOT(slotname(argtypelist))
optionalConnectionType);
51
Signals / Slots
52
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Um exemplo simples:
#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;
};
53
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Um exemplo simples:
void Counter::setValue(int value)
{
if (value != m_value) {
m_value = value;
emit valueChanged(value);
}
}
Counter a, b;
QObject::connect(&a, SIGNAL(valueChanged(int)),
&b, SLOT(setValue(int)));
a.setValue(12);
b.setValue(48);
54
Consideraes importantes:
55
Consideraes importantes:
56
Consideraes importantes:
57
Signals / Slots
58
Signals / Slots
Cdigo com
extenses do Qt
Ex: Q_OBJECT
public slots:
MOC
(Meta-Object
Compiler)
Cdigo
Standard C++
59
Layout e Parentesco
60
Layout e Parentesco
61
Layout e Parentesco
QSlider
(filho do
QWidget)
62
Layout e Parentesco
QSpinBox
(filho do
QWidget)
QSlider
(filho do
QWidget)
63
Layout e Parentesco
Tipos de layout:
QHBoxLayout
QVBoxLayout
QGridLayout
QformLayout
64
Layout e Parentesco
65
#include
#include
#include
#include
<QApplication>
<QHBoxLayout>
<QSlider>
<QSpinBox>
15
QObject::connect(spinBox,
SIGNAL(valueChanged(int)),
slider,
SLOT(setValue(int)));
QObject::connect(slider,
SIGNAL(valueChanged(int)),
spinBox,
SLOT(setValue(int)));
spinBox->setValue(35);
QHBoxLayout *layout =
new QHBoxLayout;
layout->addWidget(spinBox);
layout->addWidget(slider);
window->setLayout(layout);
window->show();
return app.exec();
19
5
6 int main(int argc, char *argv[])
7 {
8
QApplication app(argc, argv);
9
QWidget *window = new QWidget;
10
window->setWindowTitle
("Enter Your Age");
11
QSpinBox *spinBox =
new QSpinBox;
12
QSlider *slider =
new QSlider(Qt::Horizontal);
13
spinBox->setRange(0, 130);
14
slider->setRange(0, 130);
20
21
22
23
24
25
26
27
66
67
68
Qt e IDE's
KDevelop (http://www.kdevelop.org)
XCode (http://developer.apple.com/tools/xcode/)
Eclipse (http://www.eclipse.org/)
Edyuk (http://www.edyuk.org)
QDevelop (http://www.qdevelop.org/)
Desenvolvimento Cross-Platform com C++ e Qt - Sandro Santos Andrade - Live Blue
69
Qt Reference Documentation
http://doc.qt.nokia.com/4.6/
70
71
72
73
74
Lab4: Dialogs
1
2
3
4
5
6
7
8
9
10
11
12
#include <QDialog>
15
class
class
class
class
QCheckBox;
QLabel;
QLineEdit;
QPushButton;
16
17
18
19
20
21
22
23
24
25
26
void findPrevious(
const QString &str,
Qt::CaseSensitivity cs);
private slots:
void findClicked();
void enableFindButton(
const QString &text);
private:
QLabel *label;
QLineEdit *lineEdit;
QCheckBox *caseCheckBox;
QCheckBox *backwardCheckBox;
QPushButton *findButton;
QPushButton *closeButton;
};
75
Lab4: Dialogs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <QtGui>
#include "finddialog.h"
FindDialog::FindDialog(
QWidget *parent) :
QDialog(parent)
{
label = new QLabel(
tr("Find &what:"));
lineEdit = new QLineEdit;
label->setBuddy(lineEdit);
15
16
17
18
19
20
caseCheckBox =
new QCheckBox(tr("Match &case"));
21
22
backwardCheckBox =
new QCheckBox(
tr("Search &backward"));
23
24
findButton = new
QPushButton(tr("&Find"));
25
findButton->setDefault(true);
findButton->setEnabled(false);
closeButton = new
QPushButton(tr("Close"));
connect(lineEdit,
SIGNAL(textChanged(
const QString &)),
this,
SLOT(enableFindButton(
const QString &)));
connect(findButton,
SIGNAL(clicked()),
this, SLOT(findClicked()));
connect(closeButton,
SIGNAL(clicked()),
this, SLOT(close()));
76
Lab4: Dialogs
26
27
28
29
30
31
32
33
34
35
36
37
38
QHBoxLayout *topLeftLayout =
new QHBoxLayout;
topLeftLayout->addWidget(label);
topLeftLayout->addWidget(
lineEdit);
QVBoxLayout *leftLayout =
new QVBoxLayout;
leftLayout>addLayout(
topLeftLayout);
leftLayout->addWidget(
caseCheckBox);
leftLayout->addWidget(
backwardCheckBox);
QVBoxLayout *rightLayout =
new QVBoxLayout;
rightLayout->addWidget(
findButton);
rightLayout->addWidget(
closeButton);
rightLayout->addStretch();
39
QHBoxLayout *mainLayout =
new QHBoxLayout;
mainLayout->addLayout(
leftLayout);
mainLayout->addLayout(
rightLayout);
setLayout(mainLayout);
40
41
42
43
44
45
46
47
setWindowTitle(tr("Find"));
setFixedHeight(
sizeHint().height());
}
77
78
79
80
Criando um action:
1
2
3
4
5
81
1
2
3
4
5
82
1
2
3
4
5
6
7
8
83
1
2
3
4
5
6
7
8
9
84
85
86
Resources:
87
Lab6: Resources
88
Entendendo o Qt Designer
Descrio XML
do dialog ou
mainwindow
(arquivo .ui)
UIC
(User Interface
Compiler)
Cdigo
Standard C++
(classe no namespace Ui)
89
90
Lab7: Funcionalidades
1
2
3
4
5
6
7
8
9
10
11
Implementando funcionalidade
#include <QDialog>
#include "ui_cdmainwindow.h"
class CdMainWindow : public QMainWindow, public Ui::CdMainWindow
{
Q_OBJECT
public:
CdMainWindow(QWidget *parent = 0);
private slots:
void on_lineEdit_textChanged();
};
91
Lab7: Funcionalidades
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Implementando funcionalidade
#include <QtGui>
#include "cdmainwindow.h"
CdMainWindow::CdMainWindow(QWidget *parent) : QmainWindow(parent)
{
setupUi(this);
QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");
lineEdit->setValidator(new QRegExpValidator(regExp, this));
}
void CdMainWindow::on_lineEdit_textChanged()
{
okButton->setEnabled(lineEdit->hasAcceptableInput());
}
92
Lab7: Funcionalidades
Implementando funcionalidade
1
2
3
4
5
6
7
8
9
#include <QApplication>
#include "cdmainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
CdMainWindow *mw = new CdMainWindow;
mw->show();
return app.exec();
}
93
Lab7: Funcionalidades
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
94
Lab7: Funcionalidades
1
2
3
4
5
6
7
8
9
void MainWindow::addCd()
{
AddCdDialog addCdDialog(this);
if (addCdDialog.exec())
{
// Retorna QDialog::Accepted ou QDialog::Reject
...
}
}
95
96
97
Size policies
98
99
Sistema de painting
100
101
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(width() / 2, height() / 2);
painter.scale(side / 200.0, side / 200.0);
painter.setPen(Qt::NoPen);
painter.setBrush(hourColor);
painter.save();
painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
painter.drawConvexPolygon(hourHand, 3);
painter.restore();
painter.setPen(hourColor);
for (int i = 0; i < 12; ++i) {
painter.drawLine(88, 0, 96, 0);
painter.rotate(30.0);
}
Desenvolvimento Cross-Platform com C++ e Qt - Sandro Santos Andrade - Live Blue
102
painter.setPen(Qt::NoPen);
painter.setBrush(minuteColor);
painter.save();
painter.rotate(6.0 * (time.minute() + time.second() / 60.0));
painter.drawConvexPolygon(minuteHand, 3);
painter.restore();
painter.setPen(minuteColor);
for (int j = 0; j < 60; ++j) {
if ((j % 5) != 0)
painter.drawLine(92, 0, 96, 0);
painter.rotate(6.0);
}
103
Model View
104
Model View
105
Model View
106
Model View
107
Model View
108
Model View
109
1
2
3
4
5
6
7
8
9
10
110
Model View
Roles
Desenvolvimento Cross-Platform com C++ e Qt - Sandro Santos Andrade - Live Blue
111
Lab11: Delegates
1
2
3
4
5
6
112
Lab11: Delegates
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
113
Lab11: Delegates
2
3
4
5
6
7
8
9
10 void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
11
const QStyleOptionViewItem &option,
const QModelIndex &/* index */) const
12 {
13
editor->setGeometry(option.rect);
14 }
114
Generic Containers
Foreach
Desenvolvimento Cross-Platform com C++ e Qt - Sandro Santos Andrade - Live Blue
115
Generic Containers
So implicitamente compartilhados
116
Generic Containers
Viso Geral:
QList:
QLinkedList:
QVector:
QStack:
117
Generic Containers
Viso Geral:
QQueue:
QSet:
Q[Multi]Map:
Q[Multi]Hash:
118
Generic Containers
Iterators
119
Generic Containers
1
2
3
4
5
6
7
8
9
10
11
12
Iterators Java-style
QList<QString> list;
list << "A" << "B" << "C" << "D";
QListIterator<QString> i(list);
while (i.hasNext())
qDebug() << i.next();
QListIterator<QString> i(list);
i.toBack();
while (i.hasPrevious())
qDebug() << i.previous();
120
Generic Containers
1
2
3
4
5
6
7
8
9
10
11
12
QMutableListIterator<int> i(list);
while (i.hasNext()) {
if (i.next() > 128)
i.setValue(128);
}
121
Generic Containers
1
2
3
4
5
6
Iterators STL-style
QList<QString> list;
list << "A" << "B" << "C" << "D";
QList<QString>::iterator i;
for (i = list.begin(); i != list.end(); ++i)
*i = (*i).toLower();
122
Generic Containers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Foreach
QLinkedList<QString> list;
foreach (QString str, list) {
if (str.isEmpty())
break;
qDebug() << str;
}
QMap<QString, int> map;
foreach (QString str, map.keys())
qDebug() << str << ":" << map.value(str);
QMultiMap<QString, int> map;
foreach (QString str, map.uniqueKeys()) {
foreach (int i, map.values(str))
qDebug() << str << ":" << i;
}
Desenvolvimento Cross-Platform com C++ e Qt - Sandro Santos Andrade - Live Blue
123
Generic Containers
Complexidades:
124
125
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// firstNumber == 80
// secondNumber == 0
char ch;
in >> ch;
// ch == 'x'
126
Manipuladores
127
1
2
3
4
5
6
7
8
9
10
11
12
13
QFile file("file.dat");
file.open(QIODevice::ReadOnly);
QDataStream in(&file);
// read the data serialized from the file
QString str;
qint32 a;
in >> str >> a;
// extract "the answer is" and 42
128
1
2
129
Containers aninhados
130
Banco de Dados
131
Banco de Dados
Drivers disponveis:
132
Banco de Dados
133
Banco de Dados
134
Banco de Dados
1
2
3
4
5
6
7
8
QSqlQuery query;
query.exec("SELECT title, year FROM cd WHERE year >= 1998");
while (query.next())
{
QString title = query.value(0).toString();
int year = query.value(1).toInt();
qDebug() << title << ": " << year << endl;
}
135
Banco de Dados
1
2
3
4
5
6
7
8
9
10
11
12
13
136
Banco de Dados
1
2
3
4
5
6
7
8
9
10
11
12
13
137
Banco de Dados
1
2
3
4
5
6
7
8
9
10
11
138
Banco de Dados
Incluindo registros
1
...
2
connect(model, SIGNAL(beforeInsert(QSqlRecord &)),
3
this, SLOT(beforeInsertArtist(QSqlRecord &)));
4
...
5
6
int row = model->rowCount();
7
model->insertRow(row);
8
QModelIndex index = model->index(row, Artist_Name);
9
tableView->setCurrentIndex(index);
10
tableView->edit(index);
11
12
13 void ArtistForm::beforeInsertArtist(QSqlRecord &record)
14 {
15
record.setValue("id", generateId("artist"));
16 }
Desenvolvimento Cross-Platform com C++ e Qt - Sandro Santos Andrade - Live Blue
139
Banco de Dados
Incluindo registros
1
2
3
4
5
6
7
8
9
140
Banco de Dados
Excluindo registros
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 }
tableView->setFocus();
QModelIndex index = tableView->currentIndex();
if (!index.isValid()) return;
QSqlRecord record = model->record(index.row());
QSqlTableModel cdModel;
cdModel.setTable("cd");
cdModel.setFilter("artistid = " + record.value("id").toString());
cdModel.select();
if (cdModel.rowCount() == 0) {
model->removeRow(tableView->currentIndex().row());
} else {
QMessageBox::information(this,
tr("Delete Artist"),
tr("Cannot delete %1 because there are CDs associated "
"with this artist in the collection.")
.arg(record.value("name").toString()));
141
Incluso e Excluso
142
Banco de Dados
Relacionamentos e master-detail
1
2
3
4
5
connect(cdTableView->selectionModel(),
SIGNAL(currentRowChanged(const QModelIndex &,
const QModelIndex &)),
this,
SLOT(currentCdChanged(const QModelIndex &)));
143
Lab14: Master-Detail
QSqlRelationalTableModel
QSqlTableModel
144
Graphics View
145
Graphics View
Arquitetura:
Cena:
View:
146
Graphics View
Arquitetura:
Item:
147
Graphics View
Arquitetura:
Itens pr-definidos no Qt
148
Graphics View
Sistemas de Coordenadas:
149
Graphics View
1
2
3
4
5
6
7
8
9
10
11
150
Lab15: Coordenadas
1
2
3
4
5
6
7
8
9
10
11
QGraphicsScene scene;
QGraphicsRectItem *rect = scene.addRect(QRectF(0, 0, 100, 100));
QGraphicsItem *item = scene.itemAt(50, 50);
// item == rect
QGraphicsScene scene;
myPopulateScene(&scene);
QGraphicsView view(&scene);
view.show();
151
152
XML
153
XML
154
XML
1
2
3
<doc>
<quote>Ars longa vita brevis</quote>
</doc>
1
2
3
4
5
6
7
startDocument()
startElement("doc")
startElement("quote")
characters("Ars longa vita brevis")
endElement("quote")
endElement("doc")
endDocument()
155
1
2
3
4
5
6
7
8
9
10 private:
11
QTreeWidget *treeWidget;
12 };
156
XML
Processando o XML
157
XML
158
1
2
3
4
5
6
7
8
9
QDomDocument doc("mydocument");
QFile file("mydocument.xml");
if (!file.open(QIODevice::ReadOnly))
return;
if (!doc.setContent(&file)) {
file.close();
return;
}
file.close();
159
10
11
12
13
14
15
16
17
18
19
20
21
22
23
160
XML
161
Networking
162
Lab19: Networking
1
2
3
4
5
Usando o QNetworkAccessManager
163
Internacionalizao
164
Internacionalizao
165
Lab20: Internacionalizao
166
Multithreading
O desenvolvimento de aplicaes
multithreading importante para um melhor
aproveitamento dos recursos da mquina e
melhorias em aspectos ligados a escalabilidade
e tempo de resposta
O Qt disponibiliza duas API's para
multithreading:
QThread e associados
QtConcurrent
167
Multithreading
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
MyThread t;
t.start(); // dettach
168
Multithreading
Desenvolvimento multithreading
frequentemente leva a condies de corrida
Class Qt para sincronizao de threads:
169
Multithreading
Reentrncia e Thread-Safety:
170
Multithreading
1
2
3
4
5
6
7
8
9
10
11
12
171
Lab21: Sincronizando
1
2
3
4
5
6
7
8
9
10
11
12
13
172
Lab22: QtConcurrent
1
2
3
4
5
6
7
8
Usando QtConcurrent
173
Plugins
O que um plugin ?
174
Plugins
Plugin Wrapper
Plugin Handler
Desenvolvimento Cross-Platform com C++ e Qt - Sandro Santos Andrade - Live Blue
175
Plugins
Plugin Wrapper
Plugin Handler
Desenvolvimento Cross-Platform com C++ e Qt - Sandro Santos Andrade - Live Blue
176
Plugins
Plugin Wrapper
Plugin Handler
Classe que
efetivamente
implementa a
funcionalidade
177
Plugins
QAccessibleBridgePlugin
QAccessiblePlugin
QIconEnginePlugin
QImageIOPlugin
QInputContextPlugin
QPictureFormatPlugin
QSqlDriverPlugin
QStylePlugin
QTextCodecPlugin
Handler
QAccessibleBridge
QAccessibleInterface
QIconEngine
QImageIOHandler
QInputContext
QSqlDriver
QStyle
QTextCodec
178
Plugins
179
Plugins
180
Lab23: Plugins
181
Lab24: Qt Mobile
182
Concluso
Programas:
Qt in Education
NOKIA Qt Certification
Qt Dev Days
Akademy
Akademy-BR
Desenvolvimento Cross-Platform com C++ e Qt - Sandro Santos Andrade - Live Blue
183
FIM
Desenvolvimento
Cross-Platform com C++ e Qt
Sandro Santos Andrade
sandroandrade@kde.org
184