The PyQt5 programme automatically stops working after some time when using setTextCursor()

Question:

I have a PyQt5 GUI that is in charge of taking voice input from the user and converting it to text. 

Everything was fine until I was told to add a new feature where the user can edit text while speaking. The cursor should not move to the start or end of a paragraph; it should stay where it is, and the text should come as it did before, so the user can edit text while speaking.

So when I add cursor positioning code my program runs for few minutes and breaks or quits window without any errors in console*(terminal).* When I remove cursor code it works.

My VoiceWorker() class inherited from QThread():

class VoiceWorker(QtCore.QThread):

    textChanged = QtCore.pyqtSignal(str)
    sig_started = QtCore.pyqtSignal()

    def run(self) -> None:

        self.sig_started.emit()
        mic = PyAudio()

        stream = mic.open(format=FORMAT, channels=CHANNELS,
                            rate=RATE, input=True, frames_per_buffer=CHUNK)

        while not self.stopped:
            try:
                data = stream.read(4096, exception_on_overflow=False)
                if recognizer.AcceptWaveform(data):
                    text = recognizer.Result()
                    cursor = txtbox.textCursor()
                    pos = cursor.position()
                    print(f"n Cursor position : {pos} n")
                    speech = text[14:-3]+" "
                    print(f"n {speech=} n")
                    self.textChanged.emit(speech)
                    cursor.movePosition(cursor.End)
                    txtbox.setTextCursor(cursor)
            except Exception as e:
                print(f"n {e} n")
                break

and I’ve connected VoiceWorker thread to my QTextEdit()

voice_thread = VoiceWorker()
voice_thread.textChanged.connect(txtbox.insertPlainText)

When I run this programme on a Windows system, it closes without any errors, but on a Linux system, it closes with this error: "segmentation fault (core dumped)". This problem occurs only when I add cursor code.

I don’t know why this is happening; please help me to solve this issue.

I tried to search on Google but no luck becouse I didn’t understand why It’s happening.

Asked By: Ankit Tiwari

||

Answers:

Finally, I’ve figured out a solution to the above problem, and thanks to Musicamante for giving a suggestion about

for what you need, the existing signal is more than enough, just do all the
rest (cursor position, replacement, movement, etc) in the function
connected to that signal.

So this is my final code. It looks like it is able to set text at the end of a paragraph, and it sets the cursor at the same position as it was before.

class VoiceWorker(QtCore.QThread):

    textChanged = QtCore.pyqtSignal(str)
    sig_started = QtCore.pyqtSignal()

    def run(self) -> None:

        self.sig_started.emit()
        mic = PyAudio()

        stream = mic.open(format=FORMAT, channels=CHANNELS,
                            rate=RATE, input=True, frames_per_buffer=CHUNK)

        while not self.stopped:
            try:
                data = stream.read(4096, exception_on_overflow=False)
                if recognizer.AcceptWaveform(data):
                    text = recognizer.Result()
                    speech = text[14:-3]+" "
                    print(f"n {speech=} n")
                    self.textChanged.emit(speech)
            except Exception as e:
                print(f"n {e} n")
                break
voice_thread = VoiceWorker()
voice_thread.textChanged.connect(changeCursor)

and this is my pyqtslot

@pyqtSlot(str)
def changeCursor(text):
    cursor = txtbox.textCursor()
    pos = cursor.position()
    print(f"n Cursor position : {pos} n")
    cursor.movePosition(cursor.End)
    cursor.insertText(text)
    cursor.setPosition(pos)
    txtbox.setTextCursor(cursor)
Answered By: Ankit Tiwari
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.