设置QTextEdit文本格式

本文介绍如何使用Qt在文本编辑器中设置选中文本的颜色、背景色及字体,同时还包括设置整个编辑区域背景色的方法。通过QColorDialog和QFontDialog实现交互式选择。
 
  
  1. //设置选中的文本的颜色 
  2.     QColor color = QColorDialog::getColor(Qt::black, this"Select Color", QColorDialog::DontUseNativeDialog); 
  3.     if(color.isValid()) 
  4.     { 
  5.         ui->doc->setTextColor(color); 
  6.     } 
  7. //设置选中整个编辑区域的背景色 
  8.     QColor color = QColorDialog::getColor(Qt::white, this"Select Color", QColorDialog::DontUseNativeDialog); 
  9.     if(color.isValid()) 
  10.     { 
  11.         QString tmp("QTextEdit {background-color:"); 
  12.         tmp += color.name(); 
  13.         tmp += QString(";}"); 
  14.         ui->doc->setStyleSheet(tmp); 
  15.         ui->doc->setTextBackgroundColor(color); 
  16.     } 
  17. //设置选中的文本的背景色 
  18.     QColor color = QColorDialog::getColor(Qt::white, this"Select Color", QColorDialog::DontUseNativeDialog); 
  19.     if(color.isValid()) 
  20.     { 
  21.         ui->doc->setTextBackgroundColor(color); 
  22.     } 
  23. //设置选中的文本的字体 
  24.     bool ok; 
  25.     QFont font = QFontDialog::getFont(&ok, this); 
  26.     if (ok) 
  27.     { 
  28.         QTextCursor cur = ui->doc->textCursor(); 
  29.         QString sltStr = cur.selectedText(); 
  30.         ui->doc->cut(); 
  31.         QTextCharFormat fmtText; 
  32.         fmtText.setFont(font); 
  33.         cur.insertText(sltStr,fmtText); 
  34.     } 










本文转自 hakuyo 51CTO博客,原文链接:http://blog.51cto.com/hakuyo/1125782,如需转载请自行联系原作者

在使用 `QTextEdit` 控件时,若需要实现文本的**垂直居中**效果,可以通过以下方法实现: ### 方法一:使用 `QTextBlockFormat` 设置垂直居中 通过设置文本块的格式,可以控制文本在 `QTextEdit` 内部的对齐方式。具体步骤如下: 1. 获取当前文档的文本光标; 2. 创建 `QTextBlockFormat` 对象,并设置对齐方式; 3. 将格式应用到文档的默认文本块格式。 示例代码如下: ```python from PyQt5.QtWidgets import QTextEdit, QApplication, QWidget from PyQt5.QtGui import QTextBlockFormat, QTextCursor, QTextCharFormat class CenteredTextEdit(QTextEdit): def __init__(self, parent=None): super().__init__(parent) self.setCenterAlignment() def setCenterAlignment(self): cursor = self.textCursor() block_format = QTextBlockFormat() block_format.setAlignment(Qt.AlignCenter) # 设置文本块居中对齐 cursor.select(QTextCursor.Document) cursor.mergeBlockFormat(block_format) self.setTextCursor(cursor) ``` ### 方法二:结合 HTML/CSS 样式设置垂直居中 `QTextEdit` 支持富文本格式,可以通过设置 HTML 内容并结合 CSS 样式实现垂直居中的效果。例如: ```python text_edit = QTextEdit() text_edit.setHtml('<div style="display: flex; align-items: center; justify-content: center; height: 100%;">' '<p>垂直居中的文本内容</p>' '</div>') ``` 该方法利用了 CSS 的 `flex` 布局特性,确保文本在容器内垂直和水平居中。 ### 方法三:自定义控件并重绘文本 如果上述方法无法满足需求,可以通过继承 `QTextEdit` 并重写 `paintEvent` 方法,手动控制文本的绘制位置。示例如下: ```python from PyQt5.QtWidgets import QTextEdit, QApplication, QWidget from PyQt5.QtGui import QPainter, QTextOption from PyQt5.QtCore import Qt class CustomTextEdit(QTextEdit): def __init__(self, parent=None): super().__init__(parent) def paintEvent(self, event): painter = QPainter(self.viewport()) text_option = QTextOption() text_option.setAlignment(Qt.AlignCenter) # 设置文本居中对齐 painter.drawText(self.rect(), self.toPlainText(), text_option) ``` ### 方法四:设置 `QTextCharFormat` 的垂直对齐方式 可以通过 `QTextCharFormat` 设置字符格式,控制文本的垂直对齐方式。示例如下: ```python cursor = text_edit.textCursor() char_format = QTextCharFormat() char_format.setVerticalAlignment(QTextCharFormat.AlignMiddle) # 设置垂直居中 cursor.select(QTextCursor.Document) cursor.setCharFormat(char_format) text_edit.setTextCursor(cursor) ``` ### 注意事项 - 使用 `setPlainText` 或 `setText` 方法后,原有的对齐设置可能会被重置为默认的左对齐,因此需要在这些操作之后重新应用对齐格式。 - 若 `QTextEdit` 的内容频繁更新,建议将对齐设置封装为一个函数,并在每次更新后调用该函数以保持对齐效果。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值