changing QTextEdit content during 'onTextChange()' signal

0

We have some code:

void MainWindow::textChangedListener(){
    QTextEdit* dock = qobject_cast<QTextEdit *>(QObject::sender());
    dock->setText("asd");
}

And a signal:

cout << connect(it->silaTextEdit, SIGNAL(textChanged()), this, SLOT(textChangedListener())) << endl;

which returns true (it's connected).

When I change the text of the QTextEdit - app crashes with:

First-chance exception at 0x561158D7 (Qt5Guid.dll) in asd.exe: 0xC00000FD: Stack overflow (parameters: 0x00000000, 0x00092000).
Unhandled exception at 0x561158D7 (Qt5Guid.dll) in asd.exe: 0xC00000FD: Stack overflow (parameters: 0x00000000, 0x00092000).

I try to create a TextEdit, that when user type a wrong number, I will correct it, but I can't make this working.

Thanks for any help.

qt
qtextedit
qt-signals
slots
asked on Stack Overflow Mar 19, 2016 by (unknown user) • edited Dec 29, 2017 by Louis Langholtz

1 Answer

1

you probably need to disconnect the signal, otherwise you get an infinite loop

void MainWindow::textChangedListener(){
    QTextEdit* dock = qobject_cast<QTextEdit *>(QObject::sender());
    if (dock) {
        disconnect(dock, SIGNAL(textChanged()), this, SLOT(textChangedListener()))
        dock->setText("asd");
        connect(dock, SIGNAL(textChanged()), this, SLOT(textChangedListener()))
    }
}
answered on Stack Overflow Mar 19, 2016 by bibi

User contributions licensed under CC BY-SA 3.0