如何创建一个类,该类是一个在文本编辑上方具有 QTextEdit 和 QToolBar 的小部件
我的目的是创建一个 QTextEdit 及其范围文本控件。我想放入工具栏中的控件。但我在控制布局方面遇到困难。问题是,如果我将 QTextEdit 放入具有工具栏的 QWidget(我的类继承 QWidget)中,则会出现重叠。
我尝试的另一种方法如下:我的类继承 QTextEdit,并且它有一个工具栏。现在布局不同了,但不是我想要的。请帮助我欣赏美景。
My intent is to create a QTextEdit with its reach text controls. The controls I want to put in a toolbar. But I have difficulties with controling the layout. The problem is that the overlap if I put a QTextEdit in a QWidget (my class inherits QWidget) which has a toolbar.
Another way I tried was the following: my class inherits QTextEdit, and it has a toolbar. Now the layout is different but not what I want. PLease help me to have a nice view.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
前段时间我写了自己的文本编辑器,我的做法有点像你。我使用 QMainwWindow 作为 main_window,并使用一个带有布局(QVBoxLayout)的简单 QWidget 作为我的中央小部件。在该布局中,我放置了一个 QTabWidget,在其中我可以添加我自己的 text_edit(源自 QTextEdit)作为新选项卡。
我有三个工具栏,我只是将它们添加到主窗口中。这样它们就可以在我自己编写的QTextEdit周围自由移动。
我还使用 DockWidgets 添加文件资源管理器和日志记录窗口。
Some time ago I wrote my own text editor and I did it a little bit like you. I use a QMainwWindow as main_window and as my central widget a simple QWidget with a layout (QVBoxLayout) on it. In that layout I placed a QTabWidget in which I could add my own text_edit (derived from QTextEdit) as new tabs.
I had three toolbars which I simply added to the main window. So they can be moves freely around my self-wrote QTextEdit.
I also used DockWidgets to add a file explorer and a logging window.
将工具栏和 QTextEdit 放置在继承 QWidget 的类内的布局中。布局(请参阅 QVBoxLayout)相对于彼此定位项目,确保它们不会重叠。如果不使用布局,所有子窗口小部件将在位置 (0,0) 创建,即在父窗口小部件的左上角。
瞧,小部件不再重叠了。
Place your toolbar and QTextEdit in a layout inside your class which inherits QWidget. Layouts (see QVBoxLayout) positions items relative to each other making sure they don't overlap. If you don't use a layout, all child widgets will be created at position (0,0), meaning at the top-left corner of the parent widget.
And voila, the widgets don't overlap anymore.