Results 1 to 4 of 4

Thread: fetching each char entered in Qtextedit??

  1. #1
    Join Date
    Dec 2006
    Posts
    103
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default fetching each char entered in Qtextedit??

    I want to retrieve the char as and when they are entered in the QTextedit box. can anyone help me how to do it? plz specify some relevant code if possible.

    Thanks
    I worked on windows Xp with Qt 4.2.2(Open Source Version) and MinGw
    now i am trying the same things on Fedora Core 5 (linux-gcc) and Qt 4.2.2 open source edition.

  2. #2
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Smile Re: fetching each char entered in Qtextedit??

    Hi Shuchi,

    Do the following:


    Qt Code:
    1. in header file
    2. protected:
    3. bool eventFilter(QObject *target, QEvent *event);
    4. private:
    5. QTextEdit * m_pTxBox;
    6.  
    7. in cpp file:
    8. in constructor:
    9. m_pTxBox->installEventFilter(this);
    10.  
    11.  
    12. bool myWindow::eventFilter(QObject *target, QEvent *event)
    13. {
    14. if (target == m_pTxBox)
    15. {
    16. if (event->type() == QEvent::KeyPress)
    17. {
    18. QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    19. std::string str = keyEvent->text().toStdString();
    20. char ch = str[0];
    21. emit keyPressed(ch); // emit signal keyPressed with char entered
    22. }
    23. }
    24. return QWidget::eventFilter(target, event);
    25. }
    To copy to clipboard, switch view to plain text mode 

    or you can make your own class like:

    Qt Code:
    1. class TextEdit : public QTextEdit
    2. {
    3. protected:
    4. void keyPressEvent(QKeyEvent *event);
    5. signals:
    6. void keyPressed(int);
    7. ...
    8. }
    9.  
    10. void TextEdit::keyPressEvent(QKeyEvent *event)
    11. {
    12. std::string str = event->text().toStdString();
    13. int ch = str[0];
    14. emit keyPressed(ch);
    15.  
    16. QTextEdit::keyPressEvent(event);
    17. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 16th February 2007 at 11:16. Reason: changed [html] to [code]

  3. The following user says thank you to rajesh for this useful post:

    Shuchi Agrawal (16th February 2007)

  4. #3
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: fetching each char entered in Qtextedit??

    Hi Shuchi,
    Is your problem solved? Which one you used?

    installEventFilter or reimplementation of keyPressEvent?
    Or your problem was something else?

    Regards
    Rajesh

  5. The following user says thank you to rajesh for this useful post:

    Shuchi Agrawal (17th February 2007)

  6. #4
    Join Date
    Dec 2006
    Posts
    103
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: fetching each char entered in Qtextedit??

    hi,
    i simply fetched the last char of the string returned by "toPlainText()".
    but thanks for ur reply.
    I worked on windows Xp with Qt 4.2.2(Open Source Version) and MinGw
    now i am trying the same things on Fedora Core 5 (linux-gcc) and Qt 4.2.2 open source edition.

Similar Threads

  1. unable to save QCStrings properly in a buffer
    By nass in forum Qt Programming
    Replies: 13
    Last Post: 15th November 2006, 20:49
  2. Current char in QTextEdit
    By jlbrd in forum Qt Programming
    Replies: 1
    Last Post: 12th August 2006, 09:30
  3. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 06:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.