Notes-6 Cos3711 Summarised DP
Notes-6 Cos3711 Summarised DP
Q_PROPERTY (type name READ getFunction [WRITE setfunction] [RESET resetFunction] [NOTIFY ntifySignal])
[. . . .]
Class Customer: public QObject{
Q_OBJECT
Customer::Customer(const QString name, QObject * parent)
Q_PROPERTY (QString id READ getID WRITE setID NOTIFY valueChanged)
:QObject(parent){
Q_PROPERTY (QString name READ getName WRITE setName)
setObjectName(name);
Q_PROPERTY (CustomerType type READ getType WRITE setType)
}
Q_PROPERTY (Qdate dateEstablished READ getDateEstablished) //read-only
public:
Void Customer::setId(const QString &newId){
enum CustomerType {Corporate, Individual,
if (newId !- m_id) {
Educational, Government};
Qstring oldId = m_id;
Q_ENUMS(CustomerType);
m_id = newId;
emit valueChanged( id newId, oldId);
explicit Customer (const QString name = QString(),
}
QObject * parent = 0);
Void Customer::setType(CustomerType theType){
QString getId() const {
if (m_Type != theType) {
return m_id;
CustomerType oldType = m_type;
}
m_Type = theType;
}
QString getName() const {
return m_name;
Void TestCustomerProps::test() {
}
Customer cust;
cust.setObjectName( Customer
CustomerType getType() const {
cust.setName( Falafal
return m_type;
cust.setType( Government //enumproperty as string
}
QString originalId =
private:
cust.setId(orifinalId);
QString m_id, m_name;
QVariant V = cust.propert id
CustomerType m_type;
QString str = v.toString();
};
return;
[. . . .]
}
[. . . .]
Model
Data
#include <QtGui>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QFileSystemModel model;
model.setRootPath("/");
QTreeView tree;
tree.setModel(&model);
tree.setSortingEnabled(true); 1
tree.header()-
>setResizeMode(QHeaderView::ResizeToContents);
tree.resize(640, 480);
tree.show();
return app.exec();
}
Qt provides its model-view architecture to separate models and view. In this architecture a number of abstract and concrete model and view classes are
provided. Though this architecture supports the separation of model/logic from the views, it does not provide controller classes like the classic MVC pattern.
Qt s model-view architecture uses signals and slots for the communication between models and views.
A model class in Qt either has the data or communicates with the data source. In order to make use of Qt s model-view architecture one has to either use
the built-in, concrete model or view classes or implement a model or view class derived from model/view interfaces provided in Qt. In order to make use of
the architecture the view has to be set up with a model. In other words, view has a model and not the other way around.
In a sense, Qt replaces the controller in its model-view architecture with delegates that are
responsible for the rendering/display and editing of model data in a view.
int main (int argc, char* argv[]){
Regular Expression
Special characters
. any character
\n newline QLineEdit e;
\f form feed QRegExp re( a-zA-Z][_a-zA-Z0-9]+
\t tab QRegExpValidator *v = new QRegExpValidator(re);
\xhhhh hex Chracter Sets: e.setValidator(v);
\S white space e.show();
Quantifiers \S non-whitepace
+ 1 or more \d digital 0 to 9 Consist 4 digits (0-9):
? 0 or 1 \D non-digital d{4}
* 0 or more \w any word character (letter/
{i, j} at leat i, no more than j digit/underscore) Consist 6 character, forst 3 alphabetic, last 3
\W non-word character digits
A-Za-z]{3}[0-9]{3}
[AEIOU] match A, E, I, O, U
[a-g] range from a to g Minimum 4, max 6 characters, except0, z and Z
[^xyz] except x, y, z zZ]{4,6}
Three ways parse XML: QT+ - xml SAX
Qt s XML module:
Parsing Xml 1) SAX (Simple API for XML) – parse event-driven
2) DOM (Document Object Module) – tree-style parse
QXmlReader QXmlContentHandler
3) Stream-style parsing with QXmlStreamReader +parse() +startDocument()
+endDocument()
Each <tag> must have closing </tag>
+startElement()
Or selfclosing <br/> NB: Case-sensitive
+endElement()
QXmlSimpleReader +characters
Tags with attributes:
<library>
<book title = Computer Algorithm pages = • Event driven
<book title = C++ unleashed pages = • Low-level QXmlDefaultHandler
</library> • Sequential-access while parsing document
• Any file size
Tags with text: • Forward direction
<library> MyHandler
<book>
<title>Computer Algotithm</title>
<pages>688</pages>
Myhandler.cpp
</book>
QTextStream cout(stdout);
<book>
<title>C++ unleashed</title>
bool MyHandler::startDocument(){
<pages>918</pages>
indent =
</book>
return TRUE;}
</library>
bool MyHandler::characters(const Qstring & text){
QString t = text;
#include <QXmlDefaultHandler> cout << t.remove( n
class Qstring;
class MyHandler:public QXmlDefaultHandler{ Bool MayHandler::startElement(const Qstring & namespaceURI,
public: const QString & LocalName,
bool startDocument(); const QString & qName,
bool startElement(const Qstring & namespaceURI, const QXmlAttributes & atts){
const QString & LocalName, QString str = QString n%1\\%2).arg(indent).arg(qName);
const QString & qName, cout << str;
const QXmlAttributes & atts); if (atts.lenght()>0){
bool characters (const QString & text); QString fildName = atts.qName(0);
bool endElement (const QString & namespaceURI, cout << QString
const QString & localName, .arg(fieldName).arg(fieldName);
const QString & qName); }
private: cout <<
QString indents; indent +=
}; return TRUE;
}
Write to file: (open filem create text stream, call toStrin() method of DOM document)
int main (int argc, char **argv){
DOM QApplication a (argc, argv);
QDomDocument doc AdBookML
QDomElement root = doc.createElement adbook
doc.appendChild(root);
• XML elements as objects in tree structure
• File in memory, limit RAM Contact c;
• Random access
• Not handle parse errors c.name = Kal
• Creating documents c.eMail = [email protected]
c.phone =
ConcreteProduct
ConcreteFactory
BreadFactory
WhiteBread
+ makeBread(Qstring):Bread
BrownBread
WholewheatBread
create
Abstract Factory Pattern
ConcreteFactory2 ConcreteFactory1
AbstractProductB
createProductA() createPRoductA()
createProductB() createProductB()
ProductB1 ProductB2
Class has only one instance, accessed via global access point
Singleton Make constructor Private, creating one instance
Class A {
public:
static A* getInstance();
private:
A();
static A* onlyInstance;
}
A*A::onlyInstance = NULL;
A::A(){}
A*A::getInstance(){
if(onlyInstance == 0)
onlyInstance = new A();
return onlyInstance;
}
Object storing snapshot of internal state of another object
Memento Pattern
Object state restored, allow undo/rollback operation
Originator
Object know how to save itself
setMemento(Memento m) getState()
createMemento() setState()
state state
Class Memento
{
public:
private: friend class Person;
Memento();
QStringList getState();
void setState(QStringList s);
QStringList state;
};
Void Person::setMemento(Memento m)
{
QStringList s = m.getState();
name = s.at(0);
birthDate = QDate::fromString(s.at(1));
}
LogTail::~LogTail(){
terminate();
}
• QThread class provide platform-independent threads
QThread and • Qt support multi-threading and encapsulates threads using QThread class
QtConcurrent •
•
QObjects are thread safe and re-entrant, communicate across threads
Only have one GUI thread (QApplication)
• Widgets inheritedfrom QWidget not thread safe
Thread has:
> Own stack pointer
> instruction (program) counter
> processor registers
> execution state
Access to:
> shared resources (memory, wait conditionsm nutual exclusive data blocks, semaphores
Void MyThread::run(){
QTcpSocket socket;
socket.connectToHost(hostName, portNumber);
exec();
}
WebKit: open source web content rendering and editing engine
Networking Concepts
QT += webkit
http://qt-project.org/products/qt-for-mobile
QUrl class:
ftp://ftp.qt.nokia.com:2021
path
Scheme
Host port
ftp://person:[email protected]:2021
http://qt-project.org/documents#qt-other
userName
fragment password
TCP Sockets UDP Sockets
Guaranteed in-order delivery File and forget QT += network
Point-to-point only Point-to-pint or broadcast
- Memento()
+ Person(QString, QDate) - getState)_: QStringList
+ setMemento(Memento) - setState(QStringList)
+ createMemeonto(): Memento
Caretaker
- person: Person
- memento: Memento