QT6 源(90):阅读与注释 LCD显示类 QLCDNumber ,源代码以及属性测试。该类继承于容器框架 QFrame
(1)本类所在的类继承关系如下:
(2)segmentStyle 属性,后俩属性的区别很小 :
++
(3) smallDecimalPoint 属性 :
(4) 本 LCD 不仅可以展示数字,也可以展示内容是数字的文本,还可以带温度的度数标识 :
(5) 关于溢出信号的测试 :
(6)本源代码来自于头文件 qlcdnumber . h :
#ifndef QLCDNUMBER_H
#define QLCDNUMBER_H#include <QtWidgets/qtwidgetsglobal.h>
#include <QtWidgets/qframe.h>QT_BEGIN_NAMESPACE // 说明本类定义于 QT的全局命名空间里QT_REQUIRE_CONFIG(lcdnumber);class QLCDNumberPrivate;/*
The QLCDNumber widget displays a number with LCD-like digits.它可以显示几乎任何大小的数字。它可以显示小数、十六进制、八进制或二进制数字。
使用display()槽可以轻松连接到数据源,该槽被重载以接受五种参数类型中的任何一种。
还有用于使用 setMode()更改基数和 setSmallDecimalPoint()更改小数点的位置。
当QLCDNumber被要求显示超出其范围的内容时,它会发出overflow()信号。
范围由setDigitcount()设置,但setSmallDecimalPoint()也会影响它。
如果显示设置为十六进制八进制或二进制,则显示值的整数等效值。可以显示这些数字和其他符号:0/O、1、2、3、4、5/S、6、7、8、9/g、减号、小数点、
A、B、C、D、EF、h、H、L、0、P、r、u、U、Y、冒号、度数符号(在字符串中指定为单引号)和空格。
QLCDNumber将空格替换为非法字符。
无法检索 QLCDNumber对象的内容,但您可以使用 value()检索数值。
如果您确实需要文本,我们建议您将传递到display()槽的信号也连接到另一个槽,并在那里存储值。
顺便说一下,QLCDNumber是Qt中最古老的部分,它的根源可以追溯到Sinclair Spectrum上的一个BASIC程序。*/class Q_WIDGETS_EXPORT QLCDNumber : public QFrame // LCD number widget
{Q_OBJECT //插入此宏,以使用 Qt的元对象系统//此属性保存显示值,将其四舍五入到最接近的整数。//此属性对应于LCDNumber显示的当前值的最近整数。这是十六进制、八进制和二进制模式使用的值。//如果显示值不是数字,则该属性的值为0。 默认情况下,此属性包含值为 0。Q_PROPERTY(int intValue READ intValue WRITE display) //±整数Q_PROPERTY(Mode mode READ mode WRITE setMode) //整数的进制 2 8 10 16//此属性保存当前显示模式(数字进制)。//对应当前的显示模式,这是Bin、Oct、Dec(默认)和Hex模式之一。//Dec模式可以显示浮点数,其他模式显示整数等效值。//此属性保存显示值。此属性对应于LCDNumber显示的当前值。//如果显示值不是数字,则该属性的值为0。 默认情况下,此属性包含值为 0。Q_PROPERTY(double value READ value WRITE display) //浮点数Q_PROPERTY(bool smallDecimalPoint // 此属性为 true 就是表示把小数点小一点来显示。READ smallDecimalPoint WRITE setSmallDecimalPoint) //默认为 false//This property holds the style of the decimal point。//If true the decimal point is drawn between two digit positions.//Otherwise it occupies a digit position of its own,//i.e. is drawn in a digit position. The default is false.//The inter-digit space is made slightly wider when the// decimal point is drawn between the digits.//Returns the current number of digits. 若显示小数点,则小数点始终占据其中的一位Q_PROPERTY(int digitCount READ digitCount WRITE setDigitCount) //显示总位数Q_PROPERTY(SegmentStyle segmentStyle READ segmentStyle WRITE setSegmentStyle)//This property holds the style of the LCDNumber。//Outline ---- Produces raised segments filled with the background color。// 生成填充为背景颜色的凸起部分//Filled (this is the default). ---- 生成填充前景色的凸起部分。// ---- Produces raised segments filled with the foreground color.//Flat ---- Produces flat segments filled with the foreground color.// ---- 生成填充前景色的平面片段。private:Q_DISABLE_COPY(QLCDNumber)Q_DECLARE_PRIVATE(QLCDNumber)public://构造一个LCD数字,设置数字位数为5,基数为十进制,小数点模式为“小”,框架样式为凸起框。//segmentStyle()被设置为Outline。父类参数传递给 QFrame 构造函数。explicit QLCDNumber(QWidget * parent = nullptr);explicit QLCDNumber(uint numDigits, QWidget * parent = nullptr);//Constructs an LCD number, sets the number of digits to numDigits,//the base to decimal, the decimal point mode to 'small' and the//frame style to a raised box. The segmentStyle() is set to Filled.//The parent argument is passed to the QFrame constructor.~QLCDNumber();enum Mode { Hex, Dec, Oct, Bin };Q_ENUM(Mode)enum SegmentStyle { Outline, Filled, Flat };Q_ENUM(SegmentStyle)//Q_PROPERTY(int intValue READ intValue WRITE display) //±整数int intValue() const; //这是个读函数
public Q_SLOTS:void display(int num); //这是个写函数public :
//Q_PROPERTY(Mode mode READ mode WRITE setMode) //整数的进制 2 8 10 16Mode mode() const;void setMode(Mode); //此函数并不是槽函数
public Q_SLOTS:void setHexMode(); //这些不带形参的才是槽函数void setDecMode();void setOctMode();void setBinMode();public :
//Q_PROPERTY(double value READ value WRITE display) //浮点数double value() const;
public Q_SLOTS:void display(double num);public :
//Q_PROPERTY(bool smallDecimalPoint // 此属性为 true 就是表示把小数点小一点来显示。
// READ smallDecimalPoint WRITE setSmallDecimalPoint) //默认为 falsebool smallDecimalPoint() const;
public Q_SLOTS:void setSmallDecimalPoint(bool);public :
//Q_PROPERTY(int digitCount READ digitCount WRITE setDigitCount) //显示总位数int digitCount() const;void setDigitCount(int nDigits);//Q_PROPERTY(SegmentStyle segmentStyle READ segmentStyle WRITE setSegmentStyle)SegmentStyle segmentStyle() const;void setSegmentStyle(SegmentStyle);bool checkOverflow(int num) const;bool checkOverflow(double num) const;//Returns true if num is too big to be displayed in its entirety;//otherwise returns false.QSize sizeHint() const override;public Q_SLOTS://void display(int num);//void display(double num);void display(const QString & str);//显示由字符串str表示的数字。 此版本的函数忽路mode()和smallDecimalPoint()//可以显示这些数字和其他符号:0/O、1、2、3、4、5/S、6、7、8、9/g、减号、小数点、A、B、C//D、E、F、h、H、L、0、P、r、y、U、Y、冒号、度数符号(在字符串中指定为单引号)//和空格QLCDNumber将空格替换为非法字符。//Displays the number represented by the string s.//This version of the function disregards mode() and smallDecimalPoint().//These digits and other symbols can be shown: 0/O, 1, 2, 3, 4, 5/S, 6, 7, 8, 9/g,//minus, decimal point, A, B, C, D, E, F, h, H, L, o, P, r, u, U, Y, colon,//degree sign (which is specified as single quote in the string) and space.//QLCDNumber substitutes spaces for illegal characters.//以下是另一些槽函数://void setSmallDecimalPoint(bool);//void setHexMode();//void setDecMode();//void setOctMode();//void setBinMode();Q_SIGNALS:void overflow();//This signal is emitted whenever the QLCDNumber is asked to display a//too-large number or a too-long string.protected:bool event(QEvent * e) override;void paintEvent(QPaintEvent * ) override;}; //完结 class QLCDNumber : public QFrameQT_END_NAMESPACE#endif // QLCDNUMBER_H
(7)
谢谢