GtkVBox Qt 等效项

发布于 2024-10-04 21:52:39 字数 702 浏览 0 评论 0原文

在 GTK 中,我曾经有一个窗口,我在其中使用 gtk_container_add()'da GtkVBox。然后我可以将我想要的小部件打包到 GtkVBox,让它们出现在窗口中。

现在我决定尝试一下 Qt,但我似乎不知道如何在 Qt 中做到这一点。

现在我所做的是创建一个 QMainWindow,但我发现你只能将一个主窗口部件打包到其中,这显然是相当有限的。所以我想创建类似 GtkVBox 的东西并将其用作主要小部件,然后将其他小部件添加到此框中。

然而,我通过谷歌搜索发现的只有 Q3VBox 小部件(这似乎是我想要的,但已被弃用)和 QVBoxLayout。

我尝试使用 QVBoxLayout,但它告诉我无法更改 QMainWindow 的布局,因为它已经有一个布局。

编辑:这是我的做法(这是在构造函数中):

box = new QVBoxLayout;
setLayout(box)

它编译得很好,但在运行时,它在控制台上打印: QWidget::setLayout:尝试在 HCGWindow“” 上设置 QLayout“”,该窗口已经有布局 (HCGWindow 是我的应用程序的窗口,它是 QMainWindow 的子类)

那么,我如何在 Qt 中创建类似于 GtkVBox 的东西,如果解决方案是 Q3VBox,为什么它被弃用以及我应该使用什么其他东西?

谢谢

In GTK, I used to have a window to which I gtk_container_add()'d a GtkVBox. Then I could pack widgets I wanted to the GtkVBox to have them appear in the window.

Now I've decided to try out Qt, but I can't seem to figure out how to do this in Qt.

Right now what I've done is create a QMainWindow, but I found that you can only pack one main widget into it, which is obviously quite limiting. So I wanted to create something like the GtkVBox and use that as the main widget and then add other widgets to this box.

What I've found by Googling are however only the Q3VBox widget, which seems to be what I want, but is deprecated, and the QVBoxLayout.

I tried to use the QVBoxLayout, but it tells me that I cannot change the layout of my QMainWindow since it already has a layout.

Edit: Here is how I do it (this is in the constructor):

box = new QVBoxLayout;
setLayout(box)

It compiles fine, but during runtime, it prints on the console:
QWidget::setLayout: Attempting to set QLayout "" on HCGWindow "", which already has a layout
(HCGWindow is my app's window, which is a subclass of QMainWindow)

So, how can I create something similar to a GtkVBox in Qt, and if the solution is the Q3VBox, why is it deprecated and what other thing should I use?

Thanks

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

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

发布评论

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

评论(2

独木成林 2024-10-11 21:52:39

事实上,这是 Qt 文档 提供的推荐解决方案。

您应该创建一个 QVBoxLayout 并在其中添加您想要的小部件。之后,您在另一个空小部件上设置布局,然后将此小部件设置为 QMainWindow 子类的中心小部件。下面是一个代码示例:

QWidget* widget1 = new QWidget(); // This could be anything subclassing QWidget.
QWidget* widget2 = new QWidget();

QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(widget1);
layout->addWidget(widget2);

QWidget* central = new QWidget(); // Only a containing QWidget.
central->setLayout(layout);

this->setCentralWidget(central);

现在,您的 QMainWindow 子类中应该有两个 QWidget,排列在 QVBoxLayout 中。

请注意,我没有将任何父母交给任何人。您本来可以这样做,但是当您调用 addWidgetsetCentralWidget 时,小部件(和布局)的所有权将授予包含类。

如果您读过一些有关 Qt 的内容,您就会知道这允许父级在其自身即将被销毁时销毁其子级。

最后,请注意,QMainWindow 是一个例外,据我所知,它是唯一使用 setCentralWidget 作为方法的类。如果您尝试创建 QWidget 子类,您将能够使用 setLayout (如上面的示例所示)。

希望这有帮助。

In fact, here is the recommended solution provided by the Qt documentation.

You should create a QVBoxLayout and add the widgets you want in it. After that, you set the layout on another empty widget and then you set this widget as the central widget of the QMainWindow subclass. Here is an example in code:

QWidget* widget1 = new QWidget(); // This could be anything subclassing QWidget.
QWidget* widget2 = new QWidget();

QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(widget1);
layout->addWidget(widget2);

QWidget* central = new QWidget(); // Only a containing QWidget.
central->setLayout(layout);

this->setCentralWidget(central);

Now, you QMainWindow subclass should have the two QWidgets in it, arranged in a QVBoxLayout.

Note here that I did not give any parent to anyone. You could have done it, but when you call addWidget or setCentralWidget, the ownership of the widget (and the layout) is given to the containing class.

If you read a bit about Qt, you'll know that this allows the parent to destroy his children when he is about to be destroyed itself.

Finally, note that QMainWindow is an exception and, from what I know, is the only class with setCentralWidget as a method. If you attempt to create a QWidget subclass, you will be able to use setLayout (as shown in the example above).

Hope this helps.

糖粟与秋泊 2024-10-11 21:52:39

尝试创建 QVBoxLayout 实例,向其中添加小部件并将此布局添加(而不是替换)到主窗口的布局中。

注意,QLayout 类没有成员 addLayout,但子类有一个。

首先,您必须获取并记住主窗口布局的类名:

qDebug(this.layout()->objectName);

然后将 QVBoxLayout 添加到窗口布局:

dynamic_cast<YourWindowLayoutClass>(
    this.layout())->addLayout(your_qvboxlayout_object);

我希望它能起作用。

Try to create QVBoxLayout instance, add your widgets to it and add (not replace) this layout to main window's layout.

Note, QLayout class has no member addLayout, but subclasses has one.

Firstly you must get and remember classname of main window's layout:

qDebug(this.layout()->objectName);

Then add your QVBoxLayout to window's layout:

dynamic_cast<YourWindowLayoutClass>(
    this.layout())->addLayout(your_qvboxlayout_object);

I hope it will work.

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