使用 QT 为自定义标记语言构建所见即所得编辑器
我是 QT 新手,正在尝试找出为显示简单文本、图像和链接的自定义标记语言创建所见即所得编辑器小部件的最佳方法。我需要能够将更改从所见即所得编辑器传播到自定义标记表示。
作为问题域的具体示例,假设自定义标记可能有一个“玩家”标签,其中包含玩家名称和团队名称。标记可能如下所示: <代码>
Last week, <player id="1234"><name>Aaron Rodgers</name><team>Packers</team></player> threw a pass.
This text would display in the editor as:玩家姓名和球队名称可以在编辑器中以标准所见即所得方式直接编辑,这样我的用户就不必学习任何标记。此外,当鼠标悬停在球员姓名上时,将会弹出有关该球员的详细信息,对于球队也是如此。
通过这么长的介绍,我试图弄清楚从哪里开始使用 QT。看起来最合乎逻辑的选择是使用 QTextDocument 的 Rich Text API。考虑到 QTextDocument 的限制,这种方法似乎不太理想:
- 我不知道如何通过单击链接来捕获导航事件。
- 单击后的链接似乎仅在 QTextEdit 为只读时才启用。
- 实现 QTextObjectInterface 的自定义对象在复制和粘贴操作中会被忽略。
- 任何以 Rich Text 形式传递给它的基于 HTML 的标记都会被重新转换为一系列 span 标记和许多其他垃圾,这使得从 QTextObjectInterface 传播更改变得极其困难。编辑器返回到原始的自定义标记。
第二个选项似乎是 QWebKit,它允许实时编辑 HTML5 标记,因此我可以指定自定义标记和 HTML5 之间的双向转换。我不清楚如何将更改从编辑器实时传播回原始标记,而无需在每次文本更改时重新翻译整个文档。对于我(学习 WebKit 和 QT)来说,QWebKit 解决方案看起来非常庞大,而这应该是一个相对简单的问题。
我还考虑过使用本机 QT 容器、标签、图像和其他小部件手动使用自定义类来实现所见即所得。这似乎是最灵活的方法,而且最有可能不会遇到无法解决的问题。但是,我非常确定实现普通文本编辑器的所有细节(选择文本、字体更改、剪切和粘贴支持、撤消/重做、拖动对象、光标放置等)将非常耗时。
最后,我的问题是:是否有 QT 专家对从哪里开始此类项目提供一些建议?
顺便说一句,我使用 QT 是因为该应用程序是需要平台独立性的桌面应用程序。
I'm new to QT, and am trying to figure out the best means of creating a WYSIWYG editor widget for a custom markup language that displays simple text, images, and links. I need to be able to propagate changes from the WYSIWYG editor to the custom markup representation.
As a concrete example of the problem domain, imagine that the custom markup might have a "player" tag which contains a player name and a team name. The markup could look like this:
Last week, <player id="1234"><name>Aaron Rodgers</name><team>Packers</team></player> threw a pass.
This text would display in the editor as:
Last week, Aaron Rodgers of the Packers threw a pass.
The player name and the team name would be editable directly within the editor in standard WYSIWYG fashion, so that my users do not have to learn any markup. Also, when the player name is moused-over, a details pop-up will appear about that player, and similarly for the team.
With that long introduction, I'm trying to figure out where to start with QT. It seems that the most logical option would be the Rich Text API using a QTextDocument. This approach seems less than ideal given the limitations of a QTextDocument:
- I can't figure out how to capture navigation events from clicking on links.
- Following links on click seems to only be enabled when the QTextEdit is readonly.
- Custom objects that implement QTextObjectInterface are ignored in copy-and-paste operations
- Any HTML-based markup that is passed to it as Rich Text is retranslated into a series of span tags and lots of other junk, making it extremely difficult to propagate changes from the editor back to the original custom markup.
A second option appears to be QWebKit, which allows for live editing of HTML5 markup, so I could specify a two-way translation between the custom markup and HTML5. I'm not clear on how one would propagate changes from the editor back to the original markup in real-time without re-translating the entire document on every text change. The QWebKit solutions looks like awfully bulky to me (Learning WebKit along with QT) to what should be a relatively simple problem.
I have also considered implementing the WYSIWYG with a custom class using native QT containers, labels, images, and other widgets manually. This seems like the most flexible approach, and the one most likely not to run into unresolvable problems. However, I'm pretty sure that implementing all the details of a normal text editor (selecting text, font changes, cut-and-paste support, undo/redo, dragging of objects, cursor placement, etc.) will be incredibly time consuming.
So, finally, my question: are there any QT gurus out there with some advice on where to start with this sort of project?
BTW, I am using QT because the application is a desktop application that needs platform independence.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
鉴于我在这里没有得到任何建议,我决定采用
QTextEdit
方法,尽管我实际上使用的是设置为可编辑的QTextBrowser
以便我可以捕获链接导航事件。我将使用QTextCharFormat
并将链接名称设置为唯一标识符,以便从QTextEdit
转换回自定义标记。QTextEdit
已经支持图像,所以我不必处理这些。我认为我将遇到最大的障碍,因为我需要能够插入/增长/收缩其单元格可以具有 Excel 样式功能的表格。我还没有弄清楚整个过程。
Given that I got no advice here, I decided to go with the
QTextEdit
approach, although I'm actually using aQTextBrowser
that is set to be editable so that I can capture link navigation events. I will be usingQTextCharFormat
's with the link names set to unique identifiers in order to convert from theQTextEdit
back to the custom markup. TheQTextEdit
supports images already, so I won't have to deal with those.I think I will hit the biggest roadblocks with the fact that I need to be able to insert/grow/shrink tables whose cells can have Excel-style functionality. I have not yet figured that whole process out.