How to Format a string on a textedit box in QErrorMessage in PyQt5?

Question:

How to make a new line on TextEdit Box in QErrorMessage ? and How to trim the blank space in windows headings? Here is My Code,

    self.error_msg = QErrorMessage()
    self.error_msg.setFixedSize(500,200)
    
    exc_type, exc_value, exc_traceback = sys.exc_info()
    filename = exc_traceback.tb_frame.f_code.co_filename
    lineno = exc_traceback.tb_lineno
    name = exc_traceback.tb_frame.f_code.co_name
    type = exc_type.__name__
    message = exc_value
    nl = 'n'

    self.error_msg.setWindowTitle(f'{type}')
    self.error_msg.showMessage(f'File Name : {filename[:-3]}{nl}Line No. : {lineno}')
    print(f'File Name : {filename[:-2]}{nl} Line No. : {lineno}')

This code produces the following result:

enter image description here

How to trim the blank space in windows headings ( Space between AttributeError and question mark) and How to make a new line in Qtextedit box ( File name is in first line, Line No in next line)

Asked By: Bala

||

Answers:

The text field is a standard QTextEdit, and showMessage() actually uses setHtml().

In HTML, new line characters are treated as white spaces (and merged with them in the same manner). Just use the standard HTML line break separator:

n1 = '<br/>'

That question mark has nothing to do with the title text, nor QErrorMessage: it’s the standard context help button that exists on any standard Windows dialog (so, all QDialog subclasses use it by default). Since it’s a window flag, just use the standard method for customizing window hints:

self.error_msg.setWindowFlag(Qt.WindowContextHelpButtonHint, False))
Answered By: musicamante
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.