当前位置: 首页 > news >正文

QT6 源(66)篇三:阅读与注释类 QAbstractSpinBox ,这是螺旋框的基类,附上源码

(9)所有代码来自于头文件 qabstractspinbox . h :

#ifndef QABSTRACTSPINBOX_H
#define QABSTRACTSPINBOX_H#include <QtWidgets/qtwidgetsglobal.h>
#include <QtWidgets/qwidget.h>
#include <QtGui/qvalidator.h>/*
QT_CONFIG宏实现了对 Qt特性的安全编译时检查。特性可以处于三种状态:
0 或未定义:在测试时会引发编译错误
-1:该功能不可用
1:该功能可用The QT_CONFIG macro implements a safe compile time check for features of Qt.Features can be in three states:0 or undefined: This will lead to a compile error when testing for it-1: The feature is not available1: The feature is available
*/
#define QT_CONFIG(feature) (1/QT_FEATURE_##feature == 1)#define QT_REQUIRE_CONFIG(feature) Q_STATIC_ASSERT_X(QT_FEATURE_##feature == 1,\"Required feature " #feature " for file " __FILE__ " not available.")QT_REQUIRE_CONFIG(spinbox); //编译前的源代码检查QT_BEGIN_NAMESPACE          //说明螺旋框定义在 QT 的全局命名空间class QLineEdit;class QAbstractSpinBoxPrivate;
class QStyleOptionSpinBox;/*
这个类被设计为 QSpinBox、QDoubleSpinBox和 QDateTimeEdit 等小部件的通用超类。这里是类的主要属性:
1.文本:在 QAbstractSpinBox 中显示的文本。
2.对齐:QAbstractSpinBox中文本的对齐方式。
3.包装:QAbstractSpinBox是否从最小值包装到极大值,反之亦然。
Here are the main properties of the class:text     : The text that is displayed in the QAbstractSpinBox.alignment: The alignment of the text  in the QAbstractSpinBox.wrapping : Whether the QAbstractSpinBox wraps from the minimum value to themaximum value and vice versa.QAbstractSpinBox provides a virtual stepBy() function that is called whenever the
user triggers a step. This function takes an integer value to signify how many
steps were taken. E.g. Pressing Qt::Key_Down will trigger a call to stepBy(-1).
QAbstractSpinBox提供了一个虚拟的stepBy()函数,每当用户触发一步时都会调用这个函数。
这个函数接受一个整数值,表示走了多少步。例如,按下Qt::Key_Down将触发stepBy(-1)的调用。当用户按下Qt::.ControlModifier键并触发步骤时,QAbstractSpinBox将按10步进行,而不是一步。
比步骤修改器影响滚轮事件、键盘事件以及与旋转按钮的交互。
请注意,在macOS上,Control键对应于Command键。
When the user triggers a step whilst holding the Qt::ControlModifier,
QAbstractSpinBox steps by 10 instead of making a single step.
This step modifier affects wheel events,
key events and interaction with the spinbox buttons.
Note that on macOS, Control corresponds to the Command key.自Qt 5.12以来,可以使用QStyle::SH SpinBox StepModifier来选择哪个Qt:.
KeyboardModifier会增加步长。Qt::NoModifier禁用此功能。
QAbstractSpinBox还提供了一个虚拟函数stepEnabled()来确定是否允许在任何点上下滚动。
这个函数返回一个 StepEnabled 的位集。*///class Q_WIDGETS_EXPORT QWidget : public QObject, public QPaintDevice
class   Q_WIDGETS_EXPORT QAbstractSpinBox : public QWidget
{Q_OBJECT //又插入了此宏//This property holds the spin box's text, including any prefix and suffix.//There is no default text. 读取数值的函数 value() 应该是定义在子类里Q_PROPERTY(QString text     READ text) //本属性读取的是全部的文本 QStringQ_PROPERTY(bool    readOnly READ isReadOnly WRITE setReadOnly)//This property holds whether the spin box is read only.//In read-only mode, the user can still copy the text to the clipboard,//or drag and drop the text; but cannot edit it.//The QLineEdit in the QAbstractSpinBox does not show a cursor in read-only mode.//只读属性的螺旋框,将不再改变鼠标的形状。可写类型的螺旋框,会将进入的鼠标由箭头改为工字型//This property holds the alignment of the spin box//Possible Values are Qt::AlignLeft, Qt::AlignRight, and Qt::AlignHCenter.//By default, the alignment is Qt::AlignLeft//Attempting to set the alignment to an illegal flag combination does nothing.Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)//   定义了新类型 Qt::Alignment = QFlags<AlignmentFlag>  //对齐策略//enum AlignmentFlag { AlignLeft=0x0001, AlignRight=0x0002, AlignHCenter=0x0004...//This property holds the current button symbol mode//The possible values can be either UpDownArrows or PlusMinus.//The default is UpDownArrows.  //在有些样式中,这两种箭头图案与加减图案会被绘制成一样的//Note that some styles might render PlusMinus and UpDownArrows identically.Q_PROPERTY(ButtonSymbols buttonSymbols READ buttonSymbols WRITE setButtonSymbols)//枚举量就定义在本类 enum ButtonSymbols { UpDownArrows, PlusMinus, NoButtons };//This property holds whether the spin box draws itself with a frame.//If enabled (the default) the spin box draws itself inside a frame,//otherwise the spin box draws itself without any frame.Q_PROPERTY(bool frame READ hasFrame WRITE setFrame) //是否绘制边框//This property holds whether the spin box is circular.表示螺旋框中的值是否会循环//If wrapping is true stepping up from maximum() value will take you to the//minimum() value and vice versa.      //这是来自于拉丁语 vice versa//Wrapping only make sense if you have minimum() and maximum() values set.Q_PROPERTY(bool wrapping READ wrapping WRITE setWrapping) //从最大值递增到最小值。Q_PROPERTY(QString   specialValueText //即 minimum()最小值时的替换文本READ specialValueText WRITE setSpecialValueText)//这个属性持有特殊值的文本。//如果设置,当当前值等于最小值时,旋转框将显示此文本而不是数字值。//典型用法是表示此选择具有特殊(默认)含义。//例如,如果您的旋转框允许用户选择显示图像的比例因子(或缩放级别),//并且您的应用程序能够自动选择一个可以使图像完全适合显示窗口的比例因子(或缩放级别)。//您可以这样设置旋转框://  zoomSpinBox->setRange(0, 1000);//  zoomSpinBox->setSpecialValueText(tr("Automatic"));//然后,用户可以选择从 1%到 1000%的缩放比例,或者选择“自动”由应用程序选择。//然后您的代码必须将旋转框的值为0解释为来自用户的请求,以缩放图像以适合窗口。//所有值都显示前缀和后缀(如果设置),除了特殊值,特殊值只显示特殊值文本。//这个特殊文本在SpinBox::textChanged()信号中传递一个 QString。//要关闭特殊值文本显示,请使用空字符串调用此函数。//默认情况下没有特殊值文本,即数字值以通常方式显示。//如果未设置特殊值文本,则specialValueText()返回空字符串。Q_PROPERTY(bool accelerated READ isAccelerated WRITE setAccelerated)//This property holds whether the spin box will accelerate the frequency of the//steps when pressing the step Up/Down buttons.   // 默认是 true,自动加快速度//If enabled the spin box will increase/decrease the value faster the//longer you hold the button down.//这个属性表示,当按下“上/下”步进按钮时,步进框是否会加快步进频率。//如果启用,旋转框将更快地增加/减少值,您按住按钮的时间越长。//定义在本类中 enum CorrectionMode { CorrectToPreviousValue, CorrectToNearestValue };Q_PROPERTY(CorrectionMode correctionMode                 //如何修正输入的中间值READ correctionMode WRITE setCorrectionMode)//This property holds the mode to correct an Intermediate value if editing//finishes。 The default mode is QAbstractSpinBox::CorrectToPreviousValue.Q_PROPERTY(bool acceptableInput READ hasAcceptableInput) //验证输入的值//This property holds whether the input satisfies the current validation//这个属性表示输入是否满足当前的验证。Q_PROPERTY(bool showGroupSeparator //开启 true后,千分位用逗号 ,分割 15,000,000READ isGroupSeparatorShown WRITE setGroupSeparatorShown)//This property holds whether a thousands separator is enabled.//By default this property is false.//此属性表示是否启用了旋转框的键盘跟踪。//如果启用了键盘跟踪(默认为启用),则在从键盘输入新值时,//Spinbox会发出valueChanged()和textChanged()信号。//例如,当用户通过输入6、0和0来输入值600时,Spinbox会发出3个信号,分别是值6、60 和 600 。//如果禁用键盘跟踪,则在打字时,Spinbox不会发出valueChanged()和textChanged()信号。//它会在按下回车键、失去键盘焦点或使用其他Spinbox功能(例如按下箭头键)时稍后发出这些信号。Q_PROPERTY(bool keyboardTrackingREAD keyboardTracking WRITE setKeyboardTracking)private:Q_PRIVATE_SLOT(d_func(), void _q_editorTextChanged(const QString &))Q_PRIVATE_SLOT(d_func(), void _q_editorCursorPositionChanged(int, int))Q_DECLARE_PRIVATE(QAbstractSpinBox)Q_DISABLE_COPY(QAbstractSpinBox)friend class QAccessibleAbstractSpinBox;protected:  //这是一个保护权限,只让子类使用的构造函数QAbstractSpinBox(QAbstractSpinBoxPrivate &dd, QWidget *parent = nullptr);public:explicit QAbstractSpinBox(QWidget *parent = nullptr); //有参构造函数//Constructs an abstract spinbox with the given parent with default wrapping,//and alignment properties.~QAbstractSpinBox(); //虚的析构函数,其实其父类的也是虚的析构函数//Q_PROPERTY(QString text READ text) //本属性读取的是全部的文本 QStringQString text() const;//Q_PROPERTY(bool readOnly READ  isReadOnly WRITE setReadOnly)bool  isReadOnly() const;void setReadOnly(bool r);//Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)Qt::Alignment     alignment() const;void  setAlignment(Qt::Alignment flag);enum ButtonSymbols { UpDownArrows, PlusMinus, NoButtons };Q_ENUM(ButtonSymbols)
//Q_PROPERTY(ButtonSymbols buttonSymbols READ buttonSymbols WRITE setButtonSymbols)ButtonSymbols    buttonSymbols() const;void setButtonSymbols(ButtonSymbols bs);//Q_PROPERTY(bool frame READ hasFrame WRITE setFrame) //是否绘制边框bool hasFrame() const;void setFrame(bool);//Q_PROPERTY(bool wrapping READ wrapping WRITE setWrapping) //从最大值递增到最小值。bool    wrapping() const;void setWrapping(bool w);//Q_PROPERTY(QString   specialValueText //即 minimum()最小值时的替换文本
//                READ specialValueText WRITE setSpecialValueText)QString    specialValueText() const;void    setSpecialValueText(const QString & txt);//Q_PROPERTY(bool accelerated READ isAccelerated WRITE setAccelerated)bool   isAccelerated()  const; //按住鼠标不松自动递增void  setAccelerated(bool on);enum     CorrectionMode { CorrectToPreviousValue, CorrectToNearestValue };Q_ENUM(CorrectionMode)
//Q_PROPERTY(CorrectionMode correctionMode                 //如何修正输入的中间值
//                     READ correctionMode WRITE setCorrectionMode)CorrectionMode      correctionMode() const;void   setCorrectionMode(CorrectionMode cm);//Q_PROPERTY(bool acceptableInput READ hasAcceptableInput) //验证输入的值bool hasAcceptableInput() const;//Q_PROPERTY(bool showGroupSeparator //开启 true后,千分位用逗号 ,分割 15,000,000
//             READ isGroupSeparatorShown WRITE setGroupSeparatorShown)bool    isGroupSeparatorShown() const;void   setGroupSeparatorShown(bool shown);//Q_PROPERTY(bool keyboardTracking  //此属性表示是否启用了旋转框的键盘跟踪。
//           READ keyboardTracking WRITE setKeyboardTracking)bool      keyboardTracking() const;void   setKeyboardTracking(bool kt);QSize        sizeHint() const override;QSize minimumSizeHint() const override;//enum InputMethodQuery {....} 本函似乎是根据形参的限定来查询文本框的某方面的输入状态QVariant inputMethodQuery(Qt::InputMethodQuery) const override; //继承自父类//class QValidator : public QObject { // QT 中所有类都继承自 QObject//     enum State { Invalid, Intermediate, Acceptable };     };virtual QValidator::State validate(QString & input, int & pos) const;//This virtual function is called by the QAbstractSpinBox to determine//whether input is valid.//The pos parameter indicates the position in the string.//Reimplemented in the various subclasses.virtual void fixup(QString & input) const; //虚函数,可由子类重新实现//This virtual function is called by the QAbstractSpinBox//if the input is not validated to QValidator::Acceptable //此枚举值在上面的注释里//when Return is pressed or interpretText() is called.//It will try to change the text so it is valid.//Reimplemented in the various subclasses.void     interpretText(); //本函分析完螺旋框中的内容后,会依据值的变化发送信号//This function interprets the text of the spin box.//If the value has changed since last interpretation it will emit signals.//Virtual function that is called whenever the user triggers a step.//The steps parameter indicates how many steps were taken.//例如,按下Qt:KeyDown将触发调用stepBy(-1),而按下Qt::Key_PageUp将触发调用stepBy(10)。//如果你继承 QAbstractSpinBox类,你必须重新实现这个函数。//请注意,即使结果值将超出最小值和最大值的边界,这个函数也会被调用。//这个函数的任务就是处理这些情况。virtual void stepBy(int steps);enum StepType {  //这个枚举类,或许子类要用DefaultStepType,AdaptiveDecimalStepType};Q_ENUM(StepType) //注册到 QT 的元对象系统,涉及到 QMetaEnumQ_SIGNALS:           //其基类也会定义信号函数//This signal is emitted editing is finished.//This happens when the spinbox loses focus and when enter is pressed.void editingFinished();public Q_SLOTS://Steps up   by one linestep。//Calling this slot is analogous类似的 to calling stepBy(1);void stepUp   ();void stepDown ();//Steps down by one linestep。//Calling this slot is analogous      to calling stepBy(-1);//Selects all the text in the spinbox except the prefix and suffix.void selectAll(); //选中螺旋框里的输入框里的所有内容,但除掉前后缀virtual void clear    (); //清除螺旋框里的输入框里的所有内容,但保留前后缀//Clears the lineedit of all text but prefix and suffix.public:bool event(QEvent * event) override; //此函数继承自父类 QWidget//This function returns true if the event was recognized,//otherwise it returns false. If the recognized event was accepted//(see QEvent::accepted),//any further processing such as event propagation to the parent widget stops.//在本类内定义了这个枚举量enum StepEnabledFlag {  StepNone        = 0x00,StepUpEnabled   = 0x01,StepDownEnabled = 0x02 };Q_DECLARE_FLAGS(StepEnabled, StepEnabledFlag)//等于定义了新类型 StepEnabled = QFlags<StepEnabledFlag>
protected:virtual         StepEnabled stepEnabled() const;virtual void initStyleOption(QStyleOptionSpinBox * option) const;//Initialize option with the values from this QSpinBox. //此类定义与本类,非继承的//This method is useful for subclasses when they need a QStyleOptionSpinBox,//but don't want to fill in all the information themselves.QLineEdit *   lineEdit() const;void       setLineEdit(QLineEdit * edit);//class QLineEdit : public QWidget 这是螺旋框内嵌的行输入组件void       resizeEvent(QResizeEvent * event) override;void     keyPressEvent(QKeyEvent    * event) override;void   keyReleaseEvent(QKeyEvent    * event) override;#if QT_CONFIG(wheelevent)void        wheelEvent(QWheelEvent  * event) override;
#endifvoid      focusInEvent(QFocusEvent  * event) override;void     focusOutEvent(QFocusEvent  * event) override;#if QT_CONFIG(contextmenu)void  contextMenuEvent(QContextMenuEvent * event) override;
#endifvoid       changeEvent(QEvent       * event) override;void        closeEvent(QCloseEvent  * event) override;void         hideEvent(QHideEvent   * event) override;void   mousePressEvent(QMouseEvent  * event) override;void mouseReleaseEvent(QMouseEvent  * event) override;void    mouseMoveEvent(QMouseEvent  * event) override;void        timerEvent(QTimerEvent  * event) override;void        paintEvent(QPaintEvent  * event) override;void         showEvent(QShowEvent   * event) override;}; //完结 class QAbstractSpinBox : public QWidget
Q_DECLARE_OPERATORS_FOR_FLAGS(QAbstractSpinBox::StepEnabled) //定义枚举类的运算符QT_END_NAMESPACE#endif // QABSTRACTSPINBOX_H

(10)

谢谢

相关文章:

