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
      ex:
      @
      Prompt>
      @

    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.

    Initially
    @ 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 = ba.data();
    cout<<tstr<<endl;
    nline++;
    cmd->append("Pmt>");
    }
    @

    @
    bool eventFilter(QObject (obj, QEvent *eve)
    {
    if(eve->type()==QEvent::KeyPress){
    QKeyEvent keve = static_cast<QKeyEvent>(eve);
    if( keve->key()==Qt::Key_Return ||
    keve->key()==Qt::Key_Enter ) {
    str = cmd->toPlaintText();
    read(str);
    } else {
    keve->accept();
    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.
      @
      Prompt>
      |
      @

    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 :)

    qpconsole.h: http://pastebin.com/0tN70DqE
    qpconsole.cpp: http://pastebin.com/D9UCPsVE

    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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.