0

I have created a UI which contains a text edit control. Below is its code:

home.py

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit.setGeometry(QtCore.QRect(130, 140, 301, 241))
        self.textEdit.setObjectName("textEdit")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Below is the main.py:

import sys
import time
from home import Ui_MainWindow
from PyQt5.QtWidgets import QApplication, QMainWindow
from threading import Thread


class UI(QMainWindow, Ui_MainWindow):
    def __init__(self):
    QMainWindow.__init__(self)
    self.ui = Ui_MainWindow()
    self.ui.setupUi(self)
    self.ui.textEdit.append("Application starting")
    Thread(target=self.startProc).start()

    def startProc(self):
    i = 0
    while True:
        i = i + 1
        self.ui.textEdit.append("Current value: {}".format(i))
        time.sleep(1)
        if i == 5:
            break


app = QApplication(sys.argv)
main_window = UI()
main_window.show()
sys.exit(app.exec_())

So in above code, I am creating a thread (startProc) which runs for 5sec and then break. This code is working fine in Windows 10 and executes normally but when running it in Ubuntu 18.04, it runs fine and as soon as the thread stops, the whole app crashes with below errors:

QObject::connect: Cannot queue arguments of type 'QTextCursor'
(Make sure 'QTextCursor' is registered using qRegisterMetaType().)

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

Is there any specific reason that its working fine on Windows but not in Ubuntu? How can I resolve this issue?

halfer
  • 19,824
  • 17
  • 99
  • 186
S Andrew
  • 5,592
  • 27
  • 115
  • 237
  • The reality is that you can see it working on Windows just because "you're lucky", as you're doing something that is actually *not* allowed: accessing the UI from external threads, as widgets are **not** thread safe. You must use QThreads and signals, please do some research on that, as there are literally hundreds of posts and tutorials about it. – musicamante Oct 10 '22 at 10:57

0 Answers0