  • QML Image 组件详解
  • Stm32 烧录 Micropython
  • 深度学习框架搭建(Vscode/Anaconda/CUDA/Pytroch)
  • 小牛电动:荣登央视舞台,引领智能出行新潮流
  • 26考研——输入/输出系统(7)
  • 基于Redis实现-用户签到
  • 数据库 AI 助手测评:Chat2DB、SQLFlow 等工具如何提升开发效率?
  • 前端HTML基础知识
  • 超稳定性理论
  • AI Agent(2):Agent技术架构
  • 2025五一杯B题超详细解题思路
  • 【深度学习新浪潮】小米MiMo-7B报告内容浅析
  • 如何进行 JVM 性能调优?
  • Linux-04-搜索查找类命令
  • mono map
  • 【验证技能】文档要求和好文档注意点
  • 无缝监控:利用 AWS X-Ray 增强 S3 跨账户复制的可见性
  • Java 中使用正则表达式
  • OkHttp3.X 工具类封装:链式调用,支持HTTPS、重试、文件上传【内含常用设计模式设计示例】
  • 初学Vue之记事本案例
  • 挑大梁!一季度北上广等7省份进出口占外贸总值四分之三
  • 中国队夺跳水世界杯总决赛首金
  • A股2024年年报披露收官,四分之三公司盈利
  • 长三角铁路今日预计发送418万人次,持续迎来出行客流高峰
  • 国铁集团郑州局预计“五一”发送642.5万人
  • 笔墨如何“构城”?上海美院城市山水晋京展出