`
mmdev
  • 浏览: 12949410 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

QT 事件和信号

 
阅读更多

仔细来看,事件与信号其实并无多大差别,从我们对其需求上来说,都只要能注册事件或信号响应函数,在事件或信号产生时能够被通知到即可。但有一项区别在于,事件处理函数的返回值是有意义的,我们要根据这个返回值来确定是否还要继续事件的处理,比如在QT中,事件处理函数如果返回true,则这个事件处理已完成,QApplication会接着处理下一个事件,而如果返回false,那么事件分派函数会继续向上寻找下一个可以处理该事件的注册方法。信号处理函数的返回值对信号分派器来说是无意义的。

另外还有一个需要我们关注的问题是事件和信号处理时的优先级问题。在QT中,事件因为都是与窗口相关的,所以事件回调时都是从当前窗口开始,一级一级向上派发,直到有一个窗口返回true,截断了事件的处理为止。对于信号的处理则比较简单,默认是没有顺序的,如果需要明确的顺序,可以在信号注册时显示地指明槽的位置。
在QT中,事件使用了一个事件队列来维护,如果事件的处理中又产生了新的事件,那么新的事件会加入到队列尾,直到当前事件处理完毕后, QApplication再去队列头取下一个事件来处理。而信号的处理方式有些不同,信号处理是立即回调的,也就是一个信号产生后,他上面所注册的所有槽都会立即被回调。这样就会产生一个递归调用的问题,比如某个信号处理器中又产生了一个信号,会使得信号的处理像一棵树一样的展开。

事件(event)是由窗口系统或者Qt自身产生的,用以响应所发生的各类事情。当用户按下或者松开键盘或者鼠标上的按键时,就可以产生一个键

盘或者鼠标事件;当某个窗口第一次显示的时候,就会产生一个绘制事件,用来告诉窗口需要重新绘制它本身,从而使得该窗口可见。大多数事件是

作为用户动作的响应而产生的,但是也有一些例外,比如像定时器事件,则是由系统独立产生的。
一般情况下,在使用窗口部件的时候,Qt的窗口部件往往都会发送信号,而这时信号也是十分有用的,但是当我们需要编写自己的自定义窗口部

件的时候,又或者是我们想改变已经存在的Qt的窗口部件的行为的时候,事件就变的非常有用了。例如,当我们使用QPushButton时,我们对于它的

clicked()信号往往更为关注,而很少关心促成发射该信号的底层鼠标事件或者键盘事件。但是我们要实现的是一个类似于QPushButton,或者说我们

要实现一个QPushButton的话,就要编写一定的处理鼠标和键盘的事件的代码,而且在必要的时候,还需要发射clicked()信号。可以这么想信号是命

令而事件就是消息,一个战场上的军官只要在等到准确的消息之后,才会根据所传来的消息然后下达命令。
信号和事件的区别就在与,事件比信号更加底层,而且如果一个信号对应多个槽的话,信号的传递是无序的,而事件的传递是有序的。其实在多

线程的实现也依赖与Qt的事件处理机制。以上我们对于事件有了初步的了解了。下面我们继续对事件进行更细致的了解。
在QT中,事件被封装成一个个对象,所有的事件均继承自抽象类QEvent.接下来依次谈谈QT中有谁来产生,分发,接受和处理事件:
1.谁来产生事件:最容易想到的是我们的输入设备,比如键盘,鼠标产生的事件keyPressEvent,keyReleaseEvent,mousePressEvent事件(他

们被封装成QMouseEvent和QKeyEvent),这些事件来自于底层的操作系统,他们可以异步的形式通知Qt事件处理系统,后文会仔细道来。当然QT自己

也会产生很多事件,比如QObject::startTimer()会触发QTimerEvent。用户的程序还可以自己定制事件。
2.谁来接受和处理事件:答案是QObject。在Qt的内省机制剖析一文中已经介绍QObject类是整个QT对象模型的心脏,事件处理机制是QObject

三大职责(内存管理,内省(intropection)与事件处理)之一。任何一个想要接受并处理事件的对象均须继承自QObject,可以选择重QObject::event

()函数或事件的处理权交给父类。
3.谁来负责分发事件:对于non-GUI的QT程序,是由QCoreApplication负责将QEvent分发给QObject的子类-Receiver.对于Qt GUI程序,由

QApplication来负责。
在QT中,事件就是QEvent子类的一个实例。Qt处理的事件类型有一百多种
QEvent::None0Not an event.
QEvent::AccessibilityDescription130Used to query accessibility description texts (QAccessibleEvent).
QEvent::AccessibilityHelp119Used to query accessibility help texts (QAccessibleEvent).
QEvent::AccessibilityPrepare86Accessibility information is requested.
QEvent::ActionAdded114A new action has been added (QActionEvent).
QEvent::ActionChanged113An action has been changed (QActionEvent).
QEvent::ActionRemoved115An action has been removed (QActionEvent).
QEvent::ActivationChange99A widget's top-level window activation state has changed.
QEvent::ApplicationActivate121The application has been made available to the user.
QEvent::ApplicationActivatedApplicationActivateThis enum has been deprecated. Use

ApplicationActivate instead.
QEvent::ApplicationDeactivate122The application has been suspended, and is unavailable to the user.
QEvent::ApplicationFontChange36The default application font has changed.
QEvent::ApplicationLayoutDirectionChange37The default application layout direction has changed.
QEvent::ApplicationPaletteChange38The default application palette has changed.
QEvent::ApplicationWindowIconChange35The application's icon has changed.
QEvent::ChildAdded68An object gets a child (QChildEvent).
QEvent::ChildInserted70An object gets a child (QChildEvent). Qt3Support only, use ChildAdded

instead.
QEvent::ChildPolished69A widget child gets polished (QChildEvent).
QEvent::ChildRemoved71An object loses a child (QChildEvent).
QEvent::Clipboard40The clipboard contents have changed (QClipboardEvent).
QEvent::Close19Widget was closed (QCloseEvent).
QEvent::CloseSoftwareInputPanel200A widget wants to close the software input panel (SIP).
QEvent::ContentsRectChange178The margins of the widget's content rect changed.
QEvent::ContextMenu82Context popup menu (QContextMenuEvent).
QEvent::CursorChange183The widget's cursor has changed.
QEvent::DeferredDelete52The object will be deleted after it has cleaned up.
QEvent::DragEnter60The cursor enters a widget during a drag and drop operation

(QDragEnterEvent).
QEvent::DragLeave62The cursor leaves a widget during a drag and drop operation

(QDragLeaveEvent).
QEvent::DragMove61A drag and drop operation is in progress (QDragMoveEvent).
QEvent::Drop63A drag and drop operation is completed (QDropEvent).
QEvent::EnabledChange98Widget's enabled state has changed.
QEvent::Enter10Mouse enters widget's boundaries.
QEvent::EnterEditFocus150An editor widget gains focus for editing.
QEvent::EnterWhatsThisMode124Send to toplevel widgets when the application enters "What's This?" mode.
QEvent::FileOpen116File open request (QFileOpenEvent).
QEvent::FocusIn8Widget gains keyboard focus (QFocusEvent).
QEvent::FocusOut9Widget loses keyboard focus (QFocusEvent).
QEvent::FontChange97Widget's font has changed.
QEvent::GrabKeyboard188Item gains keyboard grab (QGraphicsItem only).
QEvent::GrabMouse186Item gains mouse grab (QGraphicsItem only).
QEvent::GraphicsSceneContextMenu159Context popup menu over a graphics scene (QGraphicsSceneContextMenuEvent).
QEvent::GraphicsSceneDragEnter164The cursor enters a graphics scene during a drag and drop operation

(QGraphicsSceneDragDropEvent).
QEvent::GraphicsSceneDragLeave166The cursor leaves a graphics scene during a drag and drop operation

(QGraphicsSceneDragDropEvent).
QEvent::GraphicsSceneDragMove165A drag and drop operation is in progress over a scene

(QGraphicsSceneDragDropEvent).
QEvent::GraphicsSceneDrop167A drag and drop operation is completed over a scene

(QGraphicsSceneDragDropEvent).
QEvent::GraphicsSceneHelp163The user requests help for a graphics scene (QHelpEvent).
QEvent::GraphicsSceneHoverEnter160The mouse cursor enters a hover item in a graphics scene

(QGraphicsSceneHoverEvent).
QEvent::GraphicsSceneHoverLeave162The mouse cursor leaves a hover item in a graphics scene

(QGraphicsSceneHoverEvent).
QEvent::GraphicsSceneHoverMove161The mouse cursor moves inside a hover item in a graphics scene

(QGraphicsSceneHoverEvent).
QEvent::GraphicsSceneMouseDoubleClick158Mouse press again (double click) in a graphics scene

(QGraphicsSceneMouseEvent).
QEvent::GraphicsSceneMouseMove155Move mouse in a graphics scene (QGraphicsSceneMouseEvent).
QEvent::GraphicsSceneMousePress156Mouse press in a graphics scene (QGraphicsSceneMouseEvent).
QEvent::GraphicsSceneMouseRelease157Mouse release in a graphics scene (QGraphicsSceneMouseEvent).
QEvent::GraphicsSceneMove182Widget was moved (QGraphicsSceneMoveEvent).
QEvent::GraphicsSceneResize181Widget was resized (QGraphicsSceneResizeEvent).
QEvent::GraphicsSceneWheel168Mouse wheel rolled in a graphics scene (QGraphicsSceneWheelEvent).
QEvent::Hide18Widget was hidden (QHideEvent).
QEvent::HideToParent27A child widget has been hidden.
QEvent::HoverEnter127The mouse cursor enters a hover widget (QHoverEvent).
QEvent::HoverLeave128The mouse cursor leaves a hover widget (QHoverEvent).
QEvent::HoverMove129The mouse cursor moves inside a hover widget (QHoverEvent).
QEvent::IconDrag96The main icon of a window has been dragged away (QIconDragEvent).
QEvent::IconTextChange101Widget's icon text has been changed.
QEvent::InputMethod83An input method is being used (QInputMethodEvent).
QEvent::KeyPress6Key press (QKeyEvent).
QEvent::KeyRelease7Key release (QKeyEvent).
QEvent::LanguageChange89The application translation changed.
QEvent::LayoutDirectionChange90The direction of layouts changed.
QEvent::LayoutRequest76Widget layout needs to be redone.
QEvent::Leave11Mouse leaves widget's boundaries.
QEvent::LeaveEditFocus151An editor widget loses focus for editing.
QEvent::LeaveWhatsThisMode125Send to toplevel widgets when the application leaves "What's This?" mode.
QEvent::LocaleChange88The system locale has changed.
QEvent::NonClientAreaMouseButtonDblClick176A mouse double click occurred outside the client area.
QEvent::NonClientAreaMouseButtonPress174A mouse button press occurred outside the client area.
QEvent::NonClientAreaMouseButtonRelease175A mouse button release occurred outside the client area.
QEvent::NonClientAreaMouseMove173A mouse move occurred outside the client area.
QEvent::MacSizeChange177The user changed his widget sizes (Mac OS X only).
QEvent::MenubarUpdated153The window's menu bar has been updated.
QEvent::MetaCall43An asynchronous method invocation via QMetaObject::invokeMethod().
QEvent::ModifiedChange102Widgets modification state has been changed.
QEvent::MouseButtonDblClick4Mouse press again (QMouseEvent).
QEvent::MouseButtonPress2Mouse press (QMouseEvent).
QEvent::MouseButtonRelease3Mouse release (QMouseEvent).
QEvent::MouseMove5Mouse move (QMouseEvent).
QEvent::MouseTrackingChange109The mouse tracking state has changed.
QEvent::Move13Widget's position changed (QMoveEvent).
QEvent::Paint12Screen update necessary (QPaintEvent).
QEvent::PaletteChange39Palette of the widget changed.
QEvent::ParentAboutToChange131The widget parent is about to change.
QEvent::ParentChange21The widget parent has changed.
QEvent::Polish75The widget is polished.
QEvent::PolishRequest74The widget should be polished.
QEvent::QueryWhatsThis123The widget should accept the event if it has "What's This?" help.
QEvent::RequestSoftwareInputPanel199A widget wants to open a software input panel (SIP).
QEvent::Resize14Widget's size changed (QResizeEvent).
QEvent::Shortcut117Key press in child for shortcut key handling (QShortcutEvent).
QEvent::ShortcutOverride51Key press in child, for overriding shortcut key handling (QKeyEvent).
QEvent::Show17Widget was shown on screen (QShowEvent).
QEvent::ShowToParent26A child widget has been shown.
QEvent::SockAct50Socket activated, used to implement QSocketNotifier.
QEvent::StateMachineSignal192A signal delivered to a state machine (QStateMachine::SignalEvent).
QEvent::StateMachineWrapped193The event is a wrapper for, i.e., contains, another event

(QStateMachine::WrappedEvent).
QEvent::StatusTip112A status tip is requested (QStatusTipEvent).
QEvent::StyleChange100Widget's style has been changed.
QEvent::TabletMove87Wacom tablet move (QTabletEvent).
QEvent::TabletPress92Wacom tablet press (QTabletEvent).
QEvent::TabletRelease93Wacom tablet release (QTabletEvent).
QEvent::OkRequest94Ok button in decoration pressed. Supported only for Windows CE.
QEvent::TabletEnterProximity171Wacom tablet enter proximity event (QTabletEvent), sent to QApplication.
QEvent::TabletLeaveProximity172Wacom tablet leave proximity event (QTabletEvent), sent to QApplication.
QEvent::Timer1Regular timer events (QTimerEvent).
QEvent::ToolBarChange120The toolbar button is toggled on Mac OS X.
QEvent::ToolTip110A tooltip was requested (QHelpEvent).
QEvent::ToolTipChange184The widget's tooltip has changed.
QEvent::UngrabKeyboard189Item loses keyboard grab (QGraphicsItem only).
QEvent::UngrabMouse187Item loses mouse grab (QGraphicsItem only).
QEvent::UpdateLater78The widget should be queued to be repainted at a later time.
QEvent::UpdateRequest77The widget should be repainted.
QEvent::WhatsThis111The widget should reveal "What's This?" help (QHelpEvent).
QEvent::WhatsThisClicked118A link in a widget's "What's This?" help was clicked.
QEvent::Wheel31Mouse wheel rolled (QWheelEvent).
QEvent::WinEventAct132A Windows-specific activation event has occurred.
QEvent::WindowActivate24Window was activated.
QEvent::WindowBlocked103The window is blocked by a modal dialog.
QEvent::WindowDeactivate25Window was deactivated.
QEvent::WindowIconChange34The window's icon has changed.
QEvent::WindowStateChange105The window's state (minimized, maximized or full-screen) has changed

(QWindowStateChangeEvent).
QEvent::WindowTitleChange33The window title has changed.
QEvent::WindowUnblocked104The window is unblocked after a modal dialog exited.
QEvent::ZOrderChange126The widget's z-order has changed. This event is never sent to top level

windows.
QEvent::KeyboardLayoutChange169The keyboard layout has changed.
QEvent::DynamicPropertyChange170A dynamic property was added, changed or removed from the object.
QEvent::TouchBegin194Beginning of a sequence of touch-screen and/or track-pad events

(QTouchEvent)
QEvent::TouchUpdate195Touch-screen event (QTouchEvent)
QEvent::TouchEnd196End of touch-event sequence (QTouchEvent)
QEvent::WinIdChange203The window system identifer for this native widget has changed
QEvent::Gesture198A gesture was triggered (QGestureEvent)
QEvent::GestureOverride202A gesture override was triggered (QGestureEvent)

分享到:
评论

相关推荐

    QT自定义信号和槽

    QT QT自定义信号和槽 QT自定义 信号和槽 QT信号。讲解怎么在 QT中自己定义消息和槽,QT编程可以看看

    第9章 Qt事件机制与原理

    系统内核的消息通过事件处理转变成QT的信号 9.1.3 Qt中的事件处理 (1)在Qt中,事件被封装成一个个对象,所有的事件均继承自抽象类QEvent.  事件处理的核心包括事件①产生、②分发、③接受和处理 ①事件的产生 ...

    Qt Windows 休眠唤醒信号

    Qt Windows 休眠唤醒信号

    QT静态单例管理信号和槽

    QT 单例

    Qt静态函数发送信号

    讲述Qt如何实现静态函数中发送信号,对应文章: https://lizhifun.blog.csdn.net/article/details/112631489

    QT信号与槽机制浅析

    QT信号与槽机制浅析Qt的信号和槽机制是Qt的一大特点,实际上这是和MFC中的消息映射机制相似的东西,要完成的事情也差不多,就是发送一个消息然后让其它窗口响应,当然,这里的消息是广义的 说法,简单点说就是如何在一个类...

    QT中的信号和槽机制分析

    QT中的信号和槽机制分析,QT是linux下桌面编程的首选.信号和槽是QT的核心,重要性你知道的

    QT布局,信号和槽PPT

    QT信号和槽概括,对初学QT来说有很大帮助了解架构

    QT入门之信号和槽的那些事

    QT入门之信号和槽的那些事有以下6部分 1、基础信号和槽 2、 自定义的信号和槽 3、 自定义的信号和槽发生重载的解决方法 4、 QString转char *类型 5、 信号连接信号 6、 断开连接 5、 QT4之前版本的信号和槽的连接...

    QT信号和槽机制分析

    从Qt源代码详细分析QT信号和槽机制,对深刻理解Qt的核心和Qt具有特别的帮助。

    Qt获取并显示电池电量和WIFI信号强度

    Qt获取并显示电池电量和WIFI信号强度,在win7下可用,其他平台未试,详见我的博客http://blog.csdn.net/caoshangpa/article/details/51062351

    Qt信号和槽

    信号与槽机制是qt的核心,一个信号可以接多个槽,多个信号也可以接一个槽,当然一个信号也可以接一个槽。本文的重点是讲述emit的应用,emit是qt中定义的宏,是用来发送一个信号,然后你再写一个槽函数。在构造函数中...

    Qt 信号在多层次对象间传递 多层嵌套类对象之间信号传递,可能是五层,或多层,子对象要发信号给第一层

    Qt 信号在多层次对象间传递 多层嵌套类对象之间信号传递,可能是五...QT信号量传递 QT信号量多层传递,QT信号量任意层传递,Qt信号量多层次对象间传递 博文:https://blog.csdn.net/q610098308/article/details/125976674

    QT 下 信号量使用

    QT 下 信号量使用,描述多个信号量同步机制

    Qt,充电信号显示,用Qlabel实现,简单

    Qt5.7,工程源码,充电信号显示,用简单的Qlabel实现,可以模拟手机信号,或充电条滚动

    QT不触发readyread信号槽的原因.zip

    QT的readyread信号槽总是不被触发的原因 大家在使用QT的时候有没有遇到过第一次能触发readyread信号槽,执行函数.但是就再也触发不了readyread信号槽的情况呢? 我来告诉你答案

    QT信号与槽来实现对象通信

    信号和槽机制是Qt的核心机制之一,要掌握Qt编程就需要对信号和槽有所了解。信号和槽是一种高级接口,它们被应用于对象之间的通信,它们是Qt的核心特性,也是Qt不同于其它同类工具包的重要地方之一。在我们所了解的...

    QT信号与槽总结

    QT信号和槽的关系简介,分为:信号、槽、连接、注意事项和扩展事项等五部分内容。QT信号和槽的关系简介,分为:信号、槽、连接、注意事项和扩展事项等五部分内容。QT信号和槽的关系简介,分为:信号、槽、连接、注意...

    QT 的信号与槽机制介绍

    QT 的信号与槽机制介绍 QT 的信号与槽机制介绍QT 的信号与槽机制介绍

Global site tag (gtag.js) - Google Analytics