Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Command terminal using QTextEdit
QtWS25 Last Chance

Command terminal using QTextEdit

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 3.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dcqt
    wrote on last edited by
    #1

    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)

    1 Reply Last reply
    0
    • G Offline
      G Offline
      grastvei
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DerManu
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved