QT6 源(37):界面组件的总基类 QWidget 的源码阅读(下,c++ 代码部分)
(1) QT 在 c++ 的基础上增加了自己的编译器,以支持元对象系统和 UI 界面设计,有 MOC 、 UIC 等 QT 自己的编译器。本节的源代码里,为了减少篇幅,易于阅读,去除了上篇中的属性部分, 上篇,QWidget 的属性部分
(2)源代码都来自于头文件 qwidget . h ,注释来自于官方文档,或机翻或原文。两篇加起来是 2000 行:
#ifndef QWIDGET_H
#define QWIDGET_HQT_BEGIN_NAMESPACE //说明本类 QWidget定义于最大的 QT_BEGIN_NAMESPACE空间class QWidgetData {...} //本类 QWidget的数据成员//前已阅读了 QPaintDevice,此类没啥内容,只有一些读属性的函数,反馈绘图方面的配置属性
class Q_WIDGETS_EXPORT QWidget : public QObject, public QPaintDevice
{Q_OBJECT //宏定义,再次插入此宏private:Q_DISABLE_COPY(QWidget) //禁止对本类的复制Q_PRIVATE_SLOT(d_func(), void _q_showIfNotHidden())Q_PRIVATE_SLOT(d_func(), QWindow *_q_closestWindowHandle())QWidgetData * data; //本类的数据成员public:
public://This enum describes how to render the widget when calling QWidget::render().enum RenderFlag //这是定义了一个渲染标志{ DrawWindowBackground = 0x1, //默认情况下,此选项已启用。//如果您启用此选项,即使未设置 autoFillBackground,小部件的背景也会渲染到目标中。DrawChildren = 0x2,//如果您启用此选项,则小部件的子项将递归地渲染到目标中。默认情况下,此选项已启用。IgnoreMask = 0x4 //If you enable this option,//the widget's QWidget::mask() is ignored when rendering into the target.//By default, this option is disabled.};Q_DECLARE_FLAGS(RenderFlags, RenderFlag)//定义了枚举量 RenderFlags = QFlags<RenderFlag>//Constructs a widget which is a child of parent, with widget flags set to f.//The widget flags argument, f, is normally 0,//but it can be set to customize the frame of a window//(i.e. parent must be nullptr). To customize the frame,//use a value composed from the bitwise OR of any of the window flags.explicit QWidget( QWidget * parent = nullptr, //构造函数Qt::WindowFlags f = Qt::WindowFlags());// WindowFlags = QFlags<WindowType> 这个枚举类 WindowType 有20 多项~QWidget();int devType() const override; //无注释//typedef QIntegerForSizeof<void *>::Unsigned quintptr;//# define QT_PREPEND_NAMESPACE(name) ::name//typedef QT_PREPEND_NAMESPACE(quintptr) WId;//所以 WId = ::quintptr = 8 字节无符号数WId winId() const; //返回小部件的窗口系统标识符。//原则上是便携的,但如果你使用它,你可能会做一些不可移植的事情。小心。//如果小部件是非本地(外星)的,并且对其调用了winld(),则该小部件将获得一个本地句柄。//这个值可能在运行时改变。//类型为 QEvent:WinldChange 的事件将在窗口系统标识符改变后发送给该小部件。void createWinId(); // internal, going away 无注释,似乎是 QT 的内部函数//private: QWidgetData * data; //本类的数据成员inline WId internalWinId() const //class QWidgetData:: WId winid;{ return data->winid; }//Returns the effective window system identifier of the widget,//i.e. the native parent's window system identifier.//If the widget is native, this function returns the native widget ID.//Otherwise, the window ID of the first native parent widget, i.e.,//the top-level widget that contains this widget, is returned.//Note: We recommend that you do not store this value as//it is likely to change at run-time.WId effectiveWinId() const;//class Q_WIDGETS_EXPORT QStyle : public QObject 这是有 800多行的大类,略QStyle * style() const; // GUI style settingvoid setStyle(QStyle *); // Widget types and states//警告:此函数 setStyle 特别适用于演示目的,您希望展示Ot的样式功能。//实际应用程序应避免使用此函数而应使用一致的GUI样式。//警告:目前不支持自定义 OStyle 子类使用 Ot 样式表。我们计划在未来的某个版本中解决这个问题。#if QT_DEPRECATED_SINCE(6, 1) //被报废的函数QT_DEPRECATED_VERSION_X_6_1("Use isWindow()")bool isTopLevel() const;
#endif//如果小部件是一个独立的窗口,则返回true,否则返回false。//窗口是一个小部件,它不是任何其他小部件的视觉子项,通常具有边框和窗口标题。//-个窗口可以有一个父容器。然后它会与其父容器分组,//当父容器被删除时会被删除,当父容器被最小化时会被最小化等。//如果窗口管理器支持,它还会与其父容器有一个共同的任务栏条目。//QDialog和QMainWindow小部件默认是窗口,即使构造函数中指定了父小部件。//这种行为由Qt:Window标志指定。bool isWindow() const { return (windowType() & Qt::Window); }//This property Modal holds whether the widget is a modal widget。//This property only makes sense for windows.//A modal widget prevents widgets in all other windows from getting any input.//By default, this property is false.bool isModal() const { return data->window_modality != Qt::NonModal; }//Q_PROPERTY(bool modal READ isModal)属性的读函数Qt::WindowModality windowModality() const; //属性的读写函数void setWindowModality(Qt::WindowModality windowModality);//Q_PROPERTY(Qt::WindowModality windowModality//READ windowModality WRITE setWindowModality)bool isEnabledTo(const QWidget *ancestor) const;bool isEnabled() const { return !testAttribute(Qt::WA_Disabled); }//Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled)public Q_SLOTS:void setEnabled(bool); //属性的写函数//Disables widget input events if disable is true;//otherwise enables input events.void setDisabled(bool disable);//Q_PROPERTY(bool windowModified READ isWindowModified WRITE setWindowModified)void setWindowModified(bool); //形参可变public: // Widget coordinates 本段全是属性的读写函数bool isWindowModified() const;//Q_PROPERTY(QRect frameGeometry READ frameGeometry)QRect frameGeometry() const; //属性的读函数//Q_PROPERTY(QRect geometry READ geometry WRITE setGeometry) //属性的读函数const QRect & geometry() const { return data->crect; }void setGeometry(const QRect &);inline void setGeometry(int x, int y, int w, int h){ setGeometry(QRect(ax, ay, aw, ah)); }//Q_PROPERTY(QRect normalGeometry READ normalGeometry)QRect normalGeometry() const;//属性的读函数int x() const; //Q_PROPERTY(int x READ x)int y() const; //Q_PROPERTY(int y READ y)inline int width () const { return data->crect.width (); }//Q_PROPERTY(int width READ width )//Q_PROPERTY(int height READ height)inline int height () const { return data->crect.height(); }inline QRect rect () const //Q_PROPERTY(QRect rect READ rect){ return QRect(0, 0, data->crect.width(), data->crect.height()); }//class QRect { int x1; int y1; int x2; int y2; } 矩形把握对角两个点//class QSize { int wd; int ht;} 宽与高QRect childrenRect () const;//Q_PROPERTY(QRect childrenRect READ childrenRect)//Q_PROPERTY(QPoint pos READ pos WRITE move DESIGNABLE false STORED false)QPoint pos () const;void move(const QPoint &);void move(int x, int y ){ move(QPoint(ax, ay)); }QRegion childrenRegion() const;//Q_PROPERTY(QRegion childrenRegion READ childrenRegion)//Q_PROPERTY(QSize minimumSize READ minimumSize WRITE setMinimumSize)QSize minimumSize() const;void setMinimumSize(int minw, int minh);void setMinimumSize(const QSize &){ setMinimumSize(s.width(),s.height()); }//Q_PROPERTY(int minimumWidth READ minimumWidth WRITE setMinimumWidth ...)int minimumWidth () const { return minimumSize().width (); }void setMinimumWidth(int minw); //QSize minimumSize() const; 本类成员函数//Q_PROPERTY(int minimumHeight READ minimumHeight WRITE setMinimumHeight ...)int minimumHeight() const { return minimumSize().height(); }void setMinimumHeight(int minh);//Q_PROPERTY(QSize maximumSize READ maximumSize WRITE setMaximumSize)QSize maximumSize() const;void setMaximumSize(int maxw, int maxh);void setMaximumSize(const QSize &){ setMaximumSize(s.width(),s.height()); }//Q_PROPERTY(int maximumWidth READ maximumWidth WRITE setMaximumWidth ...)int maximumWidth () const { return maximumSize().width(); }void setMaximumWidth(int maxw); //QSize maximumSize() const; 本类成员函数//Q_PROPERTY(int maximumHeight READ maximumHeight WRITE setMaximumHeight ...)int maximumHeight() const;void setMaximumHeight(int maxh);//Q_PROPERTY(QSize size READ size WRITE resize ...)QSize size () const { return data->crect.size(); }void resize (const QSize &); //写函数构成重载void resize (int w, int h){ resize (QSize(w, h)); }QSize frameSize () const; //Q_PROPERTY(QSize frameSize READ frameSize)#ifdef Q_QDOCvoid setupUi(QWidget *widget);
#endif//Q_PROPERTY(QSize sizeIncrement READ sizeIncrement WRITE setSizeIncrement)QSize sizeIncrement() const;void setSizeIncrement(int w, int h );void setSizeIncrement(const QSize &){ setSizeIncrement(s.width(),s.height()); }//Q_PROPERTY(QSize baseSize READ baseSize WRITE setBaseSize)QSize baseSize() const;void setBaseSize(int basew, int baseh);void setBaseSize(const QSize &){ setBaseSize(s.width(),s.height()); }//将小部件的最小和最大大小都设置为s,从而防止它增长或缩小。//这将覆盖 QLayout 设定的默认大小限制。要解除限制,请将大小设置为OWIDGETSIZE MAX。//或者,如果您希望小部件基于其内容具有固定大小,//则可以调用 OLayout::setSizeConstraint(OLayout::SetFixedSize);void setFixedSize(const QSize & s);void setFixedSize(int w, int h );//Sets the width of the widget to w and the height to h.//将小部件的最小和最大宽度设置为w,而不改变高度。为方便起见提供。void setFixedWidth (int w);void setFixedHeight(int h);// Widget coordinate mapping//将小部件坐标pos转换为全局屏幕坐标。//例如,mapToGlobal(QPointF(0,0)) 将给出该小部件左上角的像素的全球坐标。QPointF mapToGlobal(const QPointF & pos) const;QPoint mapToGlobal(const QPoint & ) const;//class QPointF { qreal xp; qreal yp; } //浮点坐标//class QPoint { int xp; int yp; } //整数坐标//Translates the widget coordinate pos to a coordinate in the parent widget.QPointF mapToParent(const QPointF & pos) const;QPoint mapToParent(const QPoint & ) const;//Translates the widget coordinate pos to the coordinate system of parent.//The parent must not be nullptr and must be a parent of the calling widget.QPointF mapTo(const QWidget * parent, const QPointF & pos) const;QPoint mapTo(const QWidget *, const QPoint &) const;//Translates the global screen coordinate pos to widget coordinates.QPointF mapFromGlobal(const QPointF & pos) const;QPoint mapFromGlobal(const QPoint & ) const;//Translates the parent widget coordinate pos to widget coordinates.//Same as mapFromGlobal() if the widget has no parent.QPointF mapFromParent(const QPointF & pos) const;QPoint mapFromParent(const QPoint & ) const;//Translates the widget coordinate pos from the coordinate system of//parent to this widget's coordinate system.//The parent must not be nullptr and must be a parent of the calling widget.QPointF mapFrom(const QWidget * parent, const QPointF & pos) const;QPoint mapFrom(const QWidget * , const QPoint & ) const;//Returns the window for this widget, i.e. the next ancestor widget that//has (or could have) a window-system frame.//If the widget is a window, the widget itself is returned.//Typical usage is changing the window title:// aWidget->window()->setWindowTitle("New Window Title");QWidget * window() const;inline QWidget * topLevelWidget() const //This function is deprecated.{ return window(); } //Use window() instead.//返回此小部件的本地父小部件,即具有系统标识符的下一个祖先小部件,//如果没有任何本地父小部件则为 nullptr。QWidget * nativeParentWidget() const;// Widget appearance functions //又开始处理属性的读写函数了const QPalette & palette() const;void setPalette(const QPalette &);// Q_PROPERTY(QPalette palette READ palette WRITE setPalette)//返回小部件的背景角色。 这个不是属性函数//背景角色定义从小部件调色板中使用的刷子,用于渲染背景。//如果没有设置显式的背景角色,则小部件会继承其父级小部件的背景角色。QPalette::ColorRole backgroundRole() const;void setBackgroundRole(QPalette::ColorRole role);//将小部件的背景角色设置为 role。//背景角色定义从小部件调色板中使用的刷子,用于渲染背景。//如果角色是OPalette::NoRole,则该小部件继承其父类的背景角色。//请注意,样式可以自由选择调色板中的任何颜色。//如果您使用setBackgroundRole()无法获得所需结果,则可以修改调色板或设置样式表。//返回前景角色。//前景角色定义了用于绘制前景的来自小部件调色板的颜色。//如果没有设置显式的前景角色,该函数将返回与背景角色形成对比的角色。QPalette::ColorRole foregroundRole() const;void setForegroundRole(QPalette::ColorRole role);//将小部件的前景色角色设置为 role。//前景角色定义了用于绘制前景的来自小部件调色板的颜色。//如果角色是OPalette::NoRole,则小部件使用与背景角色形成对比的前景角色。//请注意,样式可以自由选择调色板中的任何颜色。//如果您使用setForegroundRole()无法获得所需的结果,则可以修改调色板或设置样式表。//Q_PROPERTY(QFont font READ font WRITE setFont)const QFont & font() const { return data->fnt; }void setFont(const QFont &);//Returns the font metrics字体度量 for the widget's current font.//Equivalent to QFontMetrics(widget->font()).QFontMetrics fontMetrics() const { return QFontMetrics(data->fnt); }//Returns the font info for the widget's current font.//Equivalent to QFontInfo(widget->font()).QFontInfo fontInfo() const { return QFontInfo(data->fnt); }#ifndef QT_NO_CURSOR //要使用光标的//Q_PROPERTY(QCursor cursor READ cursor WRITE setCursor RESET unsetCursor)QCursor cursor() const;void setCursor(const QCursor &);void unsetCursor(); //重置光标
#endif//如果小部件位于鼠标光标下方,则返回true;否则返回false。//This value is not updated properly during drag and drop operations.bool underMouse() const { return testAttribute(Qt::WA_UnderMouse) ; }//Q_PROPERTY(bool mouseTracking READ hasMouseTracking WRITE setMouseTracking)bool hasMouseTracking() const { return testAttribute(Qt::WA_MouseTracking); }//属性void setMouseTracking(bool enable) { setAttribute(Qt::WA_MouseTracking, enable); }bool hasTabletTracking() const{ return testAttribute(Qt::WA_TabletTracking); }void setTabletTracking(bool enable){ setAttribute(Qt::WA_TabletTracking, enable);}//Q_PROPERTY(bool tabletTracking 这似乎是平板电脑与触摸笔之间的跟踪// READ hasTabletTracking WRITE setTabletTracking)//Returns the mask currently set on a widget.//If no mask is set the return value will be an empty region.QRegion mask() const; //没有此属性//Causes only the pixels of the widget for which bitmap has a//corresponding 1 bit to be visible.//If the region includes pixels outside the rect() of the widget,//window system controls in that area may or may not be visible,//depending on the platform.//Note that this effect can be slow if the region is particularly complex.//Masked widgets receive mouse events only on their visible portions.void setMask(const QBitmap & bitmap);//Causes only the parts of the widget which overlap region to be visible.//If the region includes pixels outside the rect() of the widget,//window system controls in that area may or may not be visible,//depending on the platform.void setMask(const QRegion & region);void clearMask(); //Removes any mask set by setMask().//Renders the sourceRegion of this widget into the target using renderFlags//to determine how to render. Rendering starts at targetOffset in the target.//If sourceRegion is a null region, this function will use QWidget::rect()//as the region, i.e. the entire widget.//Ensure that you call QPainter::end() for//the target device's active painter (if any) before rendering.void render(QPaintDevice * target,const QPoint & targetOffset = QPoint (),const QRegion & sourceRegion = QRegion(),RenderFlags renderFlags = RenderFlags(DrawWindowBackground | DrawChildren));//This is an overloaded function.//Renders the widget into the painter's QPainter::device().
//Transformations and settings applied to the painter will be used when rendering.void render(QPainter * painter,const QPoint & targetOffset = QPoint(),const QRegion & sourceRegion = QRegion(),RenderFlags renderFlags = RenderFlags(DrawWindowBackground | DrawChildren));//将小部件渲染为受给定矩形限制的位图。如果小部件有任何子元素,则它们也将在适当的位置绘制。//如果指定了无效大小的矩形(默认为这种情况),则整个小部件将被绘制。//注:此函数可以通过元对象系统和 QML调用。参见QINVOKABLE。Q_INVOKABLE QPixmap //看来此函数也是关于绘图渲染的grab(const QRect &rectangle = QRect( QPoint(0, 0), QSize(-1, -1) ) );#if QT_CONFIG(graphicseffect) //有此定义的。但这不是属性//The graphicsEffect function returns a pointer to the widget's graphics effect.//If the widget has no graphics effect, nullptr is returned.QGraphicsEffect * graphicsEffect() const;void setGraphicsEffect(QGraphicsEffect *effect);//The setGraphicsEffect function is for setting the widget's graphics effect.//Sets effect as the widget's effect. If there already is an effect//installed on this widget, QWidget will delete the existing effect before//installing the new effect.//If effect is the installed effect on a different widget,//setGraphicsEffect() will remove the effect from the widget and//install it on this widget.//QWidget takes ownership of effect.//Note: This function will apply the effect on itself and all its children.//Note: Graphics effects are not supported for OpenGL-based widgets,//such as QGLWidget, QOpenGLWidget and QQuickWidget.#endif // QT_CONFIG(graphicseffect)#ifndef QT_NO_GESTURES //经测试,确实没定义这个宏//Subscribes订阅 the widget to a given gesture with specific flags.void grabGesture(Qt::GestureType type , //这是关于手势点击平移滑动的枚举量Qt::GestureFlags flags = Qt::GestureFlags());//关于父子容器上手势传递的枚举量 GestureFlagsvoid ungrabGesture(Qt::GestureType type);//Unsubscribes the widget from a given gesture type
#endifQString windowTitle() const;public Q_SLOTS: //信号的槽函数,但也是属性 windowTitle的写函数void setWindowTitle(const QString &);//Q_PROPERTY(QString windowTitle READ windowTitle//WRITE setWindowTitle NOTIFY windowTitleChanged)#ifndef QT_NO_STYLE_STYLESHEET //信号的槽函数,但也是属性 windowTitle的写函数void setStyleSheet(const QString& styleSheet);//Q_PROPERTY(QString styleSheet READ styleSheet WRITE setStyleSheet)
#endif //至此,信号的槽函数定义结束public:
#ifndef QT_NO_STYLE_STYLESHEETQString styleSheet() const; //配合上面的属性 styleSheet 的读函数
#endif//Q_PROPERTY(QIcon windowIcon READ windowIcon WRITE setWindowIcon ...)QIcon windowIcon() const; //又是关于属性的void setWindowIcon(const QIcon &icon);//Q_PROPERTY(QString windowIconText ...) // deprecatedQString windowIconText() const;void setWindowIconText(const QString &);QString windowRole() const; //Returns the window's role, or an empty string.void setWindowRole(const QString &);//本属性似乎只对 win11 有用//Sets the window's role to role. This only makes sense for windows on X11.//Q_PROPERTY(QString windowFilePath READ windowFilePath WRITE setWindowFilePath)QString windowFilePath() const;void setWindowFilePath(const QString &filePath);//Q_PROPERTY(double windowOpacity READ windowOpacity WRITE setWindowOpacity)qreal windowOpacity() const;void setWindowOpacity(qreal level);#if QT_CONFIG(tooltip) //是有此宏的//Q_PROPERTY(QString toolTip READ toolTip WRITE setToolTip)QString toolTip() const;void setToolTip(const QString &);//Q_PROPERTY(int toolTipDuration READ toolTipDuration WRITE setToolTipDuration)int toolTipDuration() const;void setToolTipDuration(int msec);
#endif#if QT_CONFIG(statustip)//Q_PROPERTY(QString statusTip READ statusTip WRITE setStatusTip)QString statusTip() const;void setStatusTip(const QString &);
#endif#if QT_CONFIG(whatsthis)//Q_PROPERTY(QString whatsThis READ whatsThis WRITE setWhatsThis)QString whatsThis() const;void setWhatsThis(const QString &);
#endif#ifndef QT_NO_ACCESSIBILITY//Q_PROPERTY(QString accessibleName READ accessibleName WRITE setAccessibleName)QString accessibleName() const;void setAccessibleName(const QString &name);//Q_PROPERTY(QString accessibleDescription// READ accessibleDescription WRITE setAccessibleDescription)QString accessibleDescription() const;void setAccessibleDescription(const QString &description);
#endif//Q_PROPERTY(Qt::LayoutDirection layoutDirection// READ layoutDirection WRITE setLayoutDirection RESET unsetLayoutDirection)Qt::LayoutDirection layoutDirection() const;void setLayoutDirection(Qt::LayoutDirection direction);void unsetLayoutDirection();//Q_PROPERTY(QLocale locale READ locale WRITE setLocale RESET unsetLocale)QLocale locale() const;void setLocale(const QLocale &locale);void unsetLocale();inline bool isRightToLeft() const{ return layoutDirection() == Qt::RightToLeft; }inline bool isLeftToRight() const{ return layoutDirection() == Qt::LeftToRight; }public Q_SLOTS:inline void setFocus() { setFocus(Qt::OtherFocusReason); }public: //Q_PROPERTY(bool focus READ hasFocus) 说明这不是属性 focus的写函数void setFocus(Qt::FocusReason reason); //函数重载,无参版本作为槽函数bool hasFocus() const;void clearFocus(); //Takes keyboard input focus from the widget.//If the widget has active focus, a focus out event is sent to//this widget to tell it that it has lost the focus.//This widget must enable focus setting in order to get the keyboard input focus,//i.e. it must call setFocusPolicy().//Q_PROPERTY(bool isActiveWindow READ isActiveWindow)bool isActiveWindow() const;void activateWindow(); //将包含此小部件的顶级小部件设置为活动窗口。//活动窗口是具有键盘输入焦点的可见顶层窗口。//这个函数执行的操作与在顶层窗口的标题栏上单击鼠标相同。在X11上,结果取决于窗口管理器。//如果你想确保窗口也堆叠在顶部,你还应该调用raise()。注意,窗口必须是可见的,//否则activateWindow()没有效果。//在Windows上,如果在应用程序当前不是活动窗口时调用此函数,则不会使其成为活动窗口。//它将更改任务栏条目的颜色,以表示窗口在某种程度上发生了变化。//这是因为微软不允许应用程序中断用户当前在另一个应用程序中正在做的事情。//Q_PROPERTY(Qt::FocusPolicy focusPolicy READ focusPolicy WRITE setFocusPolicy)Qt::FocusPolicy focusPolicy() const;void setFocusPolicy(Qt::FocusPolicy policy);//Puts the second widget after the first widget in the focus order.//It effectively removes the second widget from its focus chain and//inserts it after the first widget.//If first or second has a focus proxy,//setTabOrder() correctly substitutes the proxy.//注意:自Qt5.10以来:具有子控件作为焦点代理的控件被视为复合控件。//在一或两个复合控件之间设置标签顺序时,每个内部的局部标签顺序将被保留。//这意味着如果两个控件都是复合控件,the resulting tab order will be from//the last child inside first, to the first child inside second.static void setTabOrder(QWidget * first, QWidget * second); //静态成员函数//Returns the focus proxy, or nullptr if there is no focus proxy.QWidget * focusProxy() const; //这不是属性void setFocusProxy(QWidget * w);//将小部件的焦点代理设置为小部件w。如果 w为nullptr,则该函数将重置此小部件,使其没有焦点代理。//-些小部件可以“获得焦点”,但创建子小部件,例如QLineEdit,以实际处理焦点。//在这种情况下,小部件可以将行编辑 line edit 设置为它的焦点代理。//setFocusProxy()设置当“此控件”获得焦点时实际获得焦点的控件。
//If there is a focus proxy, setFocus() and hasFocus() operate on the focus proxy.//If "this widget" is the focus widget,//then setFocusProxy() moves focus to the new focus proxy。//Q_PROPERTY(Qt::ContextMenuPolicy contextMenuPolicy// READ contextMenuPolicy WRITE setContextMenuPolicy)Qt::ContextMenuPolicy contextMenuPolicy() const;void setContextMenuPolicy(Qt::ContextMenuPolicy policy);//抓取鼠标输入。这个小部件会接收所有鼠标事件,直到调用releaseMouse()为止;//其他小部件根本不会接收到任何鼠标事件。键盘事件不受影响。//如果您想捕获键盘事件,请使用grabKeyboard()。
//Warning: Bugs in mouse-grabbing applications very often lock the terminal.
//Use this function with extreme caution,
//and consider using the -nograb command line option while debugging.
//在使用Qt时,几乎不需要抓取鼠标,因为Qt会明智地抓取和释放鼠标。
//特别是,当按下鼠标按钮时,Qt会抓取鼠标,并在最后一次释放按钮之前保持抓取状态。//注意:只有可见的widget才能抓取鼠标输入。//如果widget的isVisible()返回false,则该widget不能调用grabMouse()。void grabMouse(); // Grab functions
#ifndef QT_NO_CURSORvoid grabMouse(const QCursor & cursor);
//Grabs the mouse input and changes the cursor shape.
//The cursor will assume shape cursor (for as long as the mouse focus is grabbed) and
//this widget will be the only one to receive mouse events until
//releaseMouse() is called().
//Warning: Grabbing the mouse might lock the terminal.
#endifvoid releaseMouse(); //Releases the mouse grab.//抓取键盘输入。这个小部件接收所有键盘事件,直到调用releaseKeyboard ();//其他小部件根本不会接收到键盘事件。鼠标事件不受影响。如果您想捕获鼠标事件,请使用grabMouse()。//焦点小部件不受影响,只是它不会接收任何键盘事件。//setFocus()照常移动焦点,但新的焦点小部件只有在调用 releaseKeyboard()之后才会接收键盘事件。//如果当前有另一个小部件正在抓取键盘输入,则首先释放该小部件的抓取。//Grabs the keyboard input.//This widget receives all keyboard events until releaseKeyboard() is called;//other widgets get no keyboard events at all.//Mouse events are not affected. Use grabMouse() if you want to grab that.//The focus widget is not affected,//except that it doesn't receive any keyboard events.//setFocus() moves the focus as usual,but the new focus widget receives//keyboard events only after releaseKeyboard() is called.//If a different widget is currently grabbing keyboard input,//that widget's grab is released first.void grabKeyboard();void releaseKeyboard(); //Releases the keyboard grab.#ifndef QT_NO_SHORTCUT //快捷方式 Shortcut//为 Qt的快捷键系统添加一个快捷方式,该快捷方式在指定上下文 context中监视给定的键序列 key。//如果上下文是Qt:ApplicationShortcut,则快捷键适用于整个应用程序。//否则,它要么是此小部件的本地快捷键,Qt::WidgetShortcut,//要么是窗口本身的快捷键,Ot::WindowShortcut。//如果相同的键序列已被多个小部件捕获,当键序列发生时,//会向所有适用的小部件发送,QEvent::Shortcut事件,其顺序不确定,//但“ambiguous”标志设置为 true。//警告:你通常不需要使用这个函数;//相反,用你需要的快捷键序列创建QActions(如果你也想要相同的菜单选项和工具栏按钮),//或者如果你只需要键序列创建OShortcuts。OAction和OShortcut都为您处理所有事件过滤,//并提供在用户触发键序列时触发的信号,因此比这个低级功能更易于使用。int grabShortcut(const QKeySequence & key,Qt::ShortcutContext context = Qt::WindowShortcut);void releaseShortcut(int id);//从Qt的快捷键系统中移除具有给定ID的快捷方式。//该小部件将不再接收QEvent::Shortcut事件,用于快捷键的键序列//(除非它还有其他具有相同键序列的快捷键)。//警告:您通常不需要使用此函数,因为Qt的快捷方式系统在父控件被销毁时会自动删除快捷方式。//最好使用QAction或QShortcut来处理快捷方式,因为它们比此低级函数更易于使用。//还要注意,这是一个昂贵的操作。//如果 enable 为 true,则具有给定 id 的快捷键被启用;否则,快捷键被禁用。//警告:通常你不应该需要使用这个函数,//因为Qt的快捷键系统会自动启用/禁用快捷键,当小部件变为隐藏/可见并获得或失去焦点时。//最好使用QAction或QShortcut来处理快捷键,因为它们比这个低级函数更易于使用。void setShortcutEnabled (int id, bool enable = true);void setShortcutAutoRepeat(int id, bool enable = true);//如果 enable 为 true,则启用具有给定 id 的快捷键的自动重复;否则禁用。
#endif//返回当前正在抓取鼠标输入的控件。//如果此应用程序中当前没有小部件捕获鼠标,则返回 nullptr。static QWidget * mouseGrabber();static QWidget * keyboardGrabber();//返回当前正在抓取键盘输入的控件。//如果此应用程序中当前没有小部件抓取键盘,则返回`nullptr~。// Update/refresh functions//Q_PROPERTY(bool updatesEnabled// READ updatesEnabled WRITE setUpdatesEnabled DESIGNABLE false)inline bool updatesEnabled() const //指重新绘制界面,而非操作系统的版本更新{ return !testAttribute(Qt::WA_UpdatesDisabled); }void setUpdatesEnabled(bool enable);#if QT_CONFIG(graphicsview) //经测试,本宏是被定义的//返回图形视图中相应嵌入式小部件的代理小部件,否则返回nullptr。QGraphicsProxyWidget *graphicsProxyWidget() const;//Returns the proxy widget for the corresponding embedded widget//in a graphics view; otherwise returns nullptr.
#endifpublic Q_SLOTS: //又是信号的槽函数//更新小部件,除非已禁用更新或小部件已隐藏。//这个函数不会立即重新绘制;相反,它会在 Qt返回主事件循环时安排一个绘制事件进行处理。//这使得 Qt 能够比调用repaint()更快地优化和减少闪烁。//多次调用 update()通常只会导致-次 paintEvent()调用。//Qt通常在调用paintEvent()之前擦除小部件的区域。//如果设置了Qt::WA_OpaquePaintEvent小部件属性,则小部件负责用不透明颜色绘制其所有像素。 void update (); //update()函数永远不会导致递归。void repaint();//通过立即调用 paintEvent()直接重新绘制小部件,除非禁用更新或小部件被隐藏。//我们建议只有在需要立即重绘时(例如在动画期间)才使用repaint()方法。//在几乎所有情况下update()方法更好,因为它允许 Qt进行速度优化并最小化闪烁。//警告:如果在可能从paintEvent()调用的函数中调用repaint(),则可能会导致无限递归。public:void update(const QRegion&);void update(const QRect &);inline void update(int x, int y, int w, int h){ update(QRect(ax, ay, aw, ah)); }void repaint(int x, int y, int w, int h);void repaint(const QRect &);void repaint(const QRegion &);//Q_PROPERTY(bool visible READ isVisible WRITE setVisible ...)bool isVisible() const //属性的读写函数{ return testAttribute(Qt::WA_WState_Visible); }
public Q_SLOTS: // Widget management functions //又是信号的槽函数virtual void setVisible(bool visible);void showMinimized (); //Shows the widget minimized, as an icon.//Calling this function only affects windows.void showMaximized (); //Shows the widget maximized.void showFullScreen(); //Shows the widget in full-screen mode.void showNormal (); //这四个函数都不是属性宏里的函数,与宏无关//Restores the widget after it has been maximized or minimized.void show(); //Shows the widget and its child widgets.//This is equivalent to calling showFullScreen(), showMaximized(),//or setVisible(true), depending on the platform's default behavior//for the window flags.void hide();//Hides the widget. This function is equivalent to setVisible(false).//注:如果您正在使用QDialog或其子类,并且在此函数之后调用show()函数,//则对话框将在其原始位置显示。//关闭此小部件。如果小部件已关闭,则返回 true;否则返回 false。//首先,它向小部件发送QCloseEvent。如果小部件接受关闭事件,则隐藏小部件。//如果它忽略事件,则不会发生任何事情。QWidget::closeEvent()的默认实现接受关闭事件。//如果小部件具有 Qt::WA_DeleteOnClose 标志,则也会删除小部件。//无论小部件是否可见,都会向小部件发送关闭事件。//当具有 Qt:WA OuitOnClose属性的最后一个可见主窗口(即没有父窗口的窗口)被关闭时,//会发出QGuiApplication::lastWindowClosed()信号。//默认情况下,此属性设置给所有除临时窗口(如启动屏幕、工具窗口和弹出菜单)以外的窗口。bool close();void raise(); //将本小部件提升到父小部件堆栈的顶部。//在这次调用之后,该小部件将在任何重叠的兄弟小部件之前可见。//注:在使用activateWindow()时,可以调用此函数以确保窗口堆叠在顶部。void lower();//将小部件降低到父小部件堆栈的底部。//在这次调用之后,该小部件将在任何重叠的兄弟小部件的后面(因此被遮挡)void setHidden(bool hidden); //Convenience function,
public: //equivalent to setVisible(!hidden).inline bool isHidden() const { return testAttribute(Qt::WA_WState_Hidden); }//如果小部件被隐藏,则返回true,否则返回false。//隐藏的部件只有在调用其show()方法时才会显示。当父部件显示时,它不会自动显示。//要检查可见性,请使用!isVisible()代替(注意感叹号)。//isHidden()意味着!isVisible(),// but a widget can be not visible and not hidden at the same time.//这是小部件作为不可见小部件的子小部件的情况。This is the case for widgets that//are children of widgets that are not visible。//如果满足以下条件,则隐藏小部件://它们被创建为独立的窗口;它们被创建为可见小部件的孩子;hide()或setVisible(false)被调用。//将小部件放置在父小部件堆栈中的w下方。为了使这个工作,小部件本身和w必须是兄弟。void stackUnder(QWidget * w);//Saves the current geometry and state for top-level widgets.QByteArray saveGeometry() const;bool restoreGeometry(const QByteArray &geometry);//Restores the geometry and state of top-level widgets stored in the//byte array geometry. Returns true on success; otherwise returns false.//如果恢复的几何图形超出屏幕范围,将对其进行修改,使其位于可用屏幕几何图形内。void adjustSize();//调整小部件的大小以适合其内容。//如果大小提示有效,即大小提示的宽度和高度>=0,则此函数使用 sizeHint()。//否则,它将大小设置为覆盖所有子控件(所有子控件矩形的并集)的子控件矩形。//对于窗口,屏幕大小也会被考虑在内。如果sizeHint()小于(200,100)且大小策略为扩展,//则窗口至少为(200,100)。窗口的最大大小是屏幕宽度和高度的 2/3。bool isVisibleTo(const QWidget * ancestor) const;//Returns true if this widget would become visible if ancestor is shown;//otherwise returns false.//The true case occurs if neither the widget itself nor//any parent up to but excluding ancestor形参 has been explicitly hidden.//如果该小部件被屏幕上的其他窗口遮挡,此函数仍将返回 true,//但如果它或它们被移动,则可能在物理上可见。isVisibleTo(0)与isVisible()相同。bool isMinimized ()const; //Q_PROPERTY(bool minimized READ isMinimized )bool isMaximized ()const; //Q_PROPERTY(bool maximized READ isMaximized )bool isFullScreen()const; //Q_PROPERTY(bool fullScreen READ isFullScreen)//Returns the current window state. The window state is a OR'ed combination of//Qt::WindowState: Qt::WindowMinimized,//Qt::WindowMaximized, Qt::WindowFullScreen, and Qt::WindowActive.Qt::WindowStates windowState() const; //这不是属性void overrideWindowState(Qt::WindowStates state); //无官方注释void setWindowState(Qt::WindowStates state);//Sets the window state to state.//对如果窗口不可见(即isVisible()返回 false)则在调用 show()时窗口状态将生效。//于可见的窗口,更改是即时的。//调用此函数将隐藏小部件。您必须调用show()以使小部件再次可见。//Q_PROPERTY(QSize sizeHint READ sizeHint)virtual QSize sizeHint() const;virtual QSize minimumSizeHint() const;//Q_PROPERTY(QSize minimumSizeHint READ minimumSizeHint)//Q_PROPERTY(QSizePolicy sizePolicy READ sizePolicy WRITE setSizePolicy)QSizePolicy sizePolicy() const;void setSizePolicy(QSizePolicy);inline void setSizePolicy(QSizePolicy::Policy horizontal,QSizePolicy::Policy vertical ){ setSizePolicy(QSizePolicy(hor, ver)); }//返回给定宽度w时此小部件的首选高度。//如果此小部件具有布局,则默认实现返回布局的首选高度。//如果没有布局,则默认实现返回-1,表示首选高度不依赖于宽度。virtual int heightForWidth(int w) const; //这不是属性virtual bool hasHeightForWidth( ) const;//Returns true if the widget's preferred height depends on its width;//otherwise returns false.//返回油漆事件可以发生的未遮挡区域。//对于可见小部件,这是其他小部件未覆盖区域的近似值;否则,这是一个空区域//如果需要,repaint()函数会调用此函数,因此通常不需要调用它。QRegion visibleRegion() const;//The contentsMargins function returns the widget's contents margins.QMargins contentsMargins() const; //非属性void setContentsMargins(int left, int top, int right, int bottom);void setContentsMargins(const QMargins &margins);//Sets the margins around the contents of the widget to have the//sizes left, top, right, and bottom.//The margins are used by the layout system,//and may be used by subclasses to specify the area to draw in//(e.g. excluding the frame).//Changing the margins will trigger a resizeEvent().QRect contentsRect() const; //Returns the area inside the widget's margins.public://Returns the layout manager that is installed on this widget,//or nullptr if no layout manager is installed.//The layout manager sets the geometry of the widget's children that//have been added to the layout.QLayout * layout() const; //这不是属性void setLayout(QLayout * layout);//将此小部件的布局管理器设置为 layout。//如果此小部件上已经安装了布局管理器,则QWidget将不允许您安装另一个布局管理器。//您必须首先删除现有布局管理器(由layout()返回),然后才能使用新布局调用setLayout()。//如果 layout是另一个小部件的布局管理器,//则setLayout()将重新设置布局,使其成为此小部件的布局管理器。void updateGeometry();//Notifies the layout system that this widget has changed and//may need to change geometry.//Call this function if the sizeHint() or sizePolicy() have changed.//For explicitly hidden widgets, updateGeometry() is a no-op.//The layout system will be notified as soon as the widget is shown.//将小部件的父级设置为 parent,并重置窗口标志。小部件被移动到新父级中的位置(0,0)//如果新父项小部件位于不同的窗口中,//则重新关联的小部件及其子项将按与之前相同的内部顺序附加到新父项小部件的标签链的末尾。//如果移动的小部件之一具有键盘焦点,则setParent()调用clearFocus()以清除该小部件的焦点。//如果新父项小部件与旧父项位于同一窗口中,则设置父项不会更改选项卡顺序或键盘焦点。//如果“新”父项小部件是旧的父项小部件,则此函数不起作用。//注意:作为更改父项的一部分,即使之前可见,小部件也会变得不可见。//必须调用show()以使小部件再次可见。//警告:您几乎不可能需要这个函数。//如果您有一个动态更改其内容的部件,则使用QStackedWidget要容易得多。void setParent(QWidget *parent);void setParent(QWidget *parent, Qt::WindowFlags f);//Returns the parent of this widget,//or nullptr if it does not have any parent widget.QWidget * parentWidget() const{ return static_cast<QWidget *>(QObject::parent()); }//将该小部件及其子小部件向右平移dx像素,向下平移dy像素。dx和dy都可以是负数。//滚动后,小部件将接收需要重新绘制的区域的绘制事件。//对于 Qt知道是不透明的的小部件,这仅是新暴露的部分。//例如,如果一个不透明的小部件向左滚动8像素,那么只有右边缘的8像素宽的条纹需要更新。//由于默认情况下小部件会传播其父项的内容,因此您需要设置autoFillBackground 属性,//或使用setAttribute()设置 Ot::WA OpaquePaintEvent属性,以使小部件不透明。//对于使用内容传播的小部件,滚动会导致整个滚动区域的更新。void scroll(int dx, int dy);void scroll(int dx, int dy, const QRect& r);//QRect { int x1; int y1; int x2; int y2; }//This is an overloaded function.//This version only scrolls r and does not move the children of the widget.//If r is empty or invalid, the result is undefined.// Misc. functions//Returns the last child of this widget that setFocus had been called on.//For top level widgets this is the widget that will get focus in case//this window gets activated.//This is not the same as QApplication::focusWidget(),//which returns the focus widget in the currently active window.QWidget * focusWidget () const;//Returns the next widget in this widget's focus chain.QWidget * nextInFocusChain () const;QWidget * previousInFocusChain () const;//returns the previous widget in this widget's focus chain.// drag and drop//Q_PROPERTY(bool acceptDrops READ acceptDrops WRITE setAcceptDrops)bool acceptDrops() const;void setAcceptDrops(bool on);#ifndef QT_NO_ACTION//actions//Returns the (possibly empty) list of this widget's actions.QList<QAction*> actions() const;//将操作 action 附加到此小部件的操作列表中。//所有QWidgets都有一个QActions列表,但它们可以用许多不同的方式图形化表示。//QAction列表(由actions()返回)的默认用法是创建一个上下文QMenu。//QWidget 应该只包含每个动作的一个实例,//添加一个它已经有的动作不会导致同一个动作在 widget 中出现两次。// action的所有权不会转移到此 QWidget。void addAction (QAction * action);void addActions(const QList<QAction*> &actions);void insertAction (QAction *before, QAction * action);void insertActions(QAction *before, const QList<QAction*> & actions);void removeAction (QAction *action);
#endif//QDOC_PROPERTY(Qt::WindowFlags windowFlags READ windowFlags WRITE setWindowFlags)inline Qt::WindowFlags windowFlags() const;void setWindowFlags(Qt::WindowFlags type);//Sets the window flag flag on this widget if on is true;//otherwise clears the flag.void setWindowFlag (Qt::WindowType, bool on = true);void overrideWindowFlags(Qt::WindowFlags type);//WindowFlags = QFlags<WindowType>//Q_DECLARE_FLAGS(WindowFlags, WindowType)//Sets the window flags for the widget to flags,//without telling the window system.//Warning: Do not call this function unless you really know what you're doing.//Returns the window type of this widget.//This is identical to windowFlags() & Qt::WindowType_Mask.inline Qt::WindowType windowType() const //WindowFlags = QFlags<WindowType>{ return static_cast<Qt::WindowType>(int(data->window_flags & Qt::WindowType_Mask));}//返回具有窗口标识符/句柄ID的widget的指针。//窗口标识符的类型取决于底层窗口系统,实际定义请参见qwindowdefs.h。//如果没有具有此标识符的小部件则返回nullptr。static QWidget * find (WId);//Returns the visible child widget at the position (x, y)//in the widget's coordinate system.//If there is no visible child widget at the specified position,//the function returns nullptr.QWidget * childAt(const QPoint &p) const;inline QWidget * childAt(int x, int y) const{ return childAt(QPoint(ax, ay)); }//Sets the attribute attribute on this widget if on is true;//otherwise clears the attribute.void setAttribute(Qt::WidgetAttribute attribute, bool on = true);inline bool testAttribute(Qt::WidgetAttribute) const{ if (attribute < int(8*sizeof(uint)))return data->widget_attributes & (1<<attribute);return testAttribute_helper(attribute);}//Returns true if attribute attribute is set on this widget;//otherwise returns false.//重新实现:QPaintDevice::paintEngine()返回。 返回小部件的绘制引擎。//请注意,用户不应显式调用此函数,因为它仅用于重新实现目的。该函数由Qt内部调用,//默认实现可能并不总是返回有效的指针。QPaintEngine *paintEngine() const override;//class QWidget : public QObject, public QPaintDevicevoid ensurePolished() const; //确保抛光; Polish 改进;磨炼;提高;//确保小部件及其子项已被 QStyle 润色(即具有适当的字体和调色板)//QWidget 在完全构建但首次显示之前调用此函数。//如果您希望在执行操作之前确保小部件已润色,则可以调用此函数,//例如,小部件的sizeHint()重实现可能需要正确的字体大小。//请注意,此函数是从sizeHint()的默认实现中调用的。//抛光对于必须在所有构造函数(从基础类以及从子类)调用之后进行的最终初始化非常有用。//如果在小部件打磨时需要更改一些设置,请重新实现 event()并处理 QEvent::Polish 事件类型。//注:该函数被声明为const,以便可以从其他const函数(例如sizeHint())中调用。//Returns true if this widget is a parent,//(or grandparent and so on to any level), of the given child,//and both widgets are within the same window; otherwise returns false.bool isAncestorOf(const QWidget *child) const;#ifdef QT_KEYPAD_NAVIGATION //经测试没有定义这个宏呢bool hasEditFocus() const;void setEditFocus(bool on);
#endif//Q_PROPERTY(bool autoFillBackground// READ autoFillBackground WRITE setAutoFillBackground)bool autoFillBackground() const;void setAutoFillBackground(bool enabled);//Returns the QBackingStore this widget will be drawn into.QBackingStore * backingStore() const;//If this is a native widget, return the associated QWindow.//Otherwise return null.//Native widgets include toplevel widgets, QGLWidget, and//child widgets on which winId() was called.QWindow * windowHandle() const;//Returns the screen the widget is on.QScreen * screen() const; //这不是属性void setScreen(QScreen *);//Sets the screen on which the widget should be shown to screen.//Setting the screen only makes sense for windows.//If necessary, the widget's window will get recreated on screen.//Note: If the screen is part of a virtual desktop of multiple screens,//the window will not move automatically to screen.//To place the window relative to the screen,//use the screen's topLeft() position.static QWidget *createWindowContainer(QWindow * window,QWidget * parent = nullptr,Qt::WindowFlags flags = Qt::WindowFlags() );//创建一个 QWidget,使其能够将 window 嵌入基于 QWidget 的应用程序中。//窗口容器作为 parent 的子容器创建,并带有窗口标志 flags。//-旦窗口被嵌入容器,容器将控制窗口的几何形状和可见性。//不建议在嵌入窗口上显式调用QWindow::setGeometry()、QWindow:://show()或QWindow::hide()函数。//容器接管 window 的所有权。可以通过调用 QWindow::setParent()将窗口从窗口容器中移除。//窗口容器作为它所属的顶层窗口的本地子窗口被附加。//当一个窗口容器被用作QAbstractScrollArea或OMdiArea的子窗口时,//它会为其父级链中的每个小部件创建本地窗口,以便在这种使用情况下进行适当的堆叠和剪切。//为窗口容器创建本地窗口也允许进行适当的堆叠和剪切。这必须在显示窗口容器之前完成。//具有许多本地子窗口的应用程序可能会遇到性能问题。//窗口容器存在一些已知的限制://堆叠顺序:嵌入式窗口将作为不透明的盒子堆叠在widget层次结构之上。// 多个重叠窗口容器实例的堆叠顺序未定义。//渲染集成:窗口容器与QGraphicsProxyWidget、QWidget:render()或类似功能不兼容。//焦点处理:可以让窗口容器实例具有任何焦点策略,并通过调用 QWindow:requestActivate()//将焦点委派给窗口。但是,从 QWindow 实例返回到正常的焦点链将由 QWindow 实例实现本身决定。//例如,当进入具有标签焦点的基于 Qt Quick 的窗口时,//再次按下标签很可能只会循环在 QML应用程序内部。//此外,QWindow::requestActivate()实际上是否给予窗口焦点,取决于平台。//在基于QWidget的应用程序中使用多个窗口容器实例会极大地损害应用程序的整体性能。Q_SIGNALS: //信号函数void windowTitleChanged(const QString &title);void windowIconChanged (const QIcon &icon);void windowIconTextChanged(const QString &iconText);void customContextMenuRequested(const QPoint &pos);//当小部件的 contextMenuPolicy设置为Ot::CustomContextMenu,//并且用户请求在小部件上显示上下文菜单时,会发出此信号。
//pos是小部件接收到的上下文菜单事件的坐标位置。通常,这是在小部件坐标中。该规则的一个例外
//是OAbstractScrollArea及其子类,它们将上下文菜单事件映射到视口 viewport()坐标的位置。protected:// Event handlersbool event(QEvent *event) override;//重新实现: QObject::event(OEvent *e) 这是主要的事件处理程序;它处理事件event。//您可以在子类中重新实现此函数,但建议使用专门的事件处理程序之-//按键和释放事件的处理方式与其他事件不同。event()检查Tab和 Shift+Tab,并尝试适当地移动焦点。//如果没有小部件可以移动焦点(或者按键不是Tab或Shift+Tab),event()会调用keyPressEvent()。//鼠标和触控板事件的处理也稍微特殊:只有当小部件被启用时,//event()才会调用专门的处理器如 mousePressEvent();否则,它将丢弃事件。//如果事件被识别,此函数返回true,否则返回false。//如果被识别的事件被接受(参见QEvent::accepted),则//任何进一步的处理,如事件传播到父控件,都会停止。virtual void mousePressEvent (QMouseEvent *event);virtual void mouseReleaseEvent (QMouseEvent *event);virtual void mouseDoubleClickEvent(QMouseEvent *event);virtual void mouseMoveEvent (QMouseEvent *event);#if QT_CONFIG(wheelevent) //是有这个宏定义的virtual void wheelEvent(QWheelEvent *event);
#endifvirtual void keyPressEvent (QKeyEvent *event);virtual void keyReleaseEvent(QKeyEvent *event);virtual void focusInEvent (QFocusEvent *event);virtual void focusOutEvent(QFocusEvent *event);virtual void enterEvent(QEnterEvent *event);virtual void leaveEvent(QEvent *event);virtual void paintEvent (QPaintEvent *event);virtual void resizeEvent(QResizeEvent *event);virtual void moveEvent (QMoveEvent *event);virtual void closeEvent (QCloseEvent *event);#ifndef QT_NO_CONTEXTMENUvirtual void contextMenuEvent(QContextMenuEvent *event);
#endif#if QT_CONFIG(tabletevent) //有这个宏定义的virtual void tabletEvent(QTabletEvent *event);
#endif#ifndef QT_NO_ACTION //此函数存在的virtual void actionEvent(QActionEvent *event);
#endif#if QT_CONFIG(draganddrop) //是有此定义的 drag_and_drop//当拖动正在进行且鼠标进入此小部件时,将调用此事件处理程序。// The event is passed in the event parameter.//如果事件被忽略,则小部件将不会接收到任何拖移事件。virtual void dragEnterEvent(QDragEnterEvent *event);virtual void dragMoveEvent (QDragMoveEvent *event);//如果正在执行拖放操作,并且满足以下条件之一时,将调用此事件处理程序:光标进入此小部件、//光标在此小部件内移动或在此小部件获得焦点时按下键盘上的修改键。事件通过事件参数传递。// The event is passed in the event parameter.virtual void dragLeaveEvent(QDragLeaveEvent *event);//当拖动正在进行且鼠标离开此小部件时,将调用此事件处理程序。virtual void dropEvent (QDropEvent *event);//This event handler is called when the drag is dropped on this widget.#endif//这个事件处理程序可以在子类中重新实现,以接收在 event参数中传递的 widget显示事件。//非自发的显示事件在显示之前立即发送给小部件。窗口的自发显示事件随后交付。//注:当窗口系统的映射状态发生变化时,小部件会接收到自发的显示和隐藏事件,例如,//当用户最小化窗口时接收到自发的隐藏事件,当窗口恢复时接收到自发的显示事件。//在接收到自发的隐藏事件之后,小部件在isVisible()的意义上仍然被认为是可见的。virtual void showEvent(QShowEvent *event);virtual void hideEvent(QHideEvent *event);//这个事件处理程序可以在子类中重新实现,以接收widget隐藏事件。事件在 event参数中传递。//隐藏事件在它们被隐藏后立即发送给小部件。//This special event handler can be reimplemented in a subclass to//receive native platform events identified by//eventType which are passed in the message parameter.//In your reimplementation of this function,//if you want to stop the event being handled by Qt,//return true and set result. The result parameter has meaning only on Windows.//If you return false, this native event is passed back to Qt,//which translates the event into a Qt event and sends it to the widget.virtual bool nativeEvent( const QByteArray &eventType,void *message, qintptr *result);//Note: Events are only delivered to this event handler//if the widget has a native window handle.//Note: This function superseedes取代 the event filter functions x11Event(),//winEvent() and macEvent() of Qt 4.// Misc. protected functionsvirtual void changeEvent(QEvent * event);//This event handler can be reimplemented to handle state changes.//The state being changed in this event can be retrieved 检索//through the event supplied.//重新实现: QPaintDevice::metric(QPaintDevice::PaintDeviceMetric metric) const。//内部实现虚拟 QPaintDevice::metric()函数。 m是获取的度量。int metric(PaintDeviceMetric) const override;//初始化 painter 笔、背景和字体与给定的小部件相同。//当在QWidget上打开绘图器时,会自动调用此函数。void initPainter(QPainter * painter) const override;QPaintDevice *redirected(QPoint * offset) const override; //无定义QPainter *sharedPainter() const override; //无注释virtual void inputMethodEvent(QInputMethodEvent * event);//对于事件事件,此事件处理程序可以在子类中重新实现以接收输入法合成事件。//当输入法状态更改时,将调用此处理程序。//注意,在创建自定义文本编辑小部件时,必须显式地设置Qt::WAInputMethodEnabled窗口属性//(使用setAttribute()函数),以便接收输入法事件。//默认实现会调用event->ignore(),这将拒绝输入方法事件。//请参阅QInputMethodEvent文档以获取更多信息。
//***********************************
//protected: 以上结束了 protected 标识符public://此方法仅适用于输入小部件。输入法使用它来查询小部件的一组属性,//以便能够支持复杂的输入法操作,如支持周围文本和重新转换。virtual QVariant inputMethodQuery(Qt::InputMethodQuery) const;//Q_PROPERTY(Qt::InputMethodHints inputMethodHints// READ inputMethodHints WRITE setInputMethodHints)Qt::InputMethodHints inputMethodHints() const;void setInputMethodHints(Qt::InputMethodHints hints);protected Q_SLOTS: //信号的槽函数//更新小部件的微聚焦,并通知输入法 query指定的状态已更改。void updateMicroFocus(Qt::InputMethodQuery query = Qt::ImQueryAll);protected: //开始本基类的 protected 函数//Creates a new widget window.//The parameters window, initializeWindow, and//destroyOldWindow are ignored in Qt 5.void create(WId = 0, bool initializeWindow = true,bool destroyOldWindow = true);void destroy(bool destroyWindow = true,bool destroySubWindows = true);//Frees up window system resources.//Destroys the widget window if destroyWindow is true.//destroy() calls itself recursively for all the child widgets,//passing destroySubWindows for the destroyWindow parameter.//To have more control over destruction of subwidgets,//destroy subwidgets selectively first. 析构函数//This function is usually called from the QWidget destructor.//找到一个新的小部件,将键盘焦点分配给Tab和Shift+Tab,//如果找到新的小部件,则返回true,否则返回false。//如果next为真,则该函数向前搜索,如果next为假,则它向后搜索。//有时,您可能需要重新实现此函数。例如,Web 浏览器可能会重新实现它,//以向前或向后移动其“当前活动链接”,//并在到达“页面”上的最后一个或第一个链接时调用focusNextPrevChild()//子控件在其父控件上调用focusNextPrevChild()函数,但只有包含子控件的窗口决定重定向焦点的位置。//通过为对象重写此函数,您可以控制所有子控件的焦点遍历。virtual bool focusNextPrevChild(bool next); //下面两个函数依赖本函数inline bool focusNextChild(){ return focusNextPrevChild(true) ; } //调用上面的函数inline bool focusPreviousChild(){ return focusNextPrevChild(false); }friend class QDataWidgetMapperPrivate; // for access to focusNextPrevChildprotected: //本类的构造函数QWidget(QWidgetPrivate &d, QWidget* parent, Qt::WindowFlags f);
private:void setBackingStore(QBackingStore *store);bool testAttribute_helper(Qt::WidgetAttribute) const;QLayout *takeLayout();friend class QBackingStoreDevice;friend class QWidgetRepaintManager;friend class QApplication;friend class QApplicationPrivate;friend class QGuiApplication;friend class QGuiApplicationPrivate;friend class QBaseApplication;friend class QPainter;friend class QPainterPrivate;friend class QPixmap; // for QPixmap::fill()friend class QFontMetrics;friend class QFontInfo;friend class QLayout;friend class QWidgetItem;friend class QWidgetItemV2;friend class QX11PaintEngine;friend class QWin32PaintEngine;friend class QShortcutPrivate;friend class QWindowSurface;friend class QGraphicsProxyWidget;friend class QGraphicsProxyWidgetPrivate;friend class QStyleSheetStyle;friend struct QWidgetExceptionCleaner;friend class QWidgetWindow;friend class QAccessibleWidget;friend class QAccessibleTable;friend class QAccessibleTabButton;
#ifndef QT_NO_GESTURESfriend class QGestureManager;friend class QWinNativePanGestureRecognizer;
#endif // QT_NO_GESTURESfriend class QWidgetEffectSourcePrivate;#ifdef Q_OS_MACfriend bool qt_mac_is_metal(const QWidget *w);
#endiffriend Q_WIDGETS_EXPORT QWidgetData *qt_qwidget_data(QWidget *widget);friend Q_WIDGETS_EXPORT QWidgetPrivate *qt_widget_private(QWidget *widget);}; //完结啦 class QWidget : public QObject, public QPaintDeviceQ_DECLARE_OPERATORS_FOR_FLAGS(QWidget::RenderFlags)#ifndef Q_QDOC
template <> inline //此处重载了类型转换函数 qobject_cast
QWidget * qobject_cast<QWidget*>(QObject * o)
{if (!o || !o->isWidgetType()) return nullptr;return static_cast<QWidget *>(o); //可见,QT 里的本转换等同于 static_cast转换
}template <> inline const //进行常量指针转换
QWidget *qobject_cast<const QWidget *>(const QObject * o)
{if (!o || !o->isWidgetType()) return nullptr;return static_cast<const QWidget *>(o);
}
#endif // !Q_QDOC#if QT_DEPRECATED_SINCE(6, 1)
inline bool QWidget::isTopLevel() const
{ return (windowType() & Qt::Window); }
#endif#define QWIDGETSIZE_MAX ((1<<24)-1)#ifndef QT_NO_DEBUG_STREAM //说明支持这样的写法 qDebug() << (QWidget *)打印指针
Q_WIDGETS_EXPORT QDebug operator<<(QDebug, const QWidget *);
#endif
//既然重载了此函数,就说明不是打印内存地址了,示例:Widget(0xcc66dff9a0, name="Widget")QT_END_NAMESPACE#endif // QWIDGET_H
(3)
谢谢