-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqtdebug.py
81 lines (70 loc) · 2.04 KB
/
qtdebug.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
qtdbg.py
A simple PyQt output widget.
It's main use case is to serve as an output console, for debugging or
other purposes.
It provides a file-like interface for ease of integration with other
python features such as the logging module, on top of a slightly
pre-set QTextEdit widget.
Since it inherits QTextEdit directly, all of the widget's methods are
available directly for further customization or GUI integration.
Tested on:
- Python 3.2, PyQt4, win7
Author: raphi <[email protected]>
Created: 08/01/2013
Version: 1.0
https://gist.github.com/raphigaziano/4494398
>>> import sys
>>> app = QtGui.QApplication(sys.argv)
>>> widget = QDbgConsole()
>>> widget.write("TestMessage, yay \o/")
>>> widget.seek(0)
0
>>> widget.read()
'TestMessage, yay \\\o/'
>>> widget.seek(0)
0
>>> s = widget.read(4)
>>> assert(len(s) == 4)
>>> print(s)
Test
"""
from io import StringIO
from PyQt5 import QtGui
from PyQt5.QtWidgets import QTextEdit
class QDbgConsole(QTextEdit):
'''
A simple QTextEdit, with a few pre-set attributes and a file-like
interface.
'''
# Feel free to adjust those
WIDTH = 480
HEIGHT = 320
def __init__(self, parent=None, w=WIDTH, h=HEIGHT):
super(QDbgConsole, self).__init__(parent)
self._buffer = StringIO()
self.resize(w, h)
self.setReadOnly(True)
### File-like interface ###
###########################
def write(self, msg):
'''Add msg to the console's output, on a new line.'''
self.insertPlainText(msg)
# Autoscroll
self.moveCursor(QtGui.QTextCursor.End)
self._buffer.write(msg)
# Most of the file API is provided by the contained StringIO
# buffer.
# You can redefine any of those methods here if needed.
def __getattr__(self, attr):
'''
Fall back to the buffer object if an attribute can't be found.
'''
return getattr(self._buffer, attr)
# # -- Testing
# if __name__ == '__main__':
# import doctest
#
# doctest.testmod(verbose=True)