高手帮忙看下QTextEdit插入超链接的问题,帮忙解决一下!(QT4)

lugaideath1 2009-03-13 04:18:59
我在QTextEdit里插入一个超链接后,然后在超链接后面继续输入,但输入的内容也算是超连接,谁可以帮我解决一下这个问题,下面是代码!
运行以后,按一下insertButton,然后再按一下saveButton保存文件,然后再按loadButton,重新加载页面,不然插入的超链接看不到是超链接的,谁也可以帮忙解决一下这个显示问题呢?

myLink.h
#ifndef MYLINK_H
#define MYLINK_H

#include <QtGui>

class myLink : public QWidget
{
Q_OBJECT

public:
myLink(QWidget *parent = 0);

private slots:
void insetLink();
void loadFile();
void svaeFile();

private:
QTextEdit *textBrowser;
QGridLayout *gridLayout;
QPushButton *saveButton;
QPushButton *loadButton;
QPushButton *insertButton;

QString fileName;
};
#endif



myLink.cpp
#include "myLink.h"

myLink::myLink(QWidget *parent):QWidget(parent)
{
gridLayout = new QGridLayout(this);

saveButton = new QPushButton(this);
saveButton->setText(tr("svae html"));
gridLayout->addWidget(saveButton,0,0,1,1);

loadButton = new QPushButton(this);
loadButton->setText(tr("load html"));
gridLayout->addWidget(loadButton,0,1,1,1);

insertButton = new QPushButton(this);
insertButton->setText(tr("insert link"));
gridLayout->addWidget(insertButton,0,2,1,1);

textBrowser = new QTextEdit(this);
textBrowser->setReadOnly(false);
textBrowser->setFocus();
gridLayout->addWidget(textBrowser,1,0,1,3);
fileName = QDir::QDir("aaa.html").absolutePath();
loadFile();

connect(saveButton, SIGNAL(clicked()), this, SLOT(svaeFile()));
connect(loadButton, SIGNAL(clicked()), this, SLOT(loadFile()));
connect(insertButton, SIGNAL(clicked()), this, SLOT(insetLink()));
}

void myLink::insetLink()
{
QTextCharFormat tcf;
tcf = textBrowser->currentCharFormat();
tcf.setAnchor(true);
tcf.setAnchorHref("http://www.baidu.com");
if(!textBrowser->textCursor().hasSelection())
textBrowser->textCursor().insertText(trUtf8("百度"),tcf);
else
textBrowser->setCurrentCharFormat(tcf);
}

void myLink::loadFile()
{
if (!QFile::exists(fileName))
return;
QFile file(fileName);
if (!file.open(QFile::ReadOnly))
return;

QByteArray data = file.readAll();
QTextCodec *codec = Qt::codecForHtml(data);
QString str = codec->toUnicode(data);
if (Qt::mightBeRichText(str)) {
textBrowser->setHtml(str);
} else {
str = QString::fromLocal8Bit(data);
textBrowser->setPlainText(str);
}
}

void myLink::svaeFile()
{
if (fileName.isEmpty())
return;

QFile file(fileName);
if (!file.open(QFile::WriteOnly))
return;
QTextStream ts(&file);
ts.setCodec(QTextCodec::codecForName("UTF-8"));
ts << textBrowser->document()->toHtml("UTF-8");
textBrowser->document()->setModified(false);
}


main.cpp
#include <QtGui>
#include "myLink.h"

int main(int argc,char *argv[])
{
QApplication app(argc,argv);
QTranslator translator;
translator.load("qt_zh_CN.qm");
app.installTranslator(&translator );

myLink mainWin;
mainWin.show();
return app.exec();
};
...全文
918 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
yang22feng 2010-07-15
  • 打赏
  • 举报
回复
mark
lugaideath1 2009-03-16
  • 打赏
  • 举报
回复
up
lugaideath1 2009-03-14
  • 打赏
  • 举报
回复
定上!求高手出手帮忙!
zhoulehua 2009-03-13
  • 打赏
  • 举报
回复
等待高手
lugaideath1 2009-03-13
  • 打赏
  • 举报
回复
我指冲突的是指html方面的,因为QT有它自己html代码,就算你插入例如一个“<b>测试</b>”,然后你通过它输出html的代码,标签都变了!

还有就是,如果我插入的html还要跟前面的内容的font一致,还有插入以后再输入内容不会续上那超链接的!你运行一下我的例子就可以看到问题的了!可能我说的不太清楚!把qt4.5装上很快的!qt4.5人性化很多了!
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 lugaideath1 的回复:]
但如果直接插入html好么?插入html我知道是可以,但问题是它有提供的方法为什么不用了,因为我怕以后有别的问题要处理起来会很麻烦,或者实现不到!因为QT内部是吧html的代码重定义了的!
[/Quote]

我Qt4下的一段socket server就是用的unix系统调用,因为select里面要处理一些驱动,不能直接qsocket,一般不会有后续兼容问题的。标准C/C++以及其他的东西跟QT库都是并行的,一般不冲突。
lugaideath1 2009-03-13
  • 打赏
  • 举报
回复
如果我在
textBrowser->textCursor().insertText(trUtf8("百度"),tcf); 后再加一句textBrowser->textCursor().insertText(" ",QTextCharFormat::QTextCharFormat());
然后再写是不会续上那超链接的,但那也不太人性化了吧!而且当我退格后也会续上那超链接的!
chin_chen 2009-03-13
  • 打赏
  • 举报
回复
使用html,QTextEdit本来就支持html
zhirom 2009-03-13
  • 打赏
  • 举报
回复
tcf.setAnchor(true);
tcf.setAnchorHref("http://www.baidu.com");
if(!textBrowser->textCursor().hasSelection())
textBrowser->textCursor().insertText(trUtf8("百度"),tcf);
else
textBrowser->setCurrentCharFormat(tcf);
可不可以通过某种方式在这里限制下,如换行,或者取得地址的长度之类的,没弄过这方面的,帮顶
lugaideath1 2009-03-13
  • 打赏
  • 举报
回复
用insertHtml这方法,插入的是默认的font,和前后的font不一致!
lugaideath1 2009-03-13
  • 打赏
  • 举报
回复
还有就是,插入html后,在超连接后面继续输入内容,还是会接着那超链接的!
lugaideath1 2009-03-13
  • 打赏
  • 举报
回复
但如果直接插入html好么?插入html我知道是可以,但问题是它有提供的方法为什么不用了,因为我怕以后有别的问题要处理起来会很麻烦,或者实现不到!因为QT内部是吧html的代码重定义了的!
  • 打赏
  • 举报
回复
晕,刚发现被带了。楼主要实现超链接,怎么不直接用html啊,QTextEdit支持html。
lugaideath1 2009-03-13
  • 打赏
  • 举报
回复
openExternalLinks是干什么的呢?我设了true或者false都没有什么变化!
lugaideath1 2009-03-13
  • 打赏
  • 举报
回复
恩恩!myLink::insetLink()是不是实现插入超链接的
junying2yu 2009-03-13
  • 打赏
  • 举报
回复
up up
chin_chen 2009-03-13
  • 打赏
  • 举报
回复
openExternalLinks属性,你改下这个试试。
  • 打赏
  • 举报
回复
楼主,你先告诉我myLink::insetLink()是不是实现超链接的?我板子上才有QT4,这里虚拟机只有QT3,不好查类。
  • 打赏
  • 举报
回复
QTextCharFormat tcf;
还有这样的类了。。看看先。
lugaideath1 2009-03-13
  • 打赏
  • 举报
回复
我就是查不到!
加载更多回复(2)

64,691

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