(1)QEvent :: Type 是定义在 QEvent 里面的。但导致 QEvent 这个事件总基类的代码太长,重点不突出,故而分开显示 :
//此枚举类型定义了Qt中的有效事件类型。
enum QEvent :: Type {
/*
If you get a strange compiler error on the line with None,
it's probably because you're also including X11 headers,
which #define the symbol None.
Put the X11 includes after the Qt includes to solve this problem.
如果你在带有None的行上遇到奇怪的编译器错误,
那可能是因为你也包含了X11头文件,这些头文件定义了符号None。
将X11的包含文件放在Qt的包含文件之后以解决此问题。
*/
None = 0, // invalid event
Timer = 1, // timer event
MouseButtonPress = 2, // mouse button pressed
MouseButtonRelease = 3, // mouse button released
MouseButtonDblClick = 4, // mouse button double click
MouseMove = 5, // mouse move
KeyPress = 6, // key pressed
KeyRelease = 7, // key released
FocusIn = 8, // keyboard focus received
FocusOut = 9, // keyboard focus lost
FocusAboutToChange = 23, // keyboard focus is about to be lost
Enter = 10, // mouse enters widget
Leave = 11, // mouse leaves widget
Paint = 12, // paint widget
Move = 13, // move widget
Resize = 14, // resize widget
Create = 15, // after widget creation
Destroy = 16, // during widget destruction
Show = 17, // widget is shown
Hide = 18, // widget is hidden
Close = 19, // request to close widget
Quit = 20, // request to quit application
ParentChange = 21, // widget has been reparented
ParentAboutToChange = 131, // sent just before the parent change is done
ThreadChange = 22, // object has changed threads
WindowActivate = 24, // window was activated
WindowDeactivate = 25, // window was deactivated
ShowToParent = 26, // widget is shown to parent
HideToParent = 27, // widget is hidden to parent
Wheel = 31, // wheel event
WindowTitleChange = 33, // window title changed
WindowIconChange = 34, // icon changed
ApplicationWindowIconChange = 35, // application icon changed
ApplicationFontChange = 36, // application font changed
ApplicationLayoutDirectionChange = 37, // application layout direction changed
ApplicationPaletteChange = 38, // application palette changed
PaletteChange = 39, // widget palette changed
Clipboard = 40, // internal clipboard event
Speech = 42, // reserved for speech input
MetaCall = 43, // meta call event
SockAct = 50, // socket activation
WinEventAct = 132, // win event activation
DeferredDelete = 52, // deferred delete event
DragEnter = 60, // drag moves into widget
DragMove = 61, // drag moves in widget
DragLeave = 62, // drag leaves or is cancelled
Drop = 63, // actual drop
DragResponse = 64, // drag accepted/rejected
ChildAdded = 68, // new child widget
ChildPolished = 69, // polished抛光 child widget
ChildRemoved = 71, // deleted child widget
ShowWindowRequest = 73, // widget's window should be mapped
PolishRequest = 74, // widget should be polished
Polish = 75, // widget is polished
LayoutRequest = 76, // widget should be relayouted
UpdateRequest = 77, // widget should be repainted
UpdateLater = 78, // request update() later
EmbeddingControl = 79, // ActiveX embedding
ActivateControl = 80, // ActiveX activation
DeactivateControl = 81, // ActiveX deactivation
ContextMenu = 82, // context popup menu
InputMethod = 83, // input method
TabletMove = 87, // Wacom tablet event
LocaleChange = 88, // the system locale changed
LanguageChange = 89, // the application language changed
LayoutDirectionChange = 90, // the layout direction changed
Style = 91, // internal style event
TabletPress = 92, // tablet press
TabletRelease = 93, // tablet release
OkRequest = 94, // CE (Ok) button pressed
HelpRequest = 95, // CE (?) button pressed
IconDrag = 96, // proxy icon dragged
FontChange = 97, // font has changed
EnabledChange = 98, // enabled state has changed
ActivationChange = 99, // window activation has changed
StyleChange = 100, // style has changed
IconTextChange = 101, // icon text has changed. Deprecated.
ModifiedChange = 102, // modified state has changed
MouseTrackingChange = 109, // mouse tracking state has changed
WindowBlocked = 103, // window is about to be blocked modally
WindowUnblocked = 104, // windows modal blocking has ended
WindowStateChange = 105,
ReadOnlyChange = 106, // readonly state has changed
ToolTip = 110,
WhatsThis = 111,
StatusTip = 112,
ActionChanged = 113,
ActionAdded = 114,
ActionRemoved = 115,
FileOpen = 116, // file open request
Shortcut = 117, // shortcut triggered
ShortcutOverride = 51, // shortcut override request
WhatsThisClicked = 118,
//QAction * ToolBarC::toggleViewAction() const;
ToolBarChange = 120, // toolbar visibility toggled
ApplicationActivate = 121, // deprecated. Use ApplicationStateChange instead.
ApplicationActivated = ApplicationActivate, // deprecated 这四个废弃了
ApplicationDeactivate = 122, // deprecated. Use ApplicationStateChange instead.
ApplicationDeactivated = ApplicationDeactivate, // deprecated
QueryWhatsThis = 123, // query what's this widget help
EnterWhatsThisMode = 124,
LeaveWhatsThisMode = 125,
ZOrderChange = 126, // child widget has had its z-order changed
HoverEnter = 127, // mouse cursor enters a hover widget
HoverLeave = 128, // mouse cursor leaves a hover widget
HoverMove = 129, // mouse cursor move inside a hover widget
// last event id used = 132
EnterEditFocus = 150, // enter edit mode in keypad navigation
LeaveEditFocus = 151, // enter edit mode in keypad navigation
AcceptDropsChange = 152,
ZeroTimerEvent = 154, // Used for Windows Zero timer events
GraphicsSceneMouseMove = 155, // GraphicsView
GraphicsSceneMousePress = 156,
GraphicsSceneMouseRelease = 157,
GraphicsSceneMouseDoubleClick = 158,
GraphicsSceneContextMenu = 159,
GraphicsSceneHoverEnter = 160,
GraphicsSceneHoverMove = 161,
GraphicsSceneHoverLeave = 162,
GraphicsSceneHelp = 163,
GraphicsSceneDragEnter = 164,
GraphicsSceneDragMove = 165,
GraphicsSceneDragLeave = 166,
GraphicsSceneDrop = 167,
GraphicsSceneWheel = 168,
GraphicsSceneLeave = 220,
KeyboardLayoutChange = 169, // keyboard layout changed
DynamicPropertyChange = 170,
// A dynamic property was changed through setProperty/property
TabletEnterProximity = 171,
TabletLeaveProximity = 172,
NonClientAreaMouseMove = 173,
NonClientAreaMouseButtonPress = 174,
NonClientAreaMouseButtonRelease = 175,
NonClientAreaMouseButtonDblClick = 176,
MacSizeChange = 177, // when the Qt::WA_Mac{Normal,Small,Mini}Size changes
ContentsRectChange = 178, // sent by QWidget::setContentsMargins (internal)
MacGLWindowChange = 179, // Internal! the window of the GLWidget has changed
FutureCallOut = 180,
GraphicsSceneResize = 181,
GraphicsSceneMove = 182,
CursorChange = 183,
ToolTipChange = 184,
NetworkReplyUpdated = 185, // Internal for QNetworkReply
GrabMouse = 186,
UngrabMouse = 187,
GrabKeyboard = 188,
UngrabKeyboard = 189,
StateMachineSignal = 192,
StateMachineWrapped = 193,
TouchBegin = 194,
TouchUpdate = 195,
TouchEnd = 196,
NativeGesture = 197, // QtGui native gesture
RequestSoftwareInputPanel = 199,
CloseSoftwareInputPanel = 200,
WinIdChange = 203,
Gesture = 198,
GestureOverride = 202,
ScrollPrepare = 204,
Scroll = 205,
Expose = 206,
InputMethodQuery = 207,
OrientationChange = 208, // Screen orientation has changed
TouchCancel = 209,
ThemeChange = 210,
SockClose = 211, // socket closed
PlatformPanel = 212,
StyleAnimationUpdate = 213, // style animation target should be updated
ApplicationStateChange = 214,
WindowChangeInternal = 215, // internal for QQuickWidget
ScreenChangeInternal = 216,
PlatformSurface = 217, // Platform surface created or about to be destroyed
Pointer = 218, // Qt 5: QQuickPointerEvent; Qt 6: unused so far
TabletTrackingChange = 219, // tablet tracking state has changed
// 512 reserved for Qt Jambi's MetaCall event
// 513 reserved for Qt Jambi's DeleteOnMainThread event
User = 1000, // first user event id
MaxUser = 65535 // last user event id
};
Q_ENUM(Type)
(2)给出 QEvent 的源码,作为事件系统的总基类,本代码很少 :
#ifndef QCOREEVENT_H
#define QCOREEVENT_H
#include <QtCore/qnamespace.h>
#include <QtCore/qbytearray.h>
#include <QtCore/qobjectdefs.h>
QT_BEGIN_NAMESPACE
#define Q_EVENT_DISABLE_COPY(Class) \
protected: \
Class(const Class &) = default; \
Class(Class &&) = delete; \
Class &operator=(const Class &other) = default; \
Class &operator=(Class &&) = delete
class QEventPrivate;
/* 这是轻量级的元对象系统
#define Q_GADGET \ 此宏引入了一些代码
public: \
static const QMetaObject staticMetaObject; \ //元对象系统数据成员
void qt_check_for_QGADGET_macro(); \
typedef void QtGadgetHelper; \ //定义了一个类型
private: \
QT_WARNING_PUSH \
Q_OBJECT_NO_ATTRIBUTES_WARNING \
\
Q_DECL_HIDDEN_STATIC_METACALL \
static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **); \
\
QT_WARNING_POP \
QT_ANNOTATE_CLASS(qt_qgadget, "") \
*/
/*
The QEvent class is the base class of all event classes.
Event objects contain event parameters.
Detailed Description :
Qt的主事件循环(QCoreApplication:exec())会从事件队列中获取原生窗口系统事件,将其转换为QEvents,
并将转换后的事件发送给QObjects。
一般来说,事件源自底层窗口系统(spontaneous()返回 true),
但也可以通过 QCoreApplication::sendEvent()和
QCoreApplication::postEvent()手动发送事件(spontaneous()返回false)。
QObjects通过调用其 QObject::event()函数来接收事件。
在子类中可以重写此函数以定制事件处理并添加其他事件类型;
QWidget::event()就是一个显著的例子。
默认情况下,事件会被派发到事件处理函数,如QObject::timerEvent()和QWidget::mouseMoveEvent()。
QObject::installEventFilter()允许一个对象拦截指向其他对象的预定事件。
基本 QEvent只包含一个事件类型参数和一个“接受”标志。
通过 accept()函数设置该标志,通过ignore ()函数清除它。
该标志默认为设置状态,但不要依赖这一点,因为子类可能选择在构造函数中清除它。
QEvent的子类包含描述特定事件的额外参数。
*/
class Q_CORE_EXPORT QEvent // event base class
{
Q_GADGET
//无注释,这应该不是有用的属性
QDOC_PROPERTY(bool accepted READ isAccepted WRITE setAccepted)
Q_EVENT_DISABLE_COPY(QEvent); //定义见上,意思是本类可被复制,不可被移动。
private:
/*
We can assume that C++ types are 8-byte aligned, 我们可以假设C++类型是8字节对齐的,
and we can't assume that compilers coalesce data members from subclasses.
我们不能假设编译器会合并子类的数据成员。使用位域填充到下一个8字节对齐大小,即16字节。
Use bitfields to fill up to next 8-byte aligned size, which is 16 bytes.
这样我们不会浪费内存,并且为未来的标志留有足够的空间。
That way we don't waste memory, and have plenty of room for future flags.
不要在最重要的标志上使用位域,因为这会生成更多的代码,并且访问总是内联的。
Don't use bitfields for the most important flags,
as that would generate more code, and access is always inline.
Bytes used are: 使用的字节数是:8个vptr +2个类型+3个布尔标志=>剩余3字节,即24位。
8 vptr + 2 type + 3 bool flags => 3 bytes left, so 24 bits.
However, compilers will word-align the quint16s after the bools,
so add another unused bool to fill that gap, which leaves us with 16 bits.
然而,编译器将在书之后对齐五字节整数,
因此,添加另一个未使用的布尔值来填补这个空隙,这使我们拥有16位。
*/
bool m_posted = false;
bool m_spont = false;
bool m_accept = true ;
bool m_unused = false;
quint16 m_reserved : 13;
quint16 m_inputEvent : 1; //这些 bit是为了标识事件的所属的类型区域
quint16 m_pointerEvent : 1;
quint16 m_singlePointEvent : 1;
friend class QCoreApplication;
friend class QCoreApplicationPrivate;
friend class QThreadData;
friend class QApplication;
friend class QGraphicsScenePrivate;
// from QtTest:
friend class QSpontaneKeyEvent;
// needs this:
Q_ALWAYS_INLINE void setSpontaneous() { m_spont = true; }
protected:
struct InputEventTag { //无注释,这样的类往往是为了实现重载
explicit InputEventTag() = default;
};
QEvent(Type type, InputEventTag) : QEvent(type)
{ m_inputEvent = true; }
struct PointerEventTag {
explicit PointerEventTag() = default;
};
QEvent(Type type, PointerEventTag) : QEvent(type, InputEventTag{})
{ m_pointerEvent = true; }
struct SinglePointEventTag {
explicit SinglePointEventTag() = default;
};
QEvent(Type type, SinglePointEventTag) : QEvent(type, PointerEventTag{})
{ m_singlePointEvent = true; }
quint16 t; //本类的有一个数据成员,表示本事件的类型
public:
//此枚举类型定义了Qt中的有效事件类型。
enum Type {
//................... 略
User = 1000, // first user event id
MaxUser = 65535 // last user event id
};
Q_ENUM(Type)
//Constructs an event object of type type.
explicit QEvent(Type type); //构造函数
virtual ~QEvent(); //析构函数,可能会从事件队列里删除之。
//Destroys the event.
//If it was posted, it will be removed from the list of events to be posted.
//Creates and returns an identical copy of this event.
virtual QEvent * clone() const;
inline Type type() const { return static_cast<Type>(t); }
static int registerEventType(int hint = -1) noexcept; //返回一个空的可用的事件类型
//注册并返回一个自定义事件类型。如果可用,将使用所提供的提示;
//否则,它将返回一个介于QEvent::User和 QEvent::MaxUser 之间的尚未被注册的值。
//如果提示值不在 QEvent:User和 QEvent:MaxUser之间,则该提示将被忽略。
//如果所有可用值已被占用或程序正在关闭,则返回-1。
inline bool spontaneous() const { return m_spont ; }
inline bool isAccepted() const { return m_accept; }
inline virtual void setAccepted(bool accepted) { m_accept = accepted; }
inline void accept () { m_accept = true ; }
inline void ignore () { m_accept = false ; }
inline bool isInputEvent() const noexcept { return m_inputEvent ; }
inline bool isPointerEvent() const noexcept { return m_pointerEvent ; }
inline bool isSinglePointEvent() const noexcept { return m_singlePointEvent; }
}; //完结 class QEvent
//*********************余下的暂时先不用看***************************
class Q_CORE_EXPORT QTimerEvent : public QEvent
{
Q_EVENT_DISABLE_COPY(QTimerEvent);
public:
explicit QTimerEvent(int timerId);
~QTimerEvent();
int timerId() const { return id; }
QTimerEvent *clone() const override { return new QTimerEvent(*this); }
protected:
int id;
};
class QObject;
class Q_CORE_EXPORT QChildEvent : public QEvent
{
Q_EVENT_DISABLE_COPY(QChildEvent);
public:
QChildEvent(Type type, QObject *child);
~QChildEvent();
QObject *child() const { return c; }
bool added() const { return type() == ChildAdded; }
bool polished() const { return type() == ChildPolished; }
bool removed() const { return type() == ChildRemoved; }
QChildEvent *clone() const override { return new QChildEvent(*this); }
protected:
QObject *c;
};
class Q_CORE_EXPORT QDynamicPropertyChangeEvent : public QEvent
{
Q_EVENT_DISABLE_COPY(QDynamicPropertyChangeEvent);
public:
explicit QDynamicPropertyChangeEvent(const QByteArray &name);
~QDynamicPropertyChangeEvent();
inline QByteArray propertyName() const { return n; }
QDynamicPropertyChangeEvent *clone() const override { return new QDynamicPropertyChangeEvent(*this); }
private:
QByteArray n;
};
class Q_CORE_EXPORT QDeferredDeleteEvent : public QEvent
{
Q_EVENT_DISABLE_COPY(QDeferredDeleteEvent);
public:
explicit QDeferredDeleteEvent();
~QDeferredDeleteEvent();
int loopLevel() const { return level; }
QDeferredDeleteEvent *clone() const override { return new QDeferredDeleteEvent(*this); }
private:
int level;
friend class QCoreApplication;
};
QT_END_NAMESPACE
#endif // QCOREEVENT_H
(3)
谢谢