如何创建一个类,该类是一个在文本编辑上方具有 QTextEdit 和 QToolBar 的小部件

发布于 2024-09-17 09:43:29 字数 193 浏览 2 评论 0原文

我的目的是创建一个 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 技术交流群。

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

发布评论

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

评论(2

国际总奸 2024-09-24 09:43:42

前段时间我写了自己的文本编辑器,我的做法有点像你。我使用 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.

alt text

扛刀软妹 2024-09-24 09:43:39

将工具栏和 QTextEdit 放置在继承 QWidget 的类内的布局中。布局(请参阅 QVBoxLayout)相对于彼此定位项目,确保它们不会重叠。如果不使用布局,所有子窗口小部件将在位置 (0,0) 创建,即在父窗口小部件的左上角。

QWidget* widget = new QWidget();
QToolBar* toolbar = new QToolBar(widget);
QTextEdit* textedit = new QTextEdit(widget);

QVBoxLayout* layout = new QVBoxLayout(widget);
layout->addWidget(toolbar);
layout->addWidget(textedit);

瞧,小部件不再重叠了。

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.

QWidget* widget = new QWidget();
QToolBar* toolbar = new QToolBar(widget);
QTextEdit* textedit = new QTextEdit(widget);

QVBoxLayout* layout = new QVBoxLayout(widget);
layout->addWidget(toolbar);
layout->addWidget(textedit);

And voila, the widgets don't overlap anymore.

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