We then create a QBrush that will be used to fill anything drawn by the QPainter object. 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. On line 170, we define a QRadialGradient, which produces the "reflective" look of the LED widget. First, we handle the alignment property which specifies how the LED should fit into the space allocated to it by whatever layout within which the LED widget is contained (lines 149 to 168). on QPaintDevice objects, which include QWidget and QPixmap. QPainter has many methods for drawing lines, rectangles, text, etc. Line 145 squelches warnings about unused variables. When the Qt event loop finds this event, it invokes LED::paintEvent (line 143). We want to see changes immediately though, so we call update. We would next see it if we caused the LED widget to be hidden and then exposed again. If we didn't call update the state change would not be evident immediately. ![]() The call results in a QPaintEvent object being put on the Qt event queue. On line 113 the private variable state_ is set and then on line 114 we make a call to update(). The minimumSizeHint method returns the recommended minimum size for your widget. The heightForWidth method (line 48) returns the preferred height for the widget given the width. The virtual method sizeHint (line 49 below) should be overridden by your custom widget to give Qt's layout objects a clue about how to size the widget. It is important for a widget to behave properly when managed by one of Qt's layout managers in terms of size and location. The other slots are variations on the setState and setFlashing setters. This allows a programmer to connect signals from other widgets to the LED slots to control it. Notice that setState, setFlashing, and setFlashRate are marked as slots (lines 40, 42, and 43). Lines 24 to 45 define the property reader and writer methods:ģ1 void setAlignment(Qt::Alignment alignment) ![]() Additional parameters are described in the Qt documentation. For each property, we define "setter" and "getter" methods using the WRITE and READ parameters (leaving out the WRITE parameter makes the property read-only). The first parameter is the data type and the second is the property name. Moc recognizes Q_PROPERTY and parses its parameters the C++ pre-processor never sees it. ![]() Q_PROPERTY is for the benefit of the Qt meta-object compiler (moc). Q_PROPERTY is a C++ macro that evaluates to nothing in the C++ code that is output by the C++ pre-processor. Lines 13 to 18 specify six properties of the LED widget. More importantly for our purposes, properties are exposed in the Qt Designer property editor. A property is a value that can be read and (optionally) written. Properties are an important feature of the Qt meta-object system. LED is a QObject that defines properties and slots so we need the Q_OBJECT macro (line 11). On some platforms, it evaluates to empty. ![]() The QDESIGNER_WIDGET_EXPORT macro inserts OS-specific code that ensures the LED class will be exported in the shared library or DLL properly. This pulls in everything needed to make the widget designable. On line 4, we include the Qt Designer header file.
0 Comments
Leave a Reply. |