Basic QT C++ Syntaxes
Data type represents the basic type information for a
basic element. The naming convention follows. For numeric types it is FLOAT,
SIGNED, or UNSIGNED followed by the _BITS where BITS is the size of the data.
BOOLEAN is a true / false (1,0) represented in an 8 bit container. The UNSIGNED
variants with multiple bit definitions are for packed graphical data formats and
represent vectors with per vector member sizes which are treated as a single
unit for packing and alignment purposes. MATRIX the three matrix types contain
FLOAT_32 elements and are treated as 32 bits for alignment purposes. RS_*
objects. 32 bit opaque handles.
Basic Types & Qt Core Classes
- Basic data types are normal C++ data types
- int, bool, double, char etc.
- Structs and arrays are created normally
- Qt variants of most of the containers in the C++ Standard
- For String types, Qt holds its own type : QString
- QDate, QDateTime : – Can be compared, converted to strings
- QChar – 16-bit Unicode character
- QString – Unicode character string. Can be resized, may contain 8-bit terminating strings or binary data
- QByteArray - Used instead of QString, when memory conservation is important (Qt for embedded Linux)
- QEventLoop, QEvent – Used to enter and exit event loop
- QHash – Template providing a hash-table-based dictionar
- QQueue – Template class implementing a FIFO queue
- QPoint, QRect – Rectangle is defined using the top left and bottom right
- QTimer – One shot or periodic 1 ms timer (accuracy depends on platform)
- QVariant – Union of the common Qt types
- QVector – Template class for dynamic arrays (flat), QLinkedList more efficient, if many insertion and deletion operations needed
- Iterator classes – Java (QVectorIterator) and STL-like
Identifier Naming Rules
- Can consist of upper and lower case letters, digits, dollar sign ($) and the underscore ( _ ) character.
- Must begin with a letter, dollar sign, or an underscore
- Are case sensitive
- Keywords cannot be used as identifiers
- Within a given section of your program or scope, each user defined item must have a unique identifier
- Can be of any length.
- Heart of Qt's object model
- Base class for all object classes
- So, all QWidgets are QObjects also
- Provides object trees and object ownership
- QObject's responsibility is to provide a central location for the most important concepts in Qt
Has three major responsibilities
- Introspection (runtime identification of object types)
Parent/Child Relationship
- Each QObject instance may take a parent argument
- Child informs its parent about its existence, upon which the parent adds it to its own list of children
- If a widget object does not have a parent, it is a window
- The parent does the following for its children: Hides and shows children, when hidden/shown itself
- Enables and disables children when enabled or disabled itself
- Note that a child may be explicitly hidden, although the parent is shown
- Objects inheriting from QObject are allocated on the heap using new
- If a parent object is assigned, it takes ownership of the newly created object – and eventually calls delete
- QLabel *label = new QLabel(”Hello World”, parent);
- Objects not inheriting QObject are allocated on the stack, not the heap
- QFile and QApplication (inheriting QObject) are usually allocated on the stack
- Modal dialogs are often allocated on the stack, too
- More secure than callbacks, more flexible than virtual
- Many-to-many relationship
- A signal is a way to inform a possible observer that something of interest has happened inside the observed class
- A QPushButton is clicked
- An asynchronous service handler is finished
- Value of QSlider is changed
- Signals are member functions that are automatically implemented in the meta-object
- Only the function declaration is provided by the developer
- Signal is sent, or emitted , using the keyword emit
- emit clicked();
- emit someSignal(7, “Hello”);
- A slot is a function that is to be executed when a signal has been emitted.
- (When QPushButton is pressed), close QDialog
- (When service is ready), ask for the value and store it
- (When QSlider value is changed), show a new value in QLCDNumber
- A Slot function is a normal member function implemented by the developer
- The signals and slots mechanism is fundamental to Qt programming. It enables the application programmer to bind objects together without the objects knowing anything about each other.
- Slots are almost identical to ordinary C++ member functions.
- The main difference is that they can be connected to a signal - in which case it is automatically called when the signal is emitted.
- To setup the signal-slot connection, we must first define the connection.
Signals and Slots
.
The connect() statement looks like this:
connect(sender, SIGNAL(signal), receiver, SLOT(slot));
Main features:
One signal can be connected to many slots
-
connect(slider, SIGNAL(valueChanged(int)),
spinBox, SLOT(setValue(int)));
- Many signals can be connected to the same slot
- connect(lcd,,SIGNAL(overflow()))
-
this, SLOT(handleMathError()));
connect(calculator, SIGNAL(divisionByZero()),
this, SLOT(handleMathError()));
- A signal can be connected to another signal
-
connect(lineEdit, SIGNAL(textChanged(const QString &)),
this, SIGNAL(updateRecord(const QString &)));
- .
- Connections can be removed
disconnect(lcd, SIGNAL(overflow()),
this, SLOT(handleMathError()));
Exceptionally if a signal has more parameters than the slot it is connected to, the additional parameters are ignored
connect(ftp, SIGNAL(rawCommandReply(int, const QString &)),
|