(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)
謝謝