1

I have a QPlainTextEdit box which is read-only and is used as an application console. I basically want to append a string WITHOUT a newline or \r to the PlainTextEdit widget using appendPlainText. Looking through similar quesitions, people seem to just set cursor to the end with myTextEdit->moveCursor (QTextCursor::End); and then simply append. But this did not work for me.

In my case I use pyqtSignal to append text. So, I first signal the mainwindow to set the cursor to the end and then append my string to the QPlainTextEdit widget. But it still didn't work.

Since my MainWindow have thousands of lines of code, I will just paste the pieces related to my question.

Code:

class Emitter(QThread):
    from PyQt5.QtCore import pyqtSignal, QObject, Qt
    from PyQt5.QtGui import QPixmap, QStandardItemModel, QStandardItem, QIcon
    from multiprocessing import Process, Queue, Pipe
    
    ConsoleSGL = pyqtSignal(str)
    consoleCursorSGL = pyqtSignal(str)
    
    def __init__(self, from_process: Pipe):
        super().__init__()
        self.data_from_process = from_process


    def run(self):
        while True:
            try:
                signalData = self.data_from_process.recv()
            except EOFError:
                break
            else:
                if (len(signalData) > 1 and signalData[0] == "ConsoleSGL"):
                    self.ConsoleSGL.emit(signalData[1])
                elif (len(signalData) > 1 and signalData[0] == "consoleCursorSGL"):
                    self.consoleCursorSGL.emit(signalData[1])
                    

class MainWindow(QMainWindow, Ui_MainWindow):
    from multiprocessing import Process, Queue, Pipe
    def __init__(self, child_process_queue: Queue, emitter: Emitter):
        QMainWindow.__init__(self)
        self.process_queue = child_process_queue
        self.emitter = emitter
        self.emitter.daemon = True
        self.emitter.start()
        self.setupUi(self)
        self.pyqt5GuiCodeMain()

    def pyqt5GuiCodeMain(self):
        self.emitter.ConsoleSGL.connect(self.consoleQPlainTextEdit.appendPlainText)
        self.emitter.consoleCursorSGL.connect(self.moveDevConsoleCSR)
    
    def moveDevConsoleCSR(self, position):
        if position == "End":
            print("Going to end")
            self.consoleQPlainTextEdit.moveCursor(QTextCursor.End)

When ever I want to append to my plainTextWidget, I do this:

for text in range(0, 10):
    self.signaller.send(["consoleCursorSGL", "End"])
    self.signaller.send(["ConsoleSGL", text])

The output I get in my widget is:

0
1
2
3
4
5
6
7
8
9

Note: I do see print("Going to end") prints in my python console output.

Can anyone tell me what am I doing wrong? or how do I append without newline using pyqt5 signals?

hashy
  • 175
  • 10
  • Get the cursor (`cursor = self.consoleQPlainTextEdit.textCursor()`), use `cursor.moveCursor(cursor.End)` and then [`cursor.insertText()`](https://doc.qt.io/qt-5/qtextcursor.html#insertText). Also, avoid imports outside of the script header, unless you **really** know what you're doing: doing it in class indentation is quite pointless, since they would be probably loaded anyway. – musicamante Dec 23 '22 at 23:58
  • @musicamante Thanks, but I am getting `AttributeError: 'QTextCursor' object has no attribute 'moveCursor'`. Anyway I just solved it, going to post an answer. – hashy Dec 24 '22 at 01:14
  • Sorry, my bad. It's [`movePosition()`](https://doc.qt.io/qt-5/qtextcursor.html#movePosition). But, in any case, ***always*** check and carefully study the documentation. – musicamante Dec 24 '22 at 01:15
  • Well, It seems stackoverflow don't allow people to answer their own question. for Above, all I had to do was to use, `insertPlainText` rather than `appendPlaindText` as `self.emitter.ConsoleSGL.connect(self.consoleQPlainTextEdit.insertPlainText)`. Everything else was just fine – hashy Dec 24 '22 at 01:16
  • 1
    No: `insertPlainText()` is not a valid solution for what you want to do, because if the user clicks or selects some text in the editor, it will insert the text at that position (potentially overwriting the selection). You *must* use the QTextCursor interface. Note: the question has been closed since a similar one has already been set as its duplicated, so you cannot answer it. – musicamante Dec 24 '22 at 01:18
  • 1
    If you believe that the answers on the other post don't match what you're asking, then please add a comment explaining your motivations and we'll eventually vote for reopening. – musicamante Dec 24 '22 at 01:24

0 Answers0