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. QTextEdit , send messgae to Application
Forum Updated to NodeBB v4.3 + New Features

QTextEdit , send messgae to Application

Scheduled Pinned Locked Moved General and Desktop
15 Posts 5 Posters 5.2k Views 1 Watching
  • 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

    I am trying to send some text from QTextEdit to standard output when the enter key is pressed,.
    But the text is not getting displayed in both text box and output and the enter is also does not seem to be detected.

    whats wrong in the code?

    following the code
    MainWidget.cpp

    @
    #include "MainWidget.h"
    #include<QString>
    QString str;
    MainWidget::MainWidget()
    {
    QLayout *layout = new QVBoxLayout();
    cmdWin = new CommandWindow;
    this->setLayout(layout);
    layout->addWidget(cmdWin);
    }

    CommandWindow::CommandWindow()
    {
    str = this->toPlainText();
    }

    void CommandWindow:: read()
    {
    qDebug()<< "str" <<str;
    }

    void CommandWindow::keyPressEvent(QKeyEvent *ke)
    {
    qDebug()<<"in KeyPress";

     if(ke->key()== Qt::Key_Enter)
    {
        qDebug()<<"read is pressed";
        read();
    }
    

    }
    @

    main.cpp
    @
    #include "MainWidget.h"

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    qDebug()<< "Main Window:";
    MainWidget mainWidget;
    mainWidget.show();

    return app.exec();
    

    }
    @
    MainWidget.h
    @
    #ifndef MAINWIDGET_H
    #define MAINWIDGET_H

    #include<QApplication>
    #include <QtDebug>
    #include <QTextEdit>
    #include <QVBoxLayout>
    #include <QKeyEvent>

    class CommandWindow;
    class MainWidget : public QWidget
    {
    Q_OBJECT
    public:
    MainWidget();
    private:
    CommandWindow *cmdWin;

    };

    class CommandWindow : public QTextEdit
    {
    public:
    CommandWindow();
    void read();
    private:
    QTextEdit *textbox;
    protected:
    void keyPressEvent(QKeyEvent *ke);

    };
    #endif // MAINWIDGET_H

    @

    1 Reply Last reply
    0
    • mrdebugM Offline
      mrdebugM Offline
      mrdebug
      wrote on last edited by
      #2

      There is a ReturnPressed event. Have you tried it?

      Need programmers to hire?
      www.labcsp.com
      www.denisgottardello.it
      GMT+1
      Skype: mrdebug

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vezprog
        wrote on last edited by
        #3

        Have you run this in debug and stepped through the code to see if your getting a key event when enter is pressed at least?

        Ive always used:
        @
        if (event->key()== Qt::Key_Enter || event->key() == Qt::Key_Return){
        // do something...
        }
        @

        or you could use an event filter and check if the event is a key event using installEventFilter(true) and using the eventFilter function...then check the keys from there (will most likely be a lot slower though since your going to be picking up event event in the form).

        1 Reply Last reply
        0
        • D Offline
          D Offline
          Dcqt
          wrote on last edited by
          #4

          Thanks for suggestion, it was detecting only one enter key before ,now after adding "Qt::Key_Return" detecting both.
          But still there is a problem.
          when i press any key that is not displayed in the textwindow and str does not contain the keys that i entered.
          below is the debug sample from gdb.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            Dcqt
            wrote on last edited by
            #5

            @
            (gdb) p str
            $1 = {static null = {<No data fields>}, static shared_null = {ref = {
            _q_value = 0}, alloc = 0, size = 0, data = 0x0, clean = 0,
            simpletext = 0, righttoleft = 0, asciiCache = 0, capacity = 0,
            reserved = 0, array = {0}}, static shared_empty = {ref = {_q_value = 1},
            alloc = 0, size = 0, data = 0x12, clean = 0, simpletext = 0,
            righttoleft = 0, asciiCache = 0, capacity = 0, reserved = 0, array = {0}},
            d = 0x0, static codecForCStrings = 0x0}
            (gdb) r
            Starting program: /home/skylab/exercise/Text/Text
            [Thread debugging using libthread_db enabled]
            [New Thread 0xb578ab70 (LWP 2040)]
            [New Thread 0xb4dffb70 (LWP 2041)]
            Main Window:

            Breakpoint 1, MainWidget::MainWidget (this=0xbffff284) at MainWidget.cpp:7
            7 cmdWin = new CommandWindow;
            (gdb) s
            CommandWindow::CommandWindow (this=0xbffff284) at MainWidget.cpp:12
            12 CommandWindow::CommandWindow()
            (gdb) s
            14 str = this->toPlainText();
            (gdb) p str
            $2 = {static null = {<No data fields>}, static shared_null = {ref = {
            _q_value = 138}, alloc = 0, size = 0, data = 0x804e232, clean = 0,
            simpletext = 0, righttoleft = 0, asciiCache = 0, capacity = 0,
            reserved = 0, array = {0}}, static shared_empty = {ref = {_q_value = 2},
            alloc = 0, size = 0, data = 0xb74e709e, clean = 0, simpletext = 0,
            righttoleft = 0, asciiCache = 0, capacity = 0, reserved = 0, array = {0}},
            d = 0x804e220, static codecForCStrings = 0x0}
            (gdb) s
            toPlainText (this=<optimized out>)
            at /usr/local/qt4/include/QtGui/qtextedit.h:190
            190 { return document()->toPlainText(); }
            (gdb) s
            [Thread 0xb578ab70 (LWP 2040) exited]
            in KeyPress 74
            Enter 16777221
            in KeyPress 75
            Enter 16777221
            in KeyPress 74
            Enter 16777221
            in KeyPress 16777220
            Enter 16777221
            read is pressed
            str ""

            @

            1 Reply Last reply
            0
            • D Offline
              D Offline
              Dcqt
              wrote on last edited by
              #6

              I have added " setText("Hello"); " in below function , now the str is cntaining "Hello", i observed that the cursor is not moving when i type some characters, any idea whats the problem.?

              @CommandWindow::CommandWindow()
              {
              setText("Hello");
              str = this->toPlainText();
              }@

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Skh1002
                wrote on last edited by
                #7

                Hi Dcqt. In your keyPressEvent, if you want the widget to process the captured key, you should call the superclass' implementation of the handler. Otherwise, your keys are eaten discarded.

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  Dcqt
                  wrote on last edited by
                  #8

                  Hi Skh1002,

                  I really dont understand what is superclass, can you give some sample or reference please...

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    Skh1002
                    wrote on last edited by
                    #9

                    I'm sorry to have confused you, I used Objective-C terminology. Well, the basic key press processing is implemented in your QTextEdit parent class and although you do your own custom processing by reimplementing the keyPressEvent, you still want that basic processing to happen. However, since you have reimplemented (overloaded) the handler method, the original one is no longer called. The only way to to that is to manually call the QTextEdit::keyPressEvent() in your implementation. Something like this:

                    @
                    void CommandWindow::keyPressEvent(QKeyEvent *ke)
                    {
                    qDebug()<<"in KeyPress";

                    QTextEdit::keyPressEvent(ke);
                     if(ke->key()== Qt::Key_Enter)
                    {
                        qDebug()<<"read is pressed";
                        read();
                    }
                    

                    }
                    @

                    Edit: I also think that you need to call your read() method after the default processing of the key press. I edited the code to reflect that. Also, in your read() method you send the string to output, but you never seem to update it. I guess, it should be updated every time your widget registers a key press, that is inside your keyPressEvent().

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      Dcqt
                      wrote on last edited by
                      #10

                      Hi,
                      I managed to do it with keyPressEvent.
                      my next task is to make the QTextEdit as a terminal where we enter some commands.(custom).

                      i need to show a promt there like
                      @
                      pmt>
                      @

                      where i enter commands.

                      i tried with QTextEdit::setText("pmt>") and QTextEdit::appened("pmt>").

                      when i press backspace it is removing the promt, how will i control my cursor not to remove the promt.
                      also how will i retain the previously entered commands
                      is there any example or reference would be help full

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        Skh1002
                        wrote on last edited by
                        #11

                        I think you've got the basic set-up for it. You are already filtering key presses. A logical extension would be to check for the Backspace key as well, but to keep the previously entered commands and the prompt in the widget, you may need to filter out cursor movement keys and count characters entered by the user on the current line and eat the Backspace key (by not calling the parent's keyPressEvent() implementation) if this count is at zero.

                        I can't think of a good example right now, but it does not seem to be very hard to implement. By the way, if you don't need rich text capabilities, you may want to use QPlainTextEdit instead.

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          Dcqt
                          wrote on last edited by
                          #12

                          Hi Skh1002,

                          I try to use it the following way but i am not able stop the control from eating the character .
                          how to stop the control if the length is 4 and key is backspace.

                          The promt length is 4
                          @ Pmt> @

                          @
                          bool eventFilter(QObject (obj, QEvent *eve)
                          {
                          if(eve->type()==QEvent::KeyPress){
                          QKeyEvent keve = static_cast<QKeyEvent>(eve);
                          if(cmd->toPlainText().length()==4 && keve->key()==Qt::Key_Backspace ){
                          qDebug()<<"backspcae pressed";
                          return 0;
                          }
                          .....
                          }

                          @

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            Skh1002
                            wrote on last edited by
                            #13

                            Hi Dcqt,
                            Unfortunately, I could not respond earlier and I don't know whether or not you still need any help. But for the record, first of all, you do not have to install an event filter if you only need to register key presses. The keyPressEvent() should be enough. If you need to process other events as well, though, you may need to reimplement eventFilter().

                            If you do it with the eventFilter(), you need to indicate to the calling framework that you want to stop processing the captured event by returning true. In that case, it will be discarded. If you return false (or zero, as you do in your snippet), then the event will be propagated further and processed by your widget normally.

                            1 Reply Last reply
                            0
                            • D Offline
                              D Offline
                              Dcqt
                              wrote on last edited by
                              #14

                              Thanks Skh1002 for the reply.

                              I have solved all my problems and i used KeyPressEvent and handled all the keys required.
                              for the prompt i used cursor.position and compared with the length required.

                              Thanks for returning again.

                              1 Reply Last reply
                              0
                              • V Offline
                                V Offline
                                vittalonline
                                wrote on last edited by
                                #15

                                bool eventFilter( QObject *dist, QEvent *event )
                                {
                                if( event->type() == QEvent::KeyPress )
                                {
                                .......
                                //Here "dist" is required object, i.e, TextEdit etc.
                                }

                                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