World Time Clock Plugin Example▲

In this example, we simply extend the Custom Widget Plugin example and its custom widget (based on the Analog Clock example), by introducing the concept of signals and slots.
The World Time Clock Plugin example consists of two classes:
-
WorldTimeClock is a custom clock widget with hour and minute hands that is automatically updated every few seconds.
-
WorldTimeClockPlugin exposes the WorldTimeClock class to Qt Designer.
First we will take a look at the WorldTimeClock class which extends the Custom Widget Plugin example's AnalogClock class by providing a signal and a slot. Then we will take a quick look at the WorldTimeClockPlugin class, but this class is in most parts identical to the Custom Widget Plugin example's implementation.
Finally we take a look at the plugin's project file. The project file for custom widget plugins needs some additional information to ensure that they will work within Qt Designer. This is also covered in the Custom Widget Plugin example, but due to its importance (custom widget plugins rely on components supplied with Qt Designer which must be specified in the project file that we use) we will repeat it here.
WorldTimeClock Class▲
The WorldTimeClock class inherits QWidget, and is a custom clock widget with hour and minute hands that is automatically updated every few seconds. What makes this example different from the Custom Widget Plugin example, is the introduction of the signal and slot in the custom widget class:
class
QDESIGNER_WIDGET_EXPORT WorldTimeClock : public
QWidget
{
Q_OBJECT
public
:
explicit
WorldTimeClock(QWidget *
parent =
nullptr
);
public
slots:
void
setTimeZone(int
hourOffset);
signals
:
void
updated(QTime currentTime);
protected
:
void
paintEvent(QPaintEvent *
event) override
;
private
:
int
timeZoneOffset =
0
;
}
;
Note the use of the QDESIGNER_WIDGET_EXPORT macro. This is needed to ensure that Qt Designer can create instances of the widget on some platforms, but it is a good idea to use it on all platforms.
We declare the setTimeZone() slot with an associated timeZoneOffset variable, and we declare an updated() signal which takes the current time as argument and is emitted whenever the widget is repainted.

In Qt Designer's workspace we can then, for example, connect the WorldTimeClock widget's updated() signal to a QTimeEdit's setTime() slot using Qt Designer's mode for editing signal and slots.

We can also connect a QSpinBox's valueChanged() signal to the WorldTimeClock's setTimeZone() slot.
WorldTimeClockPlugin Class▲
The WorldTimeClockPlugin class exposes the WorldTimeClock class to Qt Designer. Its definition is equivalent to the Custom Widget Plugin example's plugin class which is explained in detail. The only part of the class definition that is specific to this particular custom widget is the class name:
class
WorldTimeClockPlugin : public
QObject,
public
QDesignerCustomWidgetInterface
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QDesignerCustomWidgetInterface"
)
Q_INTERFACES(QDesignerCustomWidgetInterface)
public
:
explicit
WorldTimeClockPlugin(QObject *
parent =
nullptr
);
bool
isContainer() const