1 / 19

Qt Mobile Programming Training Kit

Qt Mobile Programming Training Kit. Agus Kurniawan agusk@cs.ui.ac.id http://blog.aguskurniawan.net. Qt Object Model. Agus Kurniawan agusk@cs.ui.ac.id http://blog.aguskurniawan.net. The QObject. QObject is the base class of almost all Qt classes and all widgets

chesna
Download Presentation

Qt Mobile Programming Training Kit

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Qt Mobile ProgrammingTraining Kit Agus Kurniawan agusk@cs.ui.ac.id http://blog.aguskurniawan.net

  2. Qt Object Model Agus Kurniawan agusk@cs.ui.ac.id http://blog.aguskurniawan.net

  3. The QObject • QObject is the base class of almost all Qt classes and all widgets • It contains many of the mechanisms that make up Qt • events • signals and slots • properties • memory management

  4. The QObject • QObject is the base class to most Qt classes. Examples of exceptions are: • Classes that need to be lightweight such as graphical primitives • Data containers (QString, QList, QChar, etc) • Classes that needs to be copyable, as QObjects cannot be copied

  5. The QObject • They can have a name (QObject::objectName) • They are placed in a hierarchy of QObject instances • They can have connections to other QObject instances • Example: does it make sense to copy a widget at run-time? “QObject instances are individuals!”

  6. Meta data • Qt implements introspection in C++ • Every QObject has a meta object • The meta object knows about • class name (QObject::className) • inheritance (QObject::inherits) • properties • signals and slots • general information (QObject::classInfo)

  7. Meta data • The meta data is gathered at compile time by the meta object compiler, moc. Ordinary C++ Build Process sources *.cpp compiles object files *.o links executables includes headers *.h

  8. Meta data • The meta data is gathered at compile time by the meta object compiler, moc. • The moc harvests data from your headers. Qt C++ Build Process sources *.cpp compiles object files *.o links executables includes compiles headers *.h mocs generated moc_*.cpp

  9. Introspection Enables dynamic casting without RTTI The meta object knows about the details Example:It is possible to convert enumeration values to strings for easier reading and storing • The classes know about themselves at run-time • Great for implementingscripting and dynamic language bindings if (object->inherits("QAbstractItemView")) { QAbstractItemView *view = static_cast<QAbstractItemView*>(widget); view->... enum CapitalsEnum { Oslo, Helsinki, Stockholm, Copenhagen }; int index = object->metaObject()->indexOfEnumerator("CapitalsEnum"); object->metaObject()->enumerator(index)->key(object->capital());

  10. Properties Getter, const, returns value, takes no arguments Setter, returns void, takes value as only argument • QObject have properties with getter and setter methods • Naming policy: color, setColor • For booleans: isEnabled, setEnabled class QLabel : public QFrame { Q_OBJECT Q_PROPERTY(QString text READ text WRITE setText) public: QString text() const; public slots: void setText(const QString &); };

  11. Properties • Why setter methods? • Possible to validate settings • Possible to react to changes void setMin( intnewMin ) { if( newMin > m_max ) { qWarning("Ignoring setMin(%d) as min > max.", newMin); return; } ... void setMin( int newMin ) { ... m_min = newMin; updateMinimum(); }

  12. Properties • Why getter method? • Indirect properties QSize size() const { return m_size; } int width() const { return m_size.width(); }

  13. Properties Q_PROPERTY(type name READ getFunction [WRITE setFunction] [RESET resetFunction] [NOTIFY notifySignal] [DESIGNABLE bool] [SCRIPTABLE bool] [STORED bool] [USER bool] [CONSTANT] [FINAL])

  14. Using properties • Direct access • Through the meta info and property system • Discover properties at run-time QString text = label->text(); label->setText("Hello World!"); QString text = object->property("text").toString(); object->setProperty("text", "Hello World"); int QMetaObject::propertyCount(); QMetaProperty QMetaObject::property(i); QMetaProperty::name/isConstant/isDesignable/read/write/...

  15. Dynamic properties true if the property has been defined using Q_PROPERTY false if it is dynamically added returns a list of the dynamic properties • Lets you add properties to objects at run-time • Can be used to “tag” objects, etc bool ret = object->setProperty(name, value); QObject::dynamicPropertyNames() const

  16. Creating custom properties Macro describing the property Initial value Getter Setter Private state class AngleObject : public QObject { Q_OBJECT Q_PROPERTY(qreal angle READ angle WRITE setAngle) public: AngleObject(qreal angle, QObject *parent = 0); qreal angle() const; void setAngle(qreal); private: qreal m_angle; };

  17. Creating custom properties Initial value Getter simply returns the value. Here you can calculate complex values. Update internal state, then react to the change. AngleObject::AngleObject(qreal angle, QObject *parent) : QObject(parent), m_angle(angle) { } qreal AngleObject::angle() const { return m_angle; } void AngleObject::setAngle(qreal angle) { m_angle = angle; doSomething(); }

  18. Custom properties - enumerations Macro informing Qt that AngleMode is an enum type. Property using enum as type. Ordinary enum declaration. class AngleObject : public QObject { Q_OBJECT Q_ENUMS(AngleMode) Q_PROPERTY(AngleMode angleMode READ ...) public: enum AngleMode {Radians, Degrees}; ... };

  19. Thank you Agus Kurniawan agusk@cs.ui.ac.id http://blog.aguskurniawan.net

More Related