PDA

View Full Version : how to dynamically add labels



mohanakrishnan
20th October 2009, 08:17
hi im new to qt.i installed qt 4.5 in windows xp sp2.i need to create one application in qt which is already present in c#.ok my problem is i need to create a set of labels in the form
both horizontally and vertically.the noof labels to be displayed in the form is based on the query from the database...pls help me how to do..and im bit confused with layout ,boxlayout
hboxlayout ..pls explain how to use those things...Moreover the how to place the labels in the desired position in the screen
any help is highly regarded
thanks and regards
R.Mohanakrishnan

scascio
20th October 2009, 08:35
To begin easily layout mangement, just focus on QHBoxLayout, QVBoxLayout and QGridLayout.
First of all, define your layout design that will receive widgets. You can nest layout into layouts.

Simply add widget into layouts and let Qt display the widget for you.
Use sequential QBoxLayout::addWidget for both Horizontal or Vertical layouts or QGridLayout::addWidget(row, col), to set a specified cell with a specified widget.

The steps are the following :
1. read the doc about layout
2. create a layout
3. set the layout for your widget with QWidget::setLayout
4. create your widgets
5. add widgets to layout
6. enjoy

I let you looking for the right code instructions since it is not so difficult. The doc is your friend.

Then you will notice that Qt dont mind about the order of setting layouts and adding widgets : either you can add widgets then set layout or set layout then add widget to it.

anafor2004
20th October 2009, 08:42
Dear Friends,
I am working on a new project, I want to add a form some PushButtons dynamically, so I added a VerticalLayout to form, but My PushButton Class is derived from QPushButton,
If I add QPush button to Layout I can see on form, but if I add MyButton it doesnt appear.

THIS WORKS

QWidget *window = new QWidget;
QPushButton *button1 = new QPushButton("One");
QPushButton *button2 = new QPushButton("Two");
QPushButton *button3 = new QPushButton("Three");
QPushButton *button4 = new QPushButton("Four");
QPushButton *button5 = new QPushButton("Five");

QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
layout->addWidget(button4);
layout->addWidget(button5);


THIS DOESNT

QWidget *window = new QWidget;
MyButton *button1 = new MyButton("One");
MyButton *button2 = new MyButton("Two");
MyButton *button3 = new MyButton("Three");
MyButton *button4 = new MyButton("Four");
MyButton *button5 = new MyButton("Five");

QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
layout->addWidget(button4);
layout->addWidget(button5);

scascio
20th October 2009, 09:22
May be something is missing in your button implementation, like calling base class method in overloaded one.
Please provide your code.

wysota
20th October 2009, 09:32
Your MyButton class is probably incorrect then.

mohanakrishnan
20th October 2009, 10:19
hi
thanks for this info because im newbie...
i ll follow ur suggestions..
thanks..