Results 1 to 5 of 5

Thread: QTextEdit textChanged() signal occurs way too frequently

  1. #1
    Join Date
    May 2012
    Posts
    16
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QTextEdit textChanged() signal occurs way too frequently

    Not sure why, but the QTextEdit textChanged() signal seems to be happening infinitely many times for me..is there some caveat to using this signal I didn't see in the docs somewhere?

    Basically I'm just trying to implement a poor man's terminal. It works, but the printf's I've thrown in along the way get printed many many times..the way I want to implement the terminal is like this: when text is changed in the terminal, it signals the textChanged signal, which is connected to the getCommand slot. getCommand sends the command to the serialComms method, which does some processing, then emits the serial signal with the processed output string as its parameter. finally, the output string is printed on the terminal. I'm pretty sure I went overboard on the signal generation, but this should still work, according to my research on QTextEdit

    my code is very messy and spread out across many files, so sorry if there's any confusion.

    ctor of the class that holds the 'terminal':
    Qt Code:
    1. terminal = new QTextEdit();
    2. terminal -> setText(">>");
    3. terminal -> setReadOnly(FALSE);
    4.  
    5. connect(terminal, SIGNAL(textChanged()), this, SLOT(getCommand()));
    To copy to clipboard, switch view to plain text mode 

    method for getting command from QTextEdit:
    Qt Code:
    1. void advancedTab::getCommand() {
    2. QString text = terminal -> toPlainText();
    3. QByteArray ba = text.toLocal8Bit();
    4. char *message = ba.data();
    5.  
    6.  
    7. printf("emitting signal inputComm.\n");
    8.  
    9. emit inputComm(message);
    10. }
    To copy to clipboard, switch view to plain text mode 

    method for printing out command on 'terminal':
    Qt Code:
    1. void advancedTab::outputComm ( char* rawMessage) {
    2. QString output = QString(rawMessage);
    3.  
    4. terminal -> setPlainText(rawMessage);
    5. printf("MESSAGE GOTTEN!\n");
    6. }
    To copy to clipboard, switch view to plain text mode 

    method for processing command:
    Qt Code:
    1. QString serialComms(char *stringy) {
    2. //processing stuff
    3. }
    To copy to clipboard, switch view to plain text mode 

    main method:
    Qt Code:
    1. //ops is an instance of some other class, in which the serialComms method lies
    2. //advanced is an instance of the class that houses the terminal
    3.  
    4. QObject::connect (&ops, SIGNAL(serial(char*)), advanced, SLOT(outputComm(char*)));
    5. QObject::connect(advanced, SIGNAL(inputComm(char*)), &ops, SLOT(serialComms(char *)));
    To copy to clipboard, switch view to plain text mode 

    note: this happened before to me, when I was just trying out a simple example with QTextEdit. This is why I think I may be missing some obvious detail about textChanged().
    Also, when commands are typed into the terminal carefully and no key is pressed forever, which would (I think) call the signal too many times.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextEdit textChanged() signal occurs way too frequently

    When does serial() get emitted?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    dsab123 (19th June 2012)

  4. #3
    Join Date
    May 2012
    Posts
    16
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTextEdit textChanged() signal occurs way too frequently

    wysota, you sure are quick, and you're pretty awesome for that.

    I think I've isolated the problem enough where it's not a problem with QTextEdit; i replaced the body of getCommands with a simple printf and it only printed out when I ty.

    To answer your question, serial should be emitted at the end of serialComms, my mistake for leaving that out. serialComms should really look like:
    Qt Code:
    1. QString serialComms(char *stringy) {
    2. //processing stuff
    3. emit serial (someString);
    4. }
    To copy to clipboard, switch view to plain text mode 


    Added after 14 minutes:


    well, I figured out the error.

    I was putting text back to into the terminal in outputComm, which would signal the textCHanged() signal all over again, thus beginning a neverending cycle..good job me.


    Added after 19 minutes:


    Ok, now I've run into some trouble.

    How can I add text to the QTextEdit without signaling the textChanged() signal?
    Last edited by dsab123; 18th June 2012 at 23:02.

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTextEdit textChanged() signal occurs way too frequently

    How can I add text to the QTextEdit without signaling the textChanged() signal?
    You can't... that would defeat the purpose of the signal. Unfortunately QTextEdit does not seem to have an equivalent of the QLineEdit::textEdited() signal.

    You can chose to ignore the signal with a simple boolean lock flag. Set it true before you write to the edit box from the code, write the data, and set it false. In the slot immediately return if the flag is true.

    You could try QObject::blockSignals() on the edit box unless other objects are depending on the edit's signals.

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

    dsab123 (19th June 2012)

  7. #5
    Join Date
    May 2012
    Posts
    16
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTextEdit textChanged() signal occurs way too frequently

    I think what I'll most likely end up doing is using QKeyEvent to react to the return key, and go from there.

    Thanks guys!

Similar Threads

  1. QTextEdit's textChanged() signal
    By bangqianchen in forum Qt Programming
    Replies: 1
    Last Post: 11th January 2010, 10:11
  2. QTextEdit, Highlighting and textChanged()
    By onamatic in forum Qt Programming
    Replies: 2
    Last Post: 12th November 2008, 11:44
  3. Subclassing QLineEdit and suppress signal textChanged
    By Lykurg in forum Qt Programming
    Replies: 2
    Last Post: 15th February 2008, 17:40
  4. No such signal QLineEdit::textChanged(QString&)
    By zorro68 in forum Qt Programming
    Replies: 4
    Last Post: 14th November 2007, 19:35
  5. textChanged signal issue
    By chaosgeorge in forum Qt Programming
    Replies: 3
    Last Post: 9th November 2006, 00:32

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.