开发者

custom QWidget layout problems

开发者 https://www.devze.com 2023-03-27 05:06 出处:网络
I\'m pretty new to QT and I\'ve made my first custom QWidge开发者_如何学Ct subclass...it all works well, until I try adding some labels to it. They all get squashed into the top corner.

I'm pretty new to QT and I've made my first custom QWidge开发者_如何学Ct 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


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:

void    addWidget ( QWidget * widget, int row, int column, Qt::Alignment alignment = 0 )
void    addWidget ( QWidget * widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt::Alignment alignment = 0 )

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.

QGridLayout *layout = new QGridLayout ();

layout->addWidget(deviceLBL, 0, 0);
layout->addWidget(targetLBL, 0, 1);
layout->addWidget(deviceHeadingLBL, 1, 0);
layout->addWidget(targetHeadingLBL, 1, 1);
layout->addWidget(distanceLBL, 2, 0, 1, 2);

Which should look like this:

custom QWidget layout problems


After you create your widgets you need to add them to grid layout.

See addWidget() method of QGridLayout.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号