QT src5_3a编辑框的框架-文本块-字体设置的操作
QT src5_3a编辑框的框架-文本块-字体设置的操作
#ifndef MAINWINDOW5_3_H #define MAINWINDOW5_3_H #include <QMainWindow> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow5_3; } QT_END_NAMESPACE class MainWindow5_3 : public QMainWindow { Q_OBJECT public: MainWindow5_3(QWidget *parent = nullptr); ~MainWindow5_3(); private: Ui::MainWindow5_3 *ui; private slots: void showTextFrame();//获取文本框架 void showTextBlock();//获取文本块 void setTextFont(bool checked); }; #endif // MAINWINDOW5_3_H
#include "mainwindow5_3.h" #include "ui_mainwindow5_3.h" #include <QTextFrame> #include <QDebug> MainWindow5_3::MainWindow5_3(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow5_3) { ui->setupUi(this); QTextDocument *qDoc=ui->textEdit->document();//获取文档对象 QTextFrame *root_frame = qDoc->rootFrame();//获取根框架 QTextFrameFormat q_format;//创建框架的格式 q_format.setBorderBrush(Qt::red);//边界的颜色 q_format.setBorder(2);//边界线的宽度,粗细 root_frame->setFrameFormat(q_format);//框架使用格式 QTextFrameFormat frameFormat; frameFormat.setBackground(Qt::darkGreen); frameFormat.setMargin(1);//边距 frameFormat.setPadding(2);//填衬 frameFormat.setBorder(2); frameFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Dotted);//边框样式 QTextCursor cursor2=ui->textEdit->textCursor();//获取光标 cursor2.insertFrame(frameFormat);//在光标处插入框架 //获取框架 QTextFrame操作 QAction *action_textFrame=new QAction(tr("框架"),this); connect(action_textFrame,&QAction::triggered,this,&MainWindow5_3::showTextFrame); ui->toolBar->addAction(action_textFrame); //获取文本块 QTextBlock操作 QAction *action_block= new QAction(tr("文本块"),this); connect(action_block,&QAction::triggered,this,&MainWindow5_3::showTextBlock); ui->toolBar->addAction(action_block); //设置字体 QAction * action_font=new QAction(tr("字体"),this); action_font->setCheckable(true); //connect(action_font,&QAction::triggered,this,&MainWindow5_3::setTextFont); connect(action_font,&QAction::toggled,this,&MainWindow5_3::setTextFont); ui->toolBar->addAction(action_font); } MainWindow5_3::~MainWindow5_3() { delete ui; } //获取框架 QTextFrame操作 void MainWindow5_3::showTextFrame() { QTextDocument *q_doc =ui->textEdit->document(); QTextFrame *q_frame = q_doc->rootFrame(); QTextFrame::iterator frame_iterator; for (frame_iterator=q_frame->begin();!(frame_iterator.atEnd());++frame_iterator) { QTextFrame *child_frame=frame_iterator.currentFrame();//获取当前框架的指针 QTextBlock childBlock=frame_iterator.currentBlock();//获取当前文本块 if(child_frame) { qDebug()<<"frame"; } else if(childBlock.isValid()) { qDebug()<<"block:"<<childBlock.text(); } } } //获取文本块 QTextBlock操作 void MainWindow5_3::showTextBlock() { QTextDocument *qdoc= ui->textEdit->document(); QTextBlock qblock=qdoc->firstBlock();//获取文档的第一个文本块哦 for(int i=0;i<qdoc->blockCount();i++) { qDebug()<<tr("文本块:%1,首行行号:%2,长度:%3,内容:").arg(i).arg(qblock.firstLineNumber()).arg(qblock.length())<<qblock.text(); qblock=qblock.next(); } } void MainWindow5_3::setTextFont(bool checked) { if(checked)//处于选中状态 { QTextCursor qcursor=ui->textEdit->textCursor(); QTextBlockFormat block_format;//文本块格式 block_format.setAlignment(Qt::AlignCenter);//水平居中 qcursor.insertBlock(block_format);//插入文本块的格式 QTextCharFormat char_format; //字符格式 char_format.setBackground(Qt::blue);//背景色 char_format.setForeground(Qt::yellow);//字体颜色 char_format.setFont(QFont(tr("宋体"),12,QFont::Bold,true));//宋体12号,加粗,斜体字 char_format.setFontUnderline(true);//使用下划线 qcursor.setCharFormat(char_format); qcursor.insertText(tr("测试一下字体呢")); } else { qDebug()<<tr("字体未设置"); } }
欢迎讨论,相互学习。
cdtxw@foxmail.com
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
2021-09-02 C# OPCServer:使用KEPServer安装方法_如何添加仿真器与添加西门子S7PLC
2021-09-02 c# 连接KepServer仿真器demo
2021-09-02 KEPServerEX OPC添加西门子PLC S7-1200一下属性存在错误,设备ID无效
2021-09-02 MatrikonOPC server
2021-09-02 工业控制PLC OPC_UA_DA是什么?opc server