- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Events in wxWidgets
Events are integral part of every GUI application. All GUI applications are event-driven. An application reacts to different event types which are generated during its lifetime. Events are generated mainly by the user of an application. But they can be generated by other means as well, e.g. an Internet connection, a window manager, or a timer. When the application starts a main loop is created. The application sits in the main loop and waits for the events to be generated. The main loop quits, when we exit the application.
Definitions
Event is a piece of application-level information from the underlying framework, typically the GUI toolkit. Event loop is a programming construct that waits for and dispatches events or messages in a program. The event loop repeatedly looks for events to process them. A dispatcher is a process which maps events to event handlers. Event handlers are methods that react to events.
Event object is an object associated with the event. It is usually a window. Event type is a unique event that has been generated.
A simple event example
The traditional way to work with events in wxWidgets is to use static event tables. This was influenced by the Microsoft Foundation Classes (MFC). A more flexible and modern way is to use the Connect()
method. We use it throughout the wxWidgets tutorial.
Event table
In the next example, we show an example, where we use event tables.
button.h
#include <wx/wx.h> class MyButton : public wxFrame { public: MyButton(const wxString& title); void OnQuit(wxCommandEvent& event); private: DECLARE_EVENT_TABLE() };
button.cpp
#include "button.h" MyButton::MyButton(const wxString& title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(270, 150)) { wxPanel *panel = new wxPanel(this, wxID_ANY); wxButton *button = new wxButton(panel, wxID_EXIT, wxT("Quit"), wxPoint(20, 20)); Centre(); } void MyButton::OnQuit(wxCommandEvent& WXUNUSED(event)) { Close(true); } BEGIN_EVENT_TABLE(MyButton, wxFrame) EVT_BUTTON(wxID_EXIT, MyButton::OnQuit) END_EVENT_TABLE()
main.h
#include <wx/wx.h> class MyApp : public wxApp { public: virtual bool OnInit(); };
main.cpp
#include "main.h" #include "button.h" IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { MyButton *button = new MyButton(wxT("Button")); button->Show(true); return true; }
In our example we create a simple button. By clicking on the button, we close the application.
private: DECLARE_EVENT_TABLE()
In the header file, we declare an event table with the DECLARE_EVENT_TABLE()
macro.
BEGIN_EVENT_TABLE(MyButton, wxFrame) EVT_BUTTON(wxID_EXIT, MyButton::OnQuit) END_EVENT_TABLE()
We implement an event table by mapping each event to the appropriate member function.
Example using Connect()
We will talk about a move event. A move event holds information about move change events. A move event is generated, when we move a window to a new position. The class that represents the move event is wxMoveEvent
. The wxEVT_MOVE
is an event type.
move.h
#include <wx/wx.h> class Move : public wxFrame { public: Move(const wxString& title); void OnMove(wxMoveEvent & event); wxStaticText *st1; wxStaticText *st2; };
move.cpp
#include "move.h" Move::Move(const wxString& title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(250, 130)) { wxPanel *panel = new wxPanel(this, -1); st1 = new wxStaticText(panel, -1, wxT(""), wxPoint(10, 10)); st2 = new wxStaticText(panel, -1, wxT(""), wxPoint(10, 30)); Connect(wxEVT_MOVE, wxMoveEventHandler(Move::OnMove)); Centre(); } void Move::OnMove(wxMoveEvent& event) { wxPoint size = event.GetPosition(); st1->SetLabel(wxString::Format(wxT("x: %d"), size.x )); st2->SetLabel(wxString::Format(wxT("y: %d"), size.y )); }
main.h
#include <wx/wx.h> class MyApp : public wxApp { public: virtual bool OnInit(); };
main.cpp
#include "main.h" #include "move.h" IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { Move *move = new Move(wxT("Move event")); move->Show(true); return true; }
The example displays the current position of the window.
Connect(wxEVT_MOVE, wxMoveEventHandler(Move::OnMove));
Here we connect a wxEVT_MOVE
event type with the OnMove()
method.
wxPoint size = event.GetPosition();
The event parameter in the OnMove()
method is an object specific to a particular event. In our case it is the instance of a wxMoveEvent
class. This object holds information about the event. We can find out the current position by calling the GetPosition()
method of the event.

