R
R
runcode2019-04-27 12:34:03
Python
runcode, 2019-04-27 12:34:03

Why does it throw an error (QObject::connect: Cannot queue arguments of type 'QTextCharFormat' (Make sure 'QTextCharFormat' is registere)), but it works?

It gives an error but it works, help me figure it out. where to dig, what's wrong? I'm just learning. Thanks

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

import sys
import threading
from PyQt5 import QtWidgets, QtCore, QtGui
import logging


def thread(func):
    """threading"""

    def wrapper(*args, **kwargs):
        my_thread = threading.Thread(target=func, args=args, kwargs=kwargs)
        my_thread.start()

    return wrapper


class QTextEditLogger(logging.Handler):
    """logging update"""
    def __init__(self, parent):
        super().__init__()
        self.widget = QtWidgets.QTextEdit(parent)
        self.widget.setReadOnly(True)
        self.widget.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)

    def emit(self, record):
        msg = self.format(record)

        if record.levelname == 'BET':
            self.widget.setTextColor(QtGui.QColor(26, 122, 19))

        elif record.levelname == 'UPDATE':
            self.widget.setTextColor(QtGui.QColor(30, 53, 158))

        elif record.levelname == 'MESSAGE':
            self.widget.setTextColor(QtGui.QColor(244, 66, 83))

        elif record.levelname == 'UPDATE BET':
            self.widget.setTextColor(QtGui.QColor(201, 115, 30))

        else:
            self.widget.setTextColor(QtGui.QColor(0, 0, 0))

        self.widget.append(msg)
        self.widget.ensureCursorVisible()


class MyDialog(QtWidgets.QDialog):

    def __init__(self, parent=None):
        super().__init__(parent)

        self.logTextBox = QTextEditLogger(self)
        self.logTextBox.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s', "%H:%M:%S"))
        logging.getLogger().setLevel(logging.INFO)

        self._button = QtWidgets.QPushButton(self)
        self._button.setText('message')

        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(self.logTextBox.widget)
        layout.addWidget(self._button)
        self.setLayout(layout)

        self.log_update('WELCOME AUCTION BOT ')
        self._button.clicked.connect(lambda: self.log_message('AUCTION BOT '))

    @thread
    def log_update(self, text):
        """log format update """
        UPDATE = 35
        logging.getLogger().addHandler(self.logTextBox)
        logging.addLevelName(UPDATE, 'UPDATE')
        assert logging.getLevelName(UPDATE) == 'UPDATE'
        logging.log(UPDATE, text)

    @thread
    def log_message(self, text):
        """log format message """
        MESSAGE = 45
        logging.getLogger().addHandler(self.logTextBox)
        logging.addLevelName(MESSAGE, 'MESSAGE')
        assert logging.getLevelName(MESSAGE) == 'MESSAGE'
        logging.log(MESSAGE, text)


app = QtWidgets.QApplication(sys.argv)
dlg = MyDialog()
dlg.show()
dlg.raise_()
sys.exit(app.exec_())

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question