Publié le 30 août 2009 par Houssem
© 2005,2006 Houssem BDIOUI, in parts Mondrian Nuessle, et al.
This is free software, licensed under the GPL. Logo
This project is hosted by www.sourceforge.netand itssourceforge pages may be reached through this link.


The QConsole class is a custom widget that implements a basicconsole, completely written in C++ and relying on Qt (both 3.x& 4.x). It implements several features and is intended to beinherited from in order to have a "real" console for a specificscripting language, shell, etc... The class relies as much as possibleon the features offered by QTextEdit (from which it inherits) and thisimplied a very simple and light code.

QtclConsole inherits from QConsole and emulates a TCL shell in aQt application. So basically, the user is able to execute any TCLbuilt-in command and also any other user-defined commands created usingthe TCL C-API. These commands should represent all the features of agiven application, so the user will be able to execute them using theGUI and also in the embedded TCL shell. This is very useful in the EDA(Electronic Design Automation) field where most of the tools are usingTCL scripting and Qt as well.

The qtclconsolesample application shows how to embed the QtclConsole widget in a mainwindow. It is composed of  the Tcl implementation(qtclconsole.h, qtconsole.cpp), some custom Tcl commands (commands.hand commands.cpp), the commands class manager (commandsManager.h andcommandsManager.cpp) and the main.cpp file. 

QPyConsole inherits from QConsole and implements a Python shell in a Qtapplication, similar to QtclConsole. The qpyconsole sample applicationshows how to use the QPyConsole class in a qt application. For a realworld application, one would of course extend the embedded Pythoninterpreter with a complete API of the application that is to be madescriptable.

Some screenshots are available (TCL and Python).


File releasesas well as subversionaccess is provided by sourceforge. The latest release is qconsole 2.0.


  • QConsole is a custom widget that can be easily integratedin any interface.
    It is able to execute any command (implementated in child classes) andthen displays its result in blue or red whether it has failed or not.It also displays the stdout/stderr output produced by these commandswith the same colorization schema.

  • QConsole supports multi-line commands. In fact, when anincomplete command (e.g., unclosed braces) is validated, the cursor isset at the beginning of a new line.

  • QConsole supports specifying a different prompt.

  • Navigation:

    • Left and right keys to move the cursor in the editionzone (from the prompt to the end of the paragraph plus all the extralines in multi-line command mode).

      • Associated with the shift key, selection is thenpossible.

      • Associated with ctrl key, the cursor is moved betweenthe words.

      • Ctrl and shift keys can be used together.

    • If not in multi-line mode, up and down keys permit theuser to navigate in the commands' history as with advanced shells,otherwise they permit to navigate in the edition zone.

    • Home and end keys allow moving the cursor respectivelyjust after the prompt and at the end of the current line.

      • Associated with the shift key, selection is thenpossible.

  • Edition:

    • Simple key strokes work as expected and the 'enter' willplace the command into the history list, validate it and 'execute' itgiving us back the result (this is in case the command is complete,otherwise the cursor will move to a new line).

    • Pressing Ctrl + C key sequence will cancel the currentcommand and shows the prompt again.

    • Pressing the Tab key will try to autocomplete thecurrent command. If there are many possibilities, they should be allshown.

    • The Delete and Backspace keys will delete selected textor simply one character in the limits of the edition zone. The ctrl keycan be associated with them to delete whole words.

    • Copy & Paste are always enabled, but Cut isonly possible in the edition zone.

  • Mouse usage:

    • If the cursor is not placed in the edition zone, itsposition is undone.

    • Selection using the mouse is possible everywhere.

QtclConsole specific features

  • Adding custom Tcl commands is eased through the singletonclass commandsManager and its registerFunction() method. Besides, thisclass is responsible of holding the Tcl_Interp instance. Severalexamples showing its usage were implemented and can be tested in theconsole:

    • The help command shows all the custom TCL commandsregistered through the commandsManager class (as well as via theTclCallBack class).

    • The msgbox sample command was added in the commands.cppfile and implements the Qt QMessageBox::warning() static method.

    • The history command directly implemented in theQtclConsole class. It shows the history list of all validated commands.

    • The set_prompt command directly implemented in theQtclConsole class. It allows changing the prompt of the console.

    • The save_script command saves the last entered commandsin a script file.

QpyConsole specific features

QpyQconsole adds the capability to use an embedded python interpreterin a C++ Qt application. The features of QConsole are all availableincluding history, command completion (through the use of rlcompleter), etc.
Custom Python commands are available to communicate with the consolewidget:
  • clear- clears the console
  • reset - reset the interpreter and clear the console
  • save <filename> - save commands up to now ingiven file
  • load <filename> - load commands from givenfile
  • history - shows the history
  • quit- print information about quitting


Version 2.0 (releases March 23, 2006)

  • Supports Qt4.x:
    •   qt3support classes used since newQTextEdit/QTextCursor/QTextDocument classes are not flexible enough !
    •   Qt3.x still supported through usageof QT_VERSION macro
  • QConsole class:
    •  Pasting with middle mouse button possible evenwhen clicking  outside of the edition zone
    •  New internal replaceCurrentCommand() method forbetter modularity
    •  When navigating in history, multi-line commandswere not totally replaced
    •  Possibility to disable the stdout/stderrinterception
    •  Possibility to choose the colors of the cmd,stdout, stderr and completion (as well as the font)
    •  a new loadScript() method (useful for scriptinglanguages not providing this feature)
  • QTclconsole class:
    •   Use of tclnotify.* approach allowinginterpreting asynchrnous tcl commands as in:
      after 1000 {puts hello}

      Thanks to Ulrich Ring
  • New Python implementation (QPyConsole - thanks to MondrianNuessle).
  • General code review

Version 1.1 (released November 30, 2005)

  • commandsManager class:
    • The getInstance() method can take an already createdTcl_Interp parameter
    • removed the registerCommand() method
    • New unregisterFunction()
    • New set of registerVariable() methods to register C++vars as Tcl ones
    • Added support for "help messages" for functions andmethods (accessible  through the new 'help' command)
  • QConsole:
    • Fixed a bug previously allowing inserting text outside ofthe edition zone: Bug that can happen when holding the mouse buttonpressed outside the edition zone and typing something
    • Possibility to select words outside of the edition zoneusing double clicks
    • Overridden paste() removed : handled by the previous bugfix
    • Disabled the popup menu
    • New public execCommand(QString) method that executes aTcl command and dislpays back its result in the console
    • New saveScript() method that saves succeeded commands toa file
    • New completeCommand() method that gives suggestions tocomplete the current command
  • QtclConsole:
    • Redesigned into a singleton pattern
    • Implemented completeCommand() based on [ info commands ]
    • New Tcl saveScript command
  • New TclCallBack class that allowsregistring/unregistring  C++ methods as Tcl Callbacks:
    • Used for SetPrompt() and ShowHistory() in QtclConsole

Version 1.0 (released July 29. 2005)

  • Initial release version

Vous pourriez être intéressé par :

Retour à La Une de Logo Paperblog

Ces articles peuvent vous intéresser :

A propos de l’auteur

Houssem Voir son profil
Voir son blog

l'auteur n'a pas encore renseigné son compte l'auteur n'a pas encore renseigné son compte l'auteur n'a pas encore renseigné son compte

Dossier Paperblog