Event propagation
There are two types of events: basic events and command events. They differ in propagation. Event propagation is travelling of events from child widgets to parent widgets and grand parent widgets etc. Basic events do not propagate. Command events do propagate. For example wxCloseEvent
is a basic event. It does not make sense for this event to propagate to parent widgets.
By default, the event that is caught in a event handler stops propagating. To continue propagation, we must call the Skip()
method.
propagate.h
#include <wx/wx.h> class Propagate : public wxFrame { public: Propagate(const wxString& title); void OnClick(wxCommandEvent& event); }; class MyPanel : public wxPanel { public: MyPanel(wxFrame *frame, int id); void OnClick(wxCommandEvent& event); }; class MyButton : wxButton { public: MyButton(MyPanel *panel, int id, const wxString &label); void OnClick(wxCommandEvent& event); };
propagate.cpp
#include <iostream> #include "propagate.h" const int ID_BUTTON = 1; Propagate::Propagate(const wxString& title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(250, 130)) { MyPanel *panel = new MyPanel(this, -1); new MyButton(panel, ID_BUTTON, wxT("Ok")); Connect(ID_BUTTON, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(Propagate::OnClick)); Centre(); } void Propagate::OnClick(wxCommandEvent& event) { std::cout << "event reached frame class" << std::endl; event.Skip(); } MyPanel::MyPanel(wxFrame *frame, int id) : wxPanel(frame, id) { Connect(ID_BUTTON, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MyPanel::OnClick)); } void MyPanel::OnClick(wxCommandEvent& event) { std::cout << "event reached panel class" << std::endl; event.Skip(); } MyButton::MyButton(MyPanel *mypanel, int id, const wxString& label) : wxButton(mypanel, id, label, wxPoint(15, 15)) { Connect(ID_BUTTON, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MyButton::OnClick)); } void MyButton::OnClick(wxCommandEvent& event) { std::cout << "event reached button class" << std::endl; event.Skip(); }
main.h
#include <wx/wx.h> class MyApp : public wxApp { public: virtual bool OnInit(); };
main.cpp
#include "main.h" #include "propagate.h" IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { Propagate *prop = new Propagate(wxT("Propagate")); prop->Show(true); return true; }
In our example, we have a button on a panel. The panel is placed in a frame widget. We define a handler for all widgets.
event reached button class event reached panel class event reached frame class
We get this, when we click on the button. The event travels from the button to panel and to frame.
Try to omit some Skip()
methods and see what happens.
Vetoing events
Sometimes we need to stop processing an event. To do this, we call the method Veto()
.
veto.h
#include <wx/wx.h> class Veto : public wxFrame { public: Veto(const wxString& title); void OnClose(wxCloseEvent& event); };
veto.cpp
#include "veto.h" Veto::Veto(const wxString& title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(250, 130)) { Connect(wxEVT_CLOSE_WINDOW, wxCloseEventHandler(Veto::OnClose)); Centre(); } void Veto::OnClose(wxCloseEvent& event) { wxMessageDialog *dial = new wxMessageDialog(NULL, wxT("Are you sure to quit?"), wxT("Question"), wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION); int ret = dial->ShowModal(); dial->Destroy(); if (ret == wxID_YES) { Destroy(); } else { event.Veto(); } }
main.h
#include <wx/wx.h> class MyApp : public wxApp { public: virtual bool OnInit(); };
main.cpp
#include "main.h" #include "veto.h" IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { Veto *veto = new Veto(wxT("Veto")); veto->Show(true); return true; }
In our example, we process a wxCloseEvent
. This event is called, when we click the X button on the titlebar, press Alt+F4 or select close from the system menu. In many applications, we want to prevent from accidentally closing the window if we made some changes. To do this, we must connect the wxEVT_CLOSE_WINDOW
event type.
wxMessageDialog *dial = new wxMessageDialog(NULL, wxT("Are you sure to quit?"), wxT("Question"), wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION);
During the close event, we show a message dialog.
if (ret == wxID_YES) { Destroy(); } else { event.Veto(); }
Depending on the return value, we destroy the window, or veto the event. Notice that to close the window, we must call the Destroy()
method. By calling the Close()
method, we would end up in an endless cycle.
Window identifiers
Window identifiers are integers that uniquely determine the window identity in the event system. There are three ways to create a window id:
- Let the system automatically create an id.
- Use standard identifiers.
- Create our own id.
Each widget has an id parameter. This is a unique number in the event system. If we work with multiple widgets, we must differantiate among them.
wxButton(parent, -1) wxButton(parent, wxID_ANY)
If we provide -1 or wxID_ANY
for the ID parameter, we let wxWidgets automatically create an id for us. The automatically created id's are always negative, whereas user specified IDs must always be positive. We usually use this option when we do not need to change the widget state. For example a static text that will never be changed during the life of the application. We can still get the ID if we want. There is a method GetId()
, which will determine the id for us.
Standard identifiers should be used whenever possible. The identifiers can provide some standard graphics or behaviour on some platforms.
ident.h
#include <wx/wx.h> class Ident : public wxFrame { public: Ident(const wxString& title); };
ident.cpp
#include "ident.h" Ident::Ident(const wxString& title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(200, 150)) { wxPanel *panel = new wxPanel(this, -1); wxGridSizer *grid = new wxGridSizer(2, 3); grid->Add(new wxButton(panel, wxID_CANCEL), 0, wxTOP | wxLEFT, 9); grid->Add(new wxButton(panel, wxID_DELETE), 0, wxTOP, 9); grid->Add(new wxButton(panel, wxID_SAVE), 0, wxLEFT, 9); grid->Add(new wxButton(panel, wxID_EXIT)); grid->Add(new wxButton(panel, wxID_STOP), 0, wxLEFT, 9); grid->Add(new wxButton(panel, wxID_NEW)); panel->SetSizer(grid); Centre(); }
main.h
#include <wx/wx.h> class MyApp : public wxApp { public: virtual bool OnInit(); };
main.cpp
#include "main.h" #include "ident.h" IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { Ident *ident = new Ident(wxT("Identifiers")); ident->Show(true); return true; }
In our example we use standard identifiers on buttons. On Linux, the buttons have small icons.

In this chapter, we talked about events in wxWidgets.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论