Skip to content

Instantly share code, notes, and snippets.

@rdswift
Created July 4, 2021 01:01
Show Gist options
  • Save rdswift/c6943bb6767878a86506915e68807745 to your computer and use it in GitHub Desktop.
Save rdswift/c6943bb6767878a86506915e68807745 to your computer and use it in GitHub Desktop.
Log QEvents to File

A simple class to help logging QEvents to a file

To use the QEventFileLogger() in your application:

  1. Include the following definition in the object that you want to log:
    def eventFilter(self, object, event):
        """Process selected events.
        """
        evtype = event.type()
        self.event_logger.log_event(evtype)
        return False
  1. In the imports section of the module, include the line:
from event_file_logger import QEventFileLogger
  1. In the __init__() section of the object, include the lines:
        self.installEventFilter(self)
        self.event_logger = QEventFileLogger("my_filename.log")

# -*- coding: utf-8 -*-
#
# Copyright (C) 2021 Bob Swift
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
QEVENTS = {
0: "QEvent.None",
1: "QEvent.Timer",
2: "QEvent.MouseButtonPress",
3: "QEvent.MouseButtonRelease",
4: "QEvent.MouseButtonDblClick",
5: "QEvent.MouseMove",
6: "QEvent.KeyPress",
7: "QEvent.KeyRelease",
8: "QEvent.FocusIn",
9: "QEvent.FocusOut",
10: "QEvent.Enter",
11: "QEvent.Leave",
12: "QEvent.Paint",
13: "QEvent.Move",
14: "QEvent.Resize",
17: "QEvent.Show",
18: "QEvent.Hide",
19: "QEvent.Close",
21: "QEvent.ParentChange",
22: "QEvent.ThreadChange",
23: "QEvent.FocusAboutToChange",
24: "QEvent.WindowActivate",
25: "QEvent.WindowDeactivate",
26: "QEvent.ShowToParent",
27: "QEvent.HideToParent",
31: "QEvent.Wheel",
33: "QEvent.WindowTitleChange",
34: "QEvent.WindowIconChange",
35: "QEvent.ApplicationWindowIconChange",
36: "QEvent.ApplicationFontChange",
37: "QEvent.ApplicationLayoutDirectionChange",
38: "QEvent.ApplicationPaletteChange",
39: "QEvent.PaletteChange",
40: "QEvent.Clipboard",
43: "QEvent.MetaCall",
50: "QEvent.SockAct",
51: "QEvent.ShortcutOverride",
52: "QEvent.DeferredDelete",
60: "QEvent.DragEnter",
61: "QEvent.DragMove",
62: "QEvent.DragLeave",
63: "QEvent.Drop",
68: "QEvent.ChildAdded",
69: "QEvent.ChildPolished",
71: "QEvent.ChildRemoved",
74: "QEvent.PolishRequest",
75: "QEvent.Polish",
76: "QEvent.LayoutRequest",
77: "QEvent.UpdateRequest",
78: "QEvent.UpdateLater",
82: "QEvent.ContextMenu",
83: "QEvent.InputMethod",
87: "QEvent.TabletMove",
88: "QEvent.LocaleChange",
89: "QEvent.LanguageChange",
90: "QEvent.LayoutDirectionChange",
92: "QEvent.TabletPress",
93: "QEvent.TabletRelease",
96: "QEvent.IconDrag",
97: "QEvent.FontChange",
98: "QEvent.EnabledChange",
99: "QEvent.ActivationChange",
100: "QEvent.StyleChange",
101: "QEvent.IconTextChange",
102: "QEvent.ModifiedChange",
103: "QEvent.WindowBlocked",
104: "QEvent.WindowUnblocked",
105: "QEvent.WindowStateChange",
106: "QEvent.ReadOnlyChange",
109: "QEvent.MouseTrackingChange",
110: "QEvent.ToolTip",
111: "QEvent.WhatsThis",
112: "QEvent.StatusTip",
113: "QEvent.ActionChanged",
114: "QEvent.ActionAdded",
115: "QEvent.ActionRemoved",
116: "QEvent.FileOpen",
117: "QEvent.Shortcut",
118: "QEvent.WhatsThisClicked",
120: "QEvent.ToolBarChange",
121: "QEvent.ApplicationActivate",
122: "QEvent.ApplicationDeactivate",
123: "QEvent.QueryWhatsThis",
124: "QEvent.EnterWhatsThisMode",
125: "QEvent.LeaveWhatsThisMode",
126: "QEvent.ZOrderChange",
127: "QEvent.HoverEnter",
128: "QEvent.HoverLeave",
129: "QEvent.HoverMove",
131: "QEvent.ParentAboutToChange",
132: "QEvent.WinEventAct",
150: "QEvent.EnterEditFocus",
151: "QEvent.LeaveEditFocus",
155: "QEvent.GraphicsSceneMouseMove",
156: "QEvent.GraphicsSceneMousePress",
157: "QEvent.GraphicsSceneMouseRelease",
158: "QEvent.GraphicsSceneMouseDoubleClick",
159: "QEvent.GraphicsSceneContextMenu",
160: "QEvent.GraphicsSceneHoverEnter",
161: "QEvent.GraphicsSceneHoverMove",
162: "QEvent.GraphicsSceneHoverLeave",
163: "QEvent.GraphicsSceneHelp",
164: "QEvent.GraphicsSceneDragEnter",
165: "QEvent.GraphicsSceneDragMove",
166: "QEvent.GraphicsSceneDragLeave",
167: "QEvent.GraphicsSceneDrop",
168: "QEvent.GraphicsSceneWheel",
169: "QEvent.KeyboardLayoutChange",
170: "QEvent.DynamicPropertyChange",
171: "QEvent.TabletEnterProximity",
172: "QEvent.TabletLeaveProximity",
173: "QEvent.NonClientAreaMouseMove",
174: "QEvent.NonClientAreaMouseButtonPress",
175: "QEvent.NonClientAreaMouseButtonRelease",
176: "QEvent.NonClientAreaMouseButtonDblClick",
177: "QEvent.MacSizeChange",
178: "QEvent.ContentsRectChange",
181: "QEvent.GraphicsSceneResize",
182: "QEvent.GraphicsSceneMove",
183: "QEvent.CursorChange",
184: "QEvent.ToolTipChange",
186: "QEvent.GrabMouse",
187: "QEvent.UngrabMouse",
188: "QEvent.GrabKeyboard",
189: "QEvent.UngrabKeyboard",
192: "QEvent.StateMachineSignal",
193: "QEvent.StateMachineWrapped",
194: "QEvent.TouchBegin",
195: "QEvent.TouchUpdate",
196: "QEvent.TouchEnd",
197: "QEvent.NativeGesture",
198: "QEvent.Gesture",
199: "QEvent.RequestSoftwareInputPanel",
200: "QEvent.CloseSoftwareInputPanel",
202: "QEvent.GestureOverride",
203: "QEvent.WinIdChange",
204: "QEvent.ScrollPrepare",
205: "QEvent.Scroll",
206: "QEvent.Expose",
207: "QEvent.InputMethodQuery",
208: "QEvent.OrientationChange",
209: "QEvent.TouchCancel",
212: "QEvent.PlatformPanel",
214: "QEvent.ApplicationStateChange",
217: "QEvent.PlatformSurface",
219: "QEvent.TabletTrackingChange",
}
class QEventFileLogger():
def __init__(self, filename, overwrite=True):
self.counter = 0
self.filename = filename
if overwrite:
with open(self.filename, 'w', encoding='utf8') as f:
pass
def log_event(self, event_id):
event_title = QEVENTS[event_id] if event_id in QEVENTS else "Unknown Event"
self.counter += 1
with open(self.filename, 'a', encoding='utf8') as f:
f.write("{0:06d}: {1} ({2})\n".format(self.counter, event_title, event_id,))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment