An open API service indexing awesome lists of open source software.

https://github.com/pedrolcl/consolewidget

lightweight Qt console widget based on QPlainTextEdit
https://github.com/pedrolcl/consolewidget

console consolewidget qt qt6 shell

Last synced: 29 days ago
JSON representation

lightweight Qt console widget based on QPlainTextEdit

Awesome Lists containing this project

README

          

# ConsoleWidget

This component was developed by George Apostolopoulos in 2020 as part of [**QDaq** - Qt-based Data Acquisition](https://gitlab.com/qdaq/qdaq)
and forked by Pedro López-Cabanillas in 2024 from [this GitHub repository](https://github.com/gapost/qconsolewidget).

A lightweight Qt console widget based on QPlainTextEdit providing
also a QIODevice interface.

It can be used as an interactive scripting terminal or a log window.
Features include standard console-like editing, command history,
formatted input, output & error streams.

TODO: syntax highlighting

You may find here two repositories using this component as a submodule:

* [netcat-qt](https://github.com/pedrolcl/netcat-qt)
* [fluidsynth-qt-gui](https://github.com/pedrolcl/fluidsynth-qt-gui)

## Usage

If you don't want to use submodules, then to find the library downstream with CMake, you need to use either
[pkg-config](https://cmake.org/cmake/help/latest/module/FindPkgConfig.html#module:FindPkgConfig) or
[find_package](https://cmake.org/cmake/help/latest/command/find_package.html#command:find_package)

```CMake
find_package(PkgConfig)
pkg_check_modules(consolewidget)
```

or

```CMake
find_package(consolewidget)
```

You need to build and install `consolewidget` first in some already well known prefix,
or you need to call cmake with [-DCMAKE_PREFIX_PATH=yourprefix](https://cmake.org/cmake/help/latest/variable/CMAKE_PREFIX_PATH.html),
or alternatively with -Dconsolewidget_DIR=yourbuilddir (without needing to install the component).

In C++, instantiate the widget and set it to input mode. Connect a QObject slot
to the consoleCommand signal to receive the user input.

```c++
ConsoleWidget w;
w.writeStdOut("enter command> ");
w.setMode(ConsoleWidget::Input);
QObject::connect(&w,SIGNAL(consoleCommand(QString)),MyQObject,SLOT(evalCommand(QString)))
...
MyQObjet::evalCommand(const QString& code)
{
...
}
```

Alternatively you can use a QTextStream to interact with ConsoleWidget:

```c++
ConsoleWidget w;
w.device()->open(QIODevice::ReadWrite); // open the console's QIODevice
QTextStream stream(w.device());
stream << "Hello World!" << endl; // output goes to the widget
```

The stream can also be used for input

```c++
stream << "Enter an integer n = " << flush;
int n;
stream.device()->waitForReadyRead();
stream >> n;
```
The call to ```waitForReadyRead()``` enters a local loop waiting for
the user to enter a command and hit return.

## License

SPDX-License-Identifier: MIT
Copyright (c) 2020 George Apostolopoulos
Copyright (c) 2024-2025 Pedro López-Cabanillas