Skip to content
Permalink
Browse files

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
  • Loading branch information
wonder
wonder committed May 23, 2008
1 parent 74c788d commit 46acd23ca93c24221dc1ae74d3d70a8dd724861f
Showing with 23 additions and 13 deletions.
  1. +9 −4 src/app/qgspythondialog.cpp
  2. +3 −1 src/app/qgspythondialog.h
  3. +11 −8 src/app/qgspythonutils.cpp
@@ -17,15 +17,14 @@
#include "qgspythondialog.h"
#include "qgspythonutils.h"

#include <QShowEvent>
#include <QCloseEvent>

QgsPythonDialog::QgsPythonDialog(QgisInterface* pIface, QWidget *parent)
: QDialog(parent)
{
setupUi(this);
mIface = pIface;

QgsPythonUtils::installConsoleHooks();
}

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

void QgsPythonDialog::showEvent(QShowEvent* event)
{
QDialog::showEvent(event);

QgsPythonUtils::installConsoleHooks();
}

void QgsPythonDialog::closeEvent(QCloseEvent* event)
{
QgsPythonUtils::uninstallConsoleHooks();
@@ -21,6 +21,7 @@

class QgisInterface;
class QCloseEvent;
class QShowEvent;

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

void closeEvent(QCloseEvent* event);

void showEvent(QShowEvent* event);

private:

QgisInterface* mIface;
@@ -97,6 +97,16 @@ void QgsPythonUtils::initPython(QgisInterface* interface)
runString(
"def qgis_except_hook(type, value, tb):\n"
" qgis_except_hook_msg(type, value, tb, None)\n");
runString(
"class QgisOutputCatcher:\n"
" def __init__(self):\n"
" self.data = ''\n"
" def write(self, stuff):\n"
" self.data += stuff\n"
" def get_and_clean_data(self):\n"
" tmp = self.data\n"
" self.data = ''\n"
" return tmp\n");

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

runString(
"class MyOutputCatcher:\n"
" def __init__(self):\n"
" self.data = ''\n"
" def write(self, stuff):\n"
" self.data += stuff\n");
runString("_old_stdout = sys.stdout\n");
runString("sys.stdout = MyOutputCatcher()\n");

runString("sys.stdout = QgisOutputCatcher()\n");
}

void QgsPythonUtils::uninstallConsoleHooks()

0 comments on commit 46acd23

Please sign in to comment.
You can’t perform that action at this time.