QT系列教程(10) QTextEdit学习

简介

QTextEdit是文本编辑器,支持富文本功能。接下来我们创建一个Qt Application 应用,然后在ui中添加一个QTextEdit插件。
运行程序后,可以在QTextEdit中输入任何文字也包括富文本。

文本块

我们在MainWindow的ui文件中添加了textedit插件,然后在MainWindow的构造函数中写代码,修改文本框样式

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
   
    ui->setupUi(this);
   QTextDocument *doc= ui->textEdit->document();
   QTextFrame* root_frame = doc->rootFrame();
   QTextFrameFormat format;
   format.setBorderBrush(Qt::blue);
   format.setBorder(3);
   root_frame->setFrameFormat(format);
}

通过textEdit的document函数返回文本块,再通过rootFrame获取根框架,设置这个框架的边框样式为蓝色,边框为3.
运行程序后可以看到主窗口的textedit会显示一个蓝色边框的输入框,那就是根节点。我们可以插入两个纯文本

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
   
    ui->setupUi(this);
   QTextDocument *doc= ui->textEdit->document();
   QTextFrame* root_frame = doc->rootFrame();
   QTextFrameFormat format;
   format.setBorderBrush(Qt::blue);
   format.setBorder(3);
   root_frame->setFrameFormat(format);
   ui->textEdit->insertPlainText("hello world!\n");
   ui->textEdit->insertPlainText("Hello Qt\n");
}

运行程序会看到插入的纯文本在根文本块里。我们可以再设置一个文本样式,然后插入到光标所在的位置,这样会生成一个新的文本框架,然后插入两行文字,这样会生成两个文本块

   QTextFrameFormat  frameFormat;
   frameFormat.setBackground(Qt::lightGray);
   frameFormat.setMargin(10);
   frameFormat.setPadding(5);
   frameFormat.setBorder(2);
   frameFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Dashed);
   QTextCursor cursor = ui->textEdit->textCursor();
   cursor.insertFrame(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

恋恋风辰

感谢打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值