QT 中QTextEdit添加图片 .
2012-05-05 13:10
232 查看
在文本编辑框中添加图片,需要借助QTextDocument把图片作为资源添加到QTextEdit中
The QTextDocument class holds formatted text that can be viewed and edited using a QTextEdit.
QTextDocument is a container for structured rich text documents,
A QTextDocument can be edited programmatically using a QTextCursor,
两种方法
第一个:通过URL自愿形式
第二个:通过QTextImageFormat,利用QTextCursor来插入到文本编辑中
把图片添加到资源缓存中
QT中也给了个的Demo,有时间可以去看看。
The QTextDocument class holds formatted text that can be viewed and edited using a QTextEdit.
QTextDocument is a container for structured rich text documents,
A QTextDocument can be edited programmatically using a QTextCursor,
两种方法
第一个:通过URL自愿形式
QTextEdit *editor=new QTextEdit; this->editor->append("<img src=/"1.png/n //通过URL来插入到editor中
第二个:通过QTextImageFormat,利用QTextCursor来插入到文本编辑中
QTextImageFormat imageFormat; //保存图片格式对象 imageFormat.setName("1.png"); QTextCursor cursor; //编辑指针标 cursor.insertImage(imageFormat); //通过编辑指针表把图片格式的文件插入到资源中
把图片添加到资源缓存中
QT中也给了个的Demo,有时间可以去看看。
QApplication app(argc,argv); QTextCodec::setCodecForCStrings(QTextCodec::codecForName("System")); QTextCodec::setCodecForTr(QTextCodec::codecForName("System")); QTextCodec::setCodecForLocale(QTextCodec::codecForName("System")); QString s = tt(); int width=QApplication::desktop()->width(); int height=QApplication::desktop()->height(); QTextEdit *edit = new QTextEdit(); edit->setText(s); edit->append("<img src='//Program Files//helloQT//1.png'>"); edit->setReadOnly(true); edit->resize(200,200); edit->show(); app.exec();
QTextDocument *document=new QTextDocument(this); //图片容器 QUrl url; url = QUrl::fromLocalFile("1.png"); //指定Url document->addResource(QTextDocument::ImageResource,url,QVariant(url)); //添加资源到document容器中
相关文章推荐
- QT 中QTextEdit添加图片
- QT 中QTextEdit添加图片
- Qt中为QPushButton添加背景图片
- Qt setStyleSheet 添加背景色/背景图片(取消背景色,读取本地文件作为背景色)
- Qt中添加背景图片的方法
- Qt自适应大小显示图片,添加菜单
- Qt中为工程添加资源文件、给按钮添加图片
- QT QTextBrowser 添加HTML格式图片文字混合QString后换行问题
- QT笔记-Qt中添加背景图片的方法
- Qt模块化笔记之Qt_Quick——用QQuickImageProvider给Image组件添加图片
- Qt添加背景图片而不影响其控件的不用代码的设置方法
- Qt 代码: QComboBox 添加图片
- Qt(学习)知道了如何在程序中添加图片了
- QT 中添加资源图片
- Qt中添加背景图片的方法(转)
- Qt中为按钮、标签添加图片的几种方法
- Qt自适应大小显示图片,添加菜单
- QT 添加背景图片
- 组合框里添加复选框的方法(使用勾选的假象,用图片代替而已,并非QT原生支持)
- Qt:添加点击事件的Label并显示图片