You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Thursday, October 7th 2010, 5:04pm

QVBoxLayout to center

when i use the code below, Qt puts labels on the right side.
How can i put labels centered in the line?



Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
myproject::myproject(QWidget *parent, Qt::WFlags flags): QMainWindow(parent, flags){ui.setupUi(this);

{
QVBoxLayout *mainlayout = new QVBoxLayout;
QLabel *label1= new QLabel("aaa");
QLabel *label2= new QLabel("bbb");
QLabel *label3= new QLabel("cccc");
QLabel *label4= new QLabel("ddd");
mainlayout->addWidget(label1);
mainlayout->addWidget(label2);
mainlayout->addWidget(label3);
mainlayout->addWidget(label4);
ui.centralWidget->setLayout(mainlayout);

}

2

Thursday, October 7th 2010, 5:17pm

You only have to do the following:

Source code

1
mainlayout->addWidget(label1, Qt::AlignCenter);


for all of your labels.
Hope this helps.

3

Thursday, October 7th 2010, 5:35pm

unfortunately it didn't work.

4

Thursday, October 7th 2010, 6:18pm

this has worked.
thank you very much.

Source code

1
mainlayout->setAlignment(Qt::AlignCenter);