Results 1 to 14 of 14

Thread: How get signal returnPressed in QTextEdit

  1. #1
    Join Date
    Dec 2009
    Posts
    9
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How get signal returnPressed in QTextEdit

    Hello,
    I am new user Qt and I have got a small problem. So, I don't know how can I resize QLineEdit (more lines) or how can I catch signal when enter has been pressed in QTextEdit?
    I am writing simple chat program and I want to create message box, but QLineEdit is too small field and I don't know how can I catch pressed enter in QTextEdit.
    Thanks for help.
    Goodbye

  2. #2
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How get signal returnPressed in QTextEdit

    Quote Originally Posted by Roszko View Post
    Hello,
    I am new user Qt and I have got a small problem. So, I don't know how can I resize QLineEdit (more lines)
    use QTextEdit
    or how can I catch signal when enter has been pressed in QTextEdit?
    Either subclass from QTextEdit and add you functionality in keyPressEvent() or install eventFilter without subclassing.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

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

    Roszko (29th December 2009)

  4. #3
    Join Date
    Dec 2009
    Posts
    9
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How get signal returnPressed in QTextEdit

    Can you get me a simple example with keyPressEvent() using?

  5. #4
    Join Date
    Jan 2006
    Posts
    368
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How get signal returnPressed in QTextEdit

    Qt Code:
    1. void my_class::keyPressEvent(QKeyEvent *e)
    2. {
    3. QTextCursor c = textCursor();
    4. switch (e->key()) {
    5. case Qt::Key_Escape:
    6. // whatever
    7. break;
    8.  
    9. case Qt::Key_Enter:
    10. case Qt::Key_Return:
    11. // whatever
    12. break;
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to elcuco for this useful post:

    Roszko (29th December 2009)

  7. #5
    Join Date
    Dec 2009
    Posts
    9
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How get signal returnPressed in QTextEdit

    ok, but must I send any connect and what signal must be supported?

  8. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How get signal returnPressed in QTextEdit

    No signal, you subclass the control and override the keyPressEvent. If you want a signal, you'll need to emit it from that method.

  9. The following user says thank you to squidge for this useful post:

    Xandareva (30th December 2009)

  10. #7
    Join Date
    Dec 2009
    Posts
    23
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How get signal returnPressed in QTextEdit

    I have one question. How can I move cursor to begin of QTextEdit? Why do I want to move cursor? Because if I press Enter, Message will send to server and all is ok, but I have one problem, Cursor has been in second line after pressed Enter ;/ and if I send second message (press Enter), message will send with word of new line. I would cursor will be in first line after pressed enter.
    Thanks for reply

  11. #8
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How get signal returnPressed in QTextEdit

    Look at moveCursor method.

  12. #9
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How get signal returnPressed in QTextEdit

    Quote Originally Posted by Xandareva View Post
    I have one question. How can I move cursor to begin of QTextEdit? Why do I want to move cursor? Because if I press Enter, Message will send to server and all is ok, but I have one problem, Cursor has been in second line after pressed Enter ;/ and if I send second message (press Enter), message will send with word of new line. I would cursor will be in first line after pressed enter.
    Thanks for reply
    It seems that when you send a message and pass keyPressEvent further (to the default implementation) so it is then inserting new line. You must "eat" the event and not pass it further: accept it (QEvent::accept()) and not pass it to the base class implementation.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  13. The following user says thank you to faldzip for this useful post:

    Xandareva (30th December 2009)

  14. #10
    Join Date
    Dec 2009
    Posts
    23
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How get signal returnPressed in QTextEdit

    I don't know what is wrong here:
    Qt Code:
    1. bool SimpleChatClient::eventFilter(QObject *obj, QEvent *event)
    2. {
    3. if (obj == message) {
    4. if (event->type() == QEvent::KeyPress) {
    5. QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
    6. switch(keyEvent->key()) {
    7. case Qt::Key_Enter:
    8. case Qt::Key_Return:
    9. sendMessage();
    10. break;
    11. }
    12. return false;
    13. } else {
    14. return QObject::eventFilter(obj, event);
    15. }
    16. } else {
    17. return QObject::eventFilter(obj, event);
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 
    And I don't know how to does it remake to work properly.
    Last edited by Xandareva; 30th December 2009 at 09:44.

  15. #11
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How get signal returnPressed in QTextEdit

    Quote Originally Posted by Xandareva View Post
    I don't know what is wrong here:
    Qt Code:
    1. bool SimpleChatClient::eventFilter(QObject *obj, QEvent *event)
    2. {
    3. if (obj == message) {
    4. if (event->type() == QEvent::KeyPress) {
    5. QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
    6. switch(keyEvent->key()) {
    7. case Qt::Key_Enter:
    8. case Qt::Key_Return:
    9. sendMessage();
    10. break;
    11. }
    12. return false;
    13. } else {
    14. return QObject::eventFilter(obj, event);
    15. }
    16. } else {
    17. return QObject::eventFilter(obj, event);
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 
    And I don't know how to does it remake to work properly.
    If you want to "eat" the key you must return true.

    P.S. This is explained in Qt docs (QObject::eventFilter()):
    In your reimplementation of this function, if you want to filter the event out, i.e. stop it being handled further, return true; otherwise return false.
    Last edited by faldzip; 30th December 2009 at 09:55.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  16. The following user says thank you to faldzip for this useful post:

    Xandareva (30th December 2009)

  17. #12
    Join Date
    Dec 2009
    Posts
    23
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How get signal returnPressed in QTextEdit

    but now I can't write in this box.

  18. #13
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How get signal returnPressed in QTextEdit

    return true in line 10. Only in case you call sendMessage(). In other places you can return QObject::eventFilter(obj, event);
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  19. The following user says thank you to faldzip for this useful post:

    Xandareva (30th December 2009)

  20. #14
    Join Date
    Dec 2009
    Posts
    23
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How get signal returnPressed in QTextEdit

    thx! It works.

Similar Threads

  1. signal for QTextEdit
    By bismitapadhy in forum Qt Programming
    Replies: 5
    Last Post: 25th December 2009, 13:23
  2. pthread instead QThread
    By brevleq in forum Qt Programming
    Replies: 8
    Last Post: 23rd December 2008, 07:16
  3. Connection of custon signals/slots
    By brevleq in forum Qt Programming
    Replies: 2
    Last Post: 23rd December 2008, 07:04
  4. QSpinBox and returnPressed() Signal
    By BeS in forum Newbie
    Replies: 2
    Last Post: 14th October 2006, 19:23

Tags for this Thread

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.