5

I'm developing a QGIS plugin to search classes and classes methods of QGIS. The dir equivalent option it was answered in:

How can I access to elements in dir(some_class) of PyQGIS without a dictionary in my code?

As an example, the next image shows the search for methods with 'geo' pattern in QgsVectorLayer class and the result was as it expected.

enter image description here

When I tried out the equivalent help option at the plugin, with the next fixed example (see snipped code) for the 'geometryType' method of QgsVectorLayer, result it was always printed at the Python Console (not into the QTextBrowser object); as it can be observed at the next image.

.
.
.
    def getClasses(self):

        if self.dlg.checkBox.isChecked() == False:
.
.
.
        else:

            message = help(QgsVectorLayer.geometryType)
            txtBox = self.dlg.textFeedback
            txtBox.setText(message)

    def run(self):
        """Run method that performs all the real work"""
.
.
.

Other arbitrary messages were printed as I expected so, it was pointed out that this part of the code works well.

enter image description here

My question is:

There is any option to redirect the text of the help command to the QTextBrowser object or it is impossible?

Editing Note

I did a search based at the links supplied by Joseph:

Redirect stdout to a file in Python?

Unable to restore stdout to original (only to terminal)

and I can build this code:

import sys

#save a reference to the Python Console of QGIS before reassignment
oldstdout = sys.stdout

#open an arbitrary file in disc to reassign the standard output
sys.stdout = open('file', 'w')

#write to the open file
print help(QgsVectorLayer.geometryType)

#force the flush to the open file
sys.stdout.flush()

#reassign the standard output to the Python Console
sys.stdout = oldstdout

It works; as it can be observed at the next image (and the Python Console can be used again as standard output). However, I would like to improve the code by using a tempfile in memory before writing it to the QTextBrowser.

enter image description here

4
  • Does a print statement on your message variable show nothing?
    – artwork21
    Commented May 16, 2016 at 12:22
  • @artwork21 The print statement with the name 'message', as output of help command, is printing on the Python Console instead of QTextBrowser. However, output of dir command can be printed on this Qt Object because it is a list of elements. I would like to redirect the standard output of help command (Python Console) to the QTextBrowser.
    – xunilk
    Commented May 16, 2016 at 16:00
  • @Joseph Thanks a lot for the links. I'll see them later.
    – xunilk
    Commented May 16, 2016 at 16:01

1 Answer 1

4

By using a temporary file, as reassignment of the standard output (Python Console), next code works.

...
import sys, os
import tempfile as tmpf
...
        else:

            #save a reference to the Python Console of QGIS before reassignment
            oldstdout = sys.stdout

            #creating a temporary file
            file = tmpf.NamedTemporaryFile(delete=False)

            file_name = file.name

            #reassigning the standard output to temporary file
            sys.stdout = file

            #printing to temporary file
            help(QgsVectorLayer.geometryType)

            #force the flush to temporary file
            sys.stdout.flush()

            file.close()

            #reassigning the standard output to the Python Console
            sys.stdout = oldstdout

            #opening temporary file
            tmp_file = open(file_name, 'r')

            data = tmp_file.read()

            #writing to QTextBrowser
            txtBox = self.dlg.textFeedback
            txtBox.setText(data)

            tmp_file.close()

            #erasing temporary file
            os.unlink(file.name)

    def run(self):
        """Run method that performs all the real work"""
...

as it can be observed at the next image:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.