Qt - 子类化以提供文本小部件的替代视图?

发布于 2024-11-15 15:01:31 字数 962 浏览 2 评论 0原文

目前,我正处于 Qt 小部件的设计阶段,就像人们在典型的十六进制编辑器中看到的那样。一开始它看起来很简单,但是当我深入研究它的实现细节时,我遇到了一些困惑。

基本上,该小部件将由 3 个核心组件组成:它将继承 QAbstractScrollArea 或 QScrollArea 以提供滚动,并且在视口边缘中,它将显示每行的文件偏移量。然后会有两个文本编辑器;一个是文件每个字节的十六进制值,另一个是明文字符表示形式。

当然,我首先检查了 qt-apps.org 是否有任何现有的小部件,但搜索“hex”只返回了 QHexEdit 和 qPHexEditor,这两个都不是很完整。然后我考虑像他们一样完全从头开始创建一个小部件,但觉得应该有一个更优雅的解决方案。 Qt 已经在 QTextEdit 和 QPlainTextEdit 中内置了许多文本编辑器功能;为什么要重新发明轮子?

现在,虽然“纯文本视图”就像使用具有固定宽度字体和 16 个字符宽度的 QPlainTextEdit 一样简单,但“十六进制视图”却让我头疼。我一直在研究 QTextEdit、QAbstractTextDocumentLayout 等,试图找出一种呈现所需外观的方法。对于那些从未使用过十六进制编辑器的人来说,它的功能应该是这样的: – 使用固定宽度的字体,小部件应该是 47 个字符的宽度 – 小部件应该每个字节显示 2 个十六进制字符,字节之间有一个空格——每行 16 个字节

自这个想法以来,我一直在尝试找出如何对任何相关类进行子类化以提供所需的格式。不幸的是,文本编辑类似乎没有像我希望的那样严格遵循模型/视图框架,因此为其派生一个新的“视图”似乎并不容易。理想情况下,小部件的功能如下:

一个文档/模型同时用于“十六进制”和“纯文本”视图。编辑任一视图都会调整此模型并相应地更新另一个视图。信号/槽处于最佳状态。 因为 QTextEdit 和 QPlainTextEdit 已经提供了许多所需的功能(可见光标、选择、撤消/重做、本机外观和感觉等),因此重用它是理想的选择。

那么,有人有什么建议吗?我很感谢对此的任何意见。

Currently, I am in the design phase of a Qt widget like what one would see in a typical hex editor. It seemed simple enough to begin with, but as I dig into its implementation details I’m having some confusion.

Basically, the widget would consist of 3 core components: It will inherit QAbstractScrollArea or QScrollArea to provide scrolling and, in the viewport margin, it will display the file offset of each line. Then there will be two text editors; one with the hexadecimal value of each byte of the file, and one with the plaintext character representation.

I, of course, first checked qt-apps.org for any existing widgets, but a search for “hex” only returned QHexEdit and qPHexEditor, neither of which are very complete. I then considered creating a widget completely from scratch as they had, but felt like there should be a more elegant solution. Qt already has much text-editor functionality built into QTextEdit and QPlainTextEdit; why reinvent the wheel?

Now, while the “plaintext view” would be as simple as using a QPlainTextEdit with a fixed-width font and a width of 16 characters, the “hex view” is giving me a headache. I’ve been poring over QTextEdit, QAbstractTextDocumentLayout, etc., trying to figure out a way to present the desired appearance. For those who have never used a hex editor, it should function like so: – Using a fixed-width font, widget should be the width of 47 characters – Widget should display 2 hexadecimal characters per byte, with a blank space between bytes—-16 bytes per line

Since that thought, I’ve been trying to figure out how to subclass any related classes to provide the desired formatting. Unfortunately, the text editing classes don’t seem to follow the model/view framework as closely as I’d hoped, so deriving a new “view” for it doesn’t seem easy. Ideally, the widget would function like so:

One document/model for both the “hex” and “plaintext” views. Editing either view would adjust this model and update the other view appropriately. Signals/Slots at its best.
Because QTextEdit and QPlainTextEdit already provide much of the functionality needed (visible cursor, selections, undo/redo, native look and feel, etc), it would be ideal to re-use this.

So, does anyone have any recommendations? I appreciate any input on this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白馒头 2024-11-22 15:01:31

QHexEdit2 是一个非常完整的二进制数据编辑器小部件。它可以编辑非常大的文件,适用于 Qt4、Qt5、PyQt4、PyQt4 with python 2 和 3。

请参阅 https:// /github.com/Simsys/qhexedit2

QHexEdit2 is a quite complete editor widget for binary data. It can edit very big files, is available for Qt4, Qt5, PyQt4, PyQt4 with python 2 and 3.

see https://github.com/Simsys/qhexedit2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文