自定义QWidget布局问题
我对 QT 还很陌生,我已经制作了我的第一个自定义 QWidget 子类......一切都运行良好,直到我尝试向其中添加一些标签。他们都被挤到了顶角。
这是我的代码:
ARView::ARView(QWidget *parent, const char *name) {
deviceLBL = new QLabel(this);
targetLBL = new QLabel(this);
deviceHeadingLBL = new QLabel(this);
targetHeadingLBL = new QLabel(this);
distanceLBL = new QLabel(this);
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(deviceLBL);
layout->addWidget(targetLBL);
layout->addWidget(deviceHeadingLBL);
layout->addWidget(targetHeadingLBL);
layout->addWidget(distanceLBL);
this->setLayout(layout);
this->setupLocationUpdates();
}
有谁知道我做错了什么?为什么不将所有这些标签都布置在网格中? 或者,如果是的话 - 为什么网格不使用我的子类的所有可用空间?
干杯,
詹姆斯
I'm pretty new to QT and I've made my first custom QWidget subclass...it all works well, until I try adding some labels to it. They all get squashed into the top corner.
Here is my code:
ARView::ARView(QWidget *parent, const char *name) {
deviceLBL = new QLabel(this);
targetLBL = new QLabel(this);
deviceHeadingLBL = new QLabel(this);
targetHeadingLBL = new QLabel(this);
distanceLBL = new QLabel(this);
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(deviceLBL);
layout->addWidget(targetLBL);
layout->addWidget(deviceHeadingLBL);
layout->addWidget(targetHeadingLBL);
layout->addWidget(distanceLBL);
this->setLayout(layout);
this->setupLocationUpdates();
}
Does anyone know what I'm doing wrong? Why aren't all of these labels being layed out in a grid?
Or, if they are - why isn't the grid using all my subclasse's available space?
Cheers,
James
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许这个名字具有欺骗性,但是 QVBoxLayout 并不是网格布局。这是一个垂直盒子布局。这意味着它应该按照您添加项目的顺序从上到下布局您的项目。
所以你想要的实际上是一个QGridLayout。如果您查看此类布局的文档,您将看到以下功能:
这些功能应该允许您将小部件放置在网格中的任何位置。
因此,我在下面添加的代码会将 deviceLBL 和 targetLBL 放在第一行,将 deviceHeadingLBL 和 targetHeadingLBL 放在第二行,只是为了好玩,将 distanceLBL 放在第三行,但占用 2 列的空间。
应该如下所示:
Perhaps the name is deceiving, but a QVBoxLayout is not a grid layout. It is a Vertical Box layout. Which means it is supposed to layout your items from top to bottom in the order that you add them.
So what you want is actually a QGridLayout. If you look at the documentation for this kind of layout you'll see the following functions:
These should allow you to put your widgets wherever you want in a grid.
So the code I have added below will put deviceLBL and targetLBL on the first line, deviceHeadingLBL, and targetHeadingLBL on the second line, and just for fun, distanceLBL on the third line but taking 2 columns worth of space.
Which should look like this:
创建小部件后,您需要将它们添加到网格布局中。
请参阅 QGridLayout 的 addWidget() 方法。
After you create your widgets you need to add them to grid layout.
See addWidget() method of QGridLayout.