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?