Important: Please read the Qt Code of Conduct -

Command terminal using QTextEdit

  • Hi,
    I am trying to create a terminal kind of interface using QTextEdit by which i would like to execute commands on standard input terminal.
    I have a way to send the command from Qt app to standard input.
    now my tasks are

    1. setting up a command promt

    2. After every return and enter press new prompt has to be shown.

    3. cursor should always wait at Prompt > and not move back from Prompt> when i press backspace , though i should be able to remove the other chars when i type(this is really needed)

    following the code i am using to achieve the same, except the 3 point.

    @ cmd->append("Pmt>"); @

    void read(QString str)
    static int nline =0;
    QStringList lines = str.split("\n");
    QString line = lines[nline];
    QByteArray ba = line.toLocal8bit();
    char *tstr =;

    bool eventFilter(QObject (obj, QEvent *eve)
    QKeyEvent keve = static_cast<QKeyEvent>(eve);
    if( keve->key()==Qt::Key_Return ||
    keve->key()==Qt::Key_Enter ) {
    str = cmd->toPlaintText();
    } else {
    str = cmd->toPlainText();
    }else {
    return QObject::evenFilter(obj,eve);

    problems i am facing:

    1. cursor is waiting below the promt after entering the first command ex.

    2. The Promt> is getting erased by when i use backspace(need help here)

  • Maybe you can use a QString as buffer for your command? Then you can print Promt> + QString to the QTextEdit.

  • Hey, I've once done such a component. Here's the code, maybe you can steal something useful :)


    If a command is entered, the signal command(QString, QStringList) is emitted, where the QString is the command and QStringList the (optional) parameters. Use the print(QString) function to output something on the console. If a prompt shall be printed where the user may enter a new command, call prepareCommandLine()

Log in to reply