Skip to content

Commit 46acd23

Browse files
author
wonder
committedMay 23, 2008
One more fix for redirecting output when python console is open
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@8503 c8812cc2-4d05-0410-92ff-de0c093fc19c

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed
 

‎src/app/qgspythondialog.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@
1717
#include "qgspythondialog.h"
1818
#include "qgspythonutils.h"
1919

20+
#include <QShowEvent>
2021
#include <QCloseEvent>
2122

2223
QgsPythonDialog::QgsPythonDialog(QgisInterface* pIface, QWidget *parent)
2324
: QDialog(parent)
2425
{
2526
setupUi(this);
2627
mIface = pIface;
27-
28-
QgsPythonUtils::installConsoleHooks();
2928
}
3029

3130
QgsPythonDialog::~QgsPythonDialog()
@@ -46,8 +45,7 @@ void QgsPythonDialog::on_edtCmdLine_returnPressed()
4645
// we're using custom hooks for output and exceptions to show output in console
4746
if (QgsPythonUtils::runStringUnsafe(command))
4847
{
49-
QgsPythonUtils::evalString("sys.stdout.data", output);
50-
QgsPythonUtils::runString("sys.stdout.data = ''");
48+
QgsPythonUtils::evalString("sys.stdout.get_and_clean_data()", output);
5149
QString result = QgsPythonUtils::getResult();
5250
// escape the result so python objects display properly and
5351
// we can still use html output to get nicely formatted display
@@ -74,6 +72,13 @@ void QgsPythonDialog::on_edtCmdLine_returnPressed()
7472
txtHistory->ensureCursorVisible();
7573
}
7674

75+
void QgsPythonDialog::showEvent(QShowEvent* event)
76+
{
77+
QDialog::showEvent(event);
78+
79+
QgsPythonUtils::installConsoleHooks();
80+
}
81+
7782
void QgsPythonDialog::closeEvent(QCloseEvent* event)
7883
{
7984
QgsPythonUtils::uninstallConsoleHooks();

‎src/app/qgspythondialog.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
class QgisInterface;
2323
class QCloseEvent;
24+
class QShowEvent;
2425

2526
class QgsPythonDialog : public QDialog, private Ui::QgsPythonDialog
2627
{
@@ -40,7 +41,8 @@ class QgsPythonDialog : public QDialog, private Ui::QgsPythonDialog
4041
protected:
4142

4243
void closeEvent(QCloseEvent* event);
43-
44+
void showEvent(QShowEvent* event);
45+
4446
private:
4547

4648
QgisInterface* mIface;

‎src/app/qgspythonutils.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,16 @@ void QgsPythonUtils::initPython(QgisInterface* interface)
9797
runString(
9898
"def qgis_except_hook(type, value, tb):\n"
9999
" qgis_except_hook_msg(type, value, tb, None)\n");
100+
runString(
101+
"class QgisOutputCatcher:\n"
102+
" def __init__(self):\n"
103+
" self.data = ''\n"
104+
" def write(self, stuff):\n"
105+
" self.data += stuff\n"
106+
" def get_and_clean_data(self):\n"
107+
" tmp = self.data\n"
108+
" self.data = ''\n"
109+
" return tmp\n");
100110

101111
// hook for python console so all output will be redirected
102112
// and then shown in console
@@ -135,15 +145,8 @@ void QgsPythonUtils::installConsoleHooks()
135145
{
136146
runString("sys.displayhook = console_display_hook\n");
137147

138-
runString(
139-
"class MyOutputCatcher:\n"
140-
" def __init__(self):\n"
141-
" self.data = ''\n"
142-
" def write(self, stuff):\n"
143-
" self.data += stuff\n");
144148
runString("_old_stdout = sys.stdout\n");
145-
runString("sys.stdout = MyOutputCatcher()\n");
146-
149+
runString("sys.stdout = QgisOutputCatcher()\n");
147150
}
148151

149152
void QgsPythonUtils::uninstallConsoleHooks()

0 commit comments

Comments
 (0)
Please sign in to comment.