python加载文件并显示文件内容到qtextedit上_在Python中使用QTextEdit中的格式对齐文本...

I want to display text in a QTextEdit. I use the format() function to align the text and make it look like a clean table. Although I get a perfect result when displaying the text in the shell, the text doesn't seem to align in the QTextEdit, like if the width of a character varies. I mainly see the difference when the character "-" is present.

>>> first_line = "{:<10} {:<3} - {:<20}".format("1234", "EUR", "Mrs Smith")

>>> second_line = "{:<10} {:<3} - {:<20}".format("-45.62", "GBP", "M Doe")

>>> print first_line, "\n", second_line

1234 EUR - Mrs Smith

-45.62 GBP - M Doe

The result as expected in the shell. But with the QTextEdit, the alignment is not right as you can see the slight difference between "EUR" and "GBP". It's not much in this example but when I use it with many more lines, it really doesn't look right.

my_text_edit = QTextEdit()

my_text_edit.append(first_line)

my_text_edit.append(second_line)

I tried to use a QPlainTextEdit and got the same result. Anyway to get what I want with a QTextEdit/QPlainTextEdit ? or should I use another display widget (no editing is recquired, a label would do but I like the look of the text edit) ?

解决方案

I was using the default font that doesn't have a fixed width, hence the non alignment. Setting the font to a fixed width font like 'monospace' solved my problem :

fixed_font = QFont("monospace")

fixed_font.setStyleHint(QFont.TypeWriter)

my_text_edit.setFont(fixed_font)

I used "setStyleHint" to indicate which kind of font Qt should use if 'monospace' is not found on the system, "QFont.TypeWriter" indicating to choose a fixed pitch font so the alignment is still respected.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在QTextEdit显示行号,可以通过以下步骤实现: 1. 创建一个新的QWidget,用于包含QTextEdit和显示行号的QLabel。 2. 将QTextEdit的verticalScrollBar()的valueChanged信号连接到一个槽函数,该槽函数将更新QLabel的行号。 3. 在QTextEdit的paintEvent绘制行号。 以下是示例代码: ```python from PyQt5.QtWidgets import QWidget, QTextEdit, QVBoxLayout, QLabel class LineNumberArea(QWidget): def __init__(self, editor): super().__init__() self.editor = editor def sizeHint(self): return self.editor.sizeHint() def paintEvent(self, event): painter = QPainter(self) font_metrics = self.editor.fontMetrics() block = self.editor.firstVisibleBlock() line_number = block.blockNumber() top = self.editor.blockBoundingGeometry(block).translated(self.editor.contentOffset()).top() bottom = top + self.editor.blockBoundingRect(block).height() # Iterate over visible blocks while block.isValid() and top <= event.rect().bottom(): if block.isVisible() and bottom >= event.rect().top(): # Draw line number number_width = font_metrics.width(str(line_number)) number_rect = QRect(0, top, self.width() - 5, font_metrics.height()) painter.drawText(number_rect, Qt.AlignRight, str(line_number)) block = block.next() top = bottom bottom = top + self.editor.blockBoundingRect(block).height() line_number += 1 class TextEditor(QWidget): def __init__(self): super().__init__() self.text_edit = QTextEdit(self) self.line_number_area = LineNumberArea(self.text_edit) self.init_ui() def init_ui(self): layout = QVBoxLayout() layout.setContentsMargins(0, 0, 0, 0) layout.addWidget(self.text_edit) self.setLayout(layout) # Connect signal to update line number area self.text_edit.verticalScrollBar().valueChanged.connect(self.update_line_number_area) def update_line_number_area(self): rect = self.line_number_area.rect().intersected(self.text_edit.viewport().rect()) self.line_number_area.update(0, rect.y(), self.line_number_area.width(), rect.height()) def resizeEvent(self, event): super().resizeEvent(event) self.line_number_area.setGeometry(QRect(self.contentsRect().left(), self.contentsRect().top(), self.line_number_area.width(), self.height())) self.text_edit.setGeometry(QRect(self.line_number_area.width(), self.contentsRect().top(), self.contentsRect().right() - self.line_number_area.width(), self.height())) def paintEvent(self, event): super().paintEvent(event) painter = QPainter(self) painter.fillRect(self.line_number_area.rect(), Qt.lightGray) ``` 在这个示例,我们创建了一个名为LineNumberArea的新QWidget,它使用QPainter在QTextEdit旁边显示行号。在TextEditor,我们将QTextEdit和LineNumberArea组合在一起,并将QTextEdit的verticalScrollBar()的valueChanged信号连接到一个槽函数,该槽函数将更新LineNumberArea的行号。我们还实现了resizeEvent和paintEvent函数来确保LineNumberArea与QTextEdit正确地对齐,并在文本编辑器旁边绘制一个灰色背景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值