Lines 24 to 45 define the property reader and writer methods: 24 double diameter() const; 25 void set Diameter(double diameter); 26 27 QColor color() const; 28 void set Color(const QColor& color); 29 30 Qt:: Alignment alignment() const; 31 void set Alignment(Qt:: Alignment alignment); 32 33 bool state() const; 34 35 bool is Flashing() const; 36 37 int flash Rate() const; 38 39 public slots: 40 void set State(bool state); 41 void toggle State(); 42 void set Flashing(bool flashing); 43 void set Flash Rate(int rate); 44 void start Flashing(); 45 void stop Flashing(); This is where all the painting happens whenever the widget is shown (either initially or because an overlying window has moved away) or updated (for instance, by one of the property setters). On line 170, we define a which produces the "reflective" look of the LED widget. The pen is set to the color property's value, we turn anti-aliasing on (to avoid "jaggies"), set the brush to the radial gradient, and draw an ellipse, which is the actual LED.
On lines 173 to 178, we set the final color of the gradient to black if the state variable is false (off), or to the value of the color property. Finally, on lines 185 to 188, we turn on a timer to the flash rate property value if the flash property is true, or we turn off the timer if the flash property is false. We can insert it into layouts programmatically and we can use promotion in Qt Designer to place LED widgets into designs.
This blog post will describe how to write a custom Qt widget and how to integrate it into Qt Designer so that you can drag and drop it onto your designs.
It will also provide an understanding of important differences between Qt 4 and Qt 5 when it comes to creating designable widgets.
Moc recognizes Q_PROPERTY and parses its parameters; the C pre-processor never sees it.
The first parameter is the data type and the second is the property name.
This pulls in everything needed to make the widget designable. Properties are an important feature of the Qt meta-object system.
A property is a value that can be read and (optionally) written.
Below is what Qt Designer looks like with the LED widget on its palette.
Without this, we will not see the LED widget in the Designer palette.
We do not need this macro for Qt 5, the Q_PLUGIN_METADATA macro replaces it.
In Qt 5, it needs to be added to the QT variable instead.
On Linux, you can just run qmake to create a Makefile.