简体   繁体   中英

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

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:

在此处输入图像描述

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)

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM