qtextedit如何读取docc++文件读取

qt qtextedit 文字修改行距
网上搜了很多,大部分用起来有问题,要么只能修改第一段的行间距,要么就不生效。后来找了很久终于找到一个可以用的,现贴在下面,每次使用调用一次即可
QTextDocument *doc =
ui-&textEdit_label-&document();
QTextCursor textcursor = ui-&textEdit_label-&textCursor();
for(QTextBlock it = doc-&begin(); it !=doc-&end();it = it.next())
QTextBlockFormat tbf = it.blockFormat();
tbf.setLineHeight(lineSpacing,QTextBlockFormat::LineDistanceHeight);
textcursor.setPosition(it.position());
textcursor.setBlockFormat(tbf);
ui-&textEdit_label-&setTextCursor(textcursor);
设置QTextEdit的行高和行间距
Qt QTextEdit设置行间距和段间距
QT textEdit,lineedit等改变字体颜色方法
QTextEdit更改特定某些行的字体格式
QTextEdit 适应内容调整大小
Qt简单编程-词间距行间距
没有更多推荐了,扫一扫体验手机阅读
设置QTextEdit文本格式
<span type="1" blog_id="1125782" userid='
35篇文章,5W+人气,0粉丝
运维开发全攻略
¥51.0013人订阅
大数据时代的微服务之路
¥51.00498人订阅Qt QTextEdit设置行间距和段间距
段间距比较容易实现,通过设置html的css即可实现段间距调整;
行间距需要利用Qt用的几个类配合实现;
代码比较简单具体实现如下:
QString HD = ui-&H1Edit-&text();//行间距
QString HH = ui-&H2Edit-&text();//段间距
QString src = ui-&textEdit-&toHtml();
QString strV
//=ui-&textEdit-&toPlainText();
strView = "&html&&head&&meta name=\"qrichtext\" content=\"1\" /&&style type=\"text/css\"&\np, li { white-space: pre- }\n&/style&&/head&&body style=\" font-family:'Ubuntu'; font-size:11 font-weight:400; font-style:\"&&/body&&/html&";
int pos = 0;
//padding-top
//获取段落
int startNum = src.indexOf("&p",0);
int endNum = src.lastIndexOf("&/body&");
QString txt = src.mid(startNum,endNum);
//更改段间距
const QString StrHH = "margin-top:";
for(;;)//已知bug 段距大于10
pos = txt.indexOf(StrHH,pos+1);
if(pos!=-1)
txt.replace(pos+StrHH.length(),1,HH);
int despos = strView.lastIndexOf("&/body&");
strView.insert(despos,txt);
ui-&textEdit-&clear();
ui-&textEdit-&setHtml(strView);
QTextDocument *doc =
ui-&textEdit-&document();
QTextCursor textcursor = ui-&textEdit-&textCursor();
for(QTextBlock it = doc-&begin(); it !=doc-&end();it = it.next())
QTextBlockFormat tbf = it.blockFormat();
tbf.setLineHeight(HD.toInt(),QTextBlockFormat::LineDistanceHeight);
textcursor.setPosition(it.position());
textcursor.setBlockFormat(tbf);
ui-&textEdit-&setTextCursor(textcursor);
}其中段间距有bug是设置插入字符串位置的问题。需要用的自行解决。
设置QTextEdit的行高和行间距
Qt简单编程-词间距行间距
设置 QLabel 上面的 text 行间距
qt qtextedit 文字修改行距
Qt 之图形(绘制文本)
一种qt绘制多行文本是自定义行高的解决办法
没有更多推荐了,QT加载office文件简单方法
QT 使用COM组件加载office的文件,使用需要安装MS office。WORD,EXCEL文件可以直接嵌入;PPT文件无法直接嵌入。能嵌入的同志望回复告知。下面描述了 加载;*.*.*.*.*.*.*.xlsb,*.*.*.*.*.这几种文件。源代码如下:
一:组件皮肤文件
utofficewidget.ui
&?xml version="1.0" encoding="UTF-8"?&&ui version="4.0"& &class&UTOfficeWidget&/class& &widget class="QWidget" name="UTOfficeWidget"&
&property name="geometry"&
&width&400&/width&
&height&300&/height&
&/property&
&property name="windowTitle"&
&string&UTOfficeWidget&/string&
&/property&
&layout class="QVBoxLayout" name="verticalLayout"&
&widget class="QTextEdit" name="textEdit"/&
&/layout& &/widget& &layoutdefault spacing="6" margin="11"/& &resources/& &connections/&&/ui&二:头文件
utofficewidget.h#ifndef UTOFFICEWIDGET_H#define UTOFFICEWIDGET_H#include &QWidget&#include&QAxObject&#include&QAxWidget&#include &QTextStream&#include&QFile&#include "ui_utofficewidget.h"class UTOfficeWidget : public QWidget{Q_OBJECTpublic:UTOfficeWidget(QWidget *parent = 0);~UTOfficeWidget();public://打开文件*.*.*.*.*.*.*.xlsb,*.*.*.*.*.void Open(QString filePath);//关闭 是否需要保存 默认不保存void Close(bool isSave=false);//保存void Save();//另存为。。void SaveAs(QString newFilePath);private://文档打开控件QAxWidget* officeC//当前打开文件的路径QString currentFilePUi::UTOfficeW};#endif // UTOFFICEWIDGET_H三:CPP文件:#include "utofficewidget.h"UTOfficeWidget::UTOfficeWidget(QWidget *parent): QWidget(parent),officeContent(NULL){ui.setupUi(this);}UTOfficeWidget::~UTOfficeWidget(){Close();}void UTOfficeWidget::Open( QString filePath ){if (filePath.isEmpty()){}if (officeContent){Close();}if (filePath.endsWith(".docx") ||filePath.endsWith(".doc") || filePath.endsWith(".doc")){officeContent=new QAxWidget ("Word.Document", ui.textEdit);officeContent-& setGeometry (QRect (10, 10, this-&width()-10, this-&height()-10));officeContent-&setControl (filePath);officeContent-& show ();}else if (filePath.endsWith(".xlsx") || filePath.endsWith(".xls") ||filePath.endsWith(".xlsm")){officeContent=new QAxWidget ("Excel.WorkBook", ui.textEdit);officeContent-& setGeometry (QRect (10, 10, this-&width()-10, this-&height()-10));officeContent-&setControl (filePath);officeContent-& show ();}else if (filePath.endsWith(".ppt") || filePath.endsWith(".pptx") ||filePath.endsWith(".pptm")){//"Powerpoint.Presentation"officeContent=new QAxWidget ("Powerpoint.Application",ui.textEdit);//pptPresentation-&setControl("{048EB43E-E0-557DA96038AF}");
//pptPresentation-&setControl("{A91-11CF-60263B}");officeContent-& setGeometry (QRect (10, 10, this-&width()-10, this-&height()-10));QAxObject *presentations = officeContent-&querySubObject( "Presentations" ); presentations-&dynamicCall("Open(QString)",filePath);}else if (filePath.endsWith(".txt") || filePath.endsWith(".xml")){QFile sfile(filePath);if (sfile.open(QFile::ReadOnly | QFile::Text)){QTextStream in(&sfile);ui.textEdit-&setText(in.readAll());sfile.close();}}else{}currentFilePath=fileP}void UTOfficeWidget::Close( bool isSave/*=false*/ ){if (officeContent){if (currentFilePath.endsWith(".ppt") || currentFilePath.endsWith(".pptx") ||currentFilePath.endsWith(".pptm")){officeContent-&dynamicCall("Quit(void)");}else if (currentFilePath.endsWith(".txt") || currentFilePath.endsWith(".xml")){ui.textEdit-&clear();}else{officeContent-&dynamicCall("Close(bool)",true);}}}void UTOfficeWidget::Save(){}void UTOfficeWidget::SaveAs( QString newFilePath ){}四:调用测试:
UTOfficeWidget *officeConent=new UTOfficeWidget(this);
officeConent-&setFixedSize(this-&width(),this-&height());officeConent-&Open(QString("D:/123.docx"));ui.horizontalLayout-&addWidget(officeConent);
qt QAxWidget打开Office文件及pdf
QT实现Word读写
Qt窗体实现类似于PowerPoint 切换页面的推进效果
Qt窗体中嵌入office文档的新方法
Qt显示pdf系列2——QAxWidget打开Office文件及pdf
QObject怎样把ppt嵌入qt widget里显示,并控制自动播放
qt4.8.6操作ppt
qt中打开excel、word等文件方法
没有更多推荐了,和大家分享工作中遇到的一些问题和总结
QTextEdit添加文本对象(将N个文字结合成一个整体)
在处理文字的时候我们有时候不得不这样做:“当输入譬如:@人民日报 这样的标记的时候,我希望 [@人民日报] 这几个字符是一个整体, 不能对其中的任意一个字符进行修改
要改也只能改整体。”
类似于QQ群的 @ 功能。
现在就记录核心实现,先看看效果:
其中插入的表情就是一个整体,不能修改其中的内容(红色部分)。而自己手输入进去的就是当普通的文本处理了。
首先,定义一个对象来处理这种自定义类型的TextFormat:
先定义自定义的Object的类型和它的属性:
enum TextObject
TO_ObjectAt = QTextFormat::UserObject + 1
enum ObjectProperty
= QTextFormat::UserProperty + 1,
OP_FontSize
这是用来处理自定义的TextObject对象的接口
class DIYTextObjectHandler: public QObject, QTextObjectInterface
Q_INTERFACES(QTextObjectInterface)
QSizeF intrinsicSize(QTextDocument *doc, int posInDocument,
const QTextFormat &format)
int fontSize = format.property(MainWindow::OP_FontSize).toInt();
QString text = format.property(MainWindow::OP_Text).toString();
return QSizeF(text.length() * fontSize, fontSize);
void drawObject(QPainter *painter, const QRectF &rect, QTextDocument *doc,
int posInDocument, const QTextFormat &format)
QString text = format.property(MainWindow::OP_Text).toString();
painter-&save();
painter-&setPen(Qt::red);
painter-&drawText(rect, text);
painter-&restore();
};这个DIYTextObjectHandler就是用来处理我们插入进去的标签的,也就是说,这个表情是由这个对象来"画出来"的。
接下来要把这个handler注册到TextEidt中:
void MainWindow::setupTextObjectHandler()
QTextDocument* doc = ui-&textEdit-&document();
DIYTextObjectHandler* handler = new DIYTextObjectH
handler-&setParent(this);
doc-&documentLayout()-&registerHandler(TO_ObjectAt, handler);
qDebug() && "register handler complite.";
当程序运行的时候调用这个函数初始化。
然后再我们的插入标签事件当中这样插入标签:
void MainWindow::insertTextObject()
QString text = ui-&lineEdit-&text();
QTextCharF
format.setObjectType(TO_ObjectAt);
format.setProperty(OP_Text, QVariant::fromValue(text));
format.setProperty(OP_FontSize, QVariant::fromValue(14));
ui-&textEdit-&textCursor().insertText(QString(QChar::ObjectReplacementCharacter),
qDebug() && "insert text object: " &&
这样插入了一个标签实现了我们的 At 某人 的UI功能
QTextEdit 的一些总结
Qt中如何实现QTextEdit中文字的单个删除 和 连续删除
QTextEdit显示超链接
QTextEdit实时显示运行结果
计算QTextEdit的高度
QTextEdit限制文本长度
怎样获取QTextEdit
中的选中的文本
Qt QTextEdit 使用拖放、复制、粘贴进行文本或图像的插入
如何打开一个已存在的txt格式的文本文档 ,并显示在QTextEdit中
没有更多推荐了,

我要回帖

更多关于 从文件中读取数据 的文章

 

随机推荐