tags:

views:

71

answers:

2

Hi, I am having a widget with a push button. I want, for every click on the push button one label should be added in the widget. I am giving the code below, but is not working. I don't know why. Somebody help me?

class EditThingsWindow:public QWidget
{
QPushButton * add;
 Q_OBJECT
public:
EditThingsWindow();
public slots:
void addButtonClicked();
};

EditThingsWindow::EditThingsWindow():QWidget()
{
QWidget * wid=this;
wid->resize(400,400);

add=new QPushButton(wid);
add->setText("Add");
add->move(20,10);
line=new QLineEdit(wid);
line->move(30,50);

QObject::connect(add,SIGNAL(clicked()),this,SLOT(addButtonClicked()));
}

void EditThingsWindow::addButtonClicked()
{
QLabel * label=new QLabel(this);
label->move(200,160);
label->setText(";;;;;;;;;;;;;;");

}
+1  A: 

Just add layout and place your newborn labels into it.

 QHBoxLayout *layout = new QHBoxLayout; // or some another QLayout descendant
 layout->addWidget(newWidget);

 widget->setLayout(layout);

the only place I had to change is add layout into Widget and then

void EditThingsWindow::addButtonClicked() 
{
    QLabel * label=new QLabel(this);
    layout->addWidget(label);
    label->move(200,160);
    label->setText(";;;;;;;;;;;;;;");
}

got things done.

If you MUST (you don't!) mess with absolute positioning, you should do all these boilerplate code by yourself. Headers and includes omitted.

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    EditThingsWindow w(0);
    w.show();
    return a.exec();
}

EditThingsWindow::EditThingsWindow(QWidget *parent):QWidget(parent)
{
i = 0;
setGeometry(2, 2, 400, 400);
add=new QPushButton(this);
add->setGeometry(2, 2, 100, 20);
add->setText("Add");
add->move(20,10);

QObject::connect(add,SIGNAL(clicked()),this,SLOT(addButtonClicked()));
}

void EditThingsWindow::addButtonClicked()
{
QLabel * label=new QLabel(this);
label->setGeometry(10, 30 + i* 30, 50, 20);
i++;
label->setText(";;;;;;;;;;;;;;");
label->show();
}
Max
@Max I want to put the label where ever I want. But if I use any layouts, then, I have to lose that choice. Because Horizontal layout will put the widgets in horizontal way, and vertical layout will put widgets in vertical way
prabhakaran
@prabhakaran Then, if you need not to use layouts (VERY strange, because you then must handle absolute position of every control after every form position/size change). Layouts are flexible and powerful, you should learn them.But. If you want to mess with absolute pos, then: label->move(10 + i* 30, 10), i = {1, 2, 3} :) It works:)
Max
@prabhakaran I modified your example, it works with absolute positioning, but again, read about layouts, they are extremely powerful and there is no (almost :) ) app layout (sorry) you can't achieve with them.
Max
@prabhakaran: You can use a QTableLayout or create your own one too. If you work with absolute positions, you will have to manage window resizing or forbid resizing.
Patrice Bernassola
+4  A: 

A new QLabel is indeed added to the EditThingsWindow every time you click on the button. However, since the labels are not placed in a layout, and they are all moved at the same position with the same text (hence the same size), they all appear on top of each other, and you can only see the top one, which is probably why you think they are not being added.

Add a layout to the EditThingsWindow widget, and add each new QLabel to the layout, and you will see all the labels appear.

Fred
@Fred, The situation is worse than you said. According to you at least one label should be shown. But I didn't see any label.
prabhakaran
@prabhkaran: try calling label->show() in the addbuttonClicked() method. A newly created widget might not be automatically visible if not inserted into a layout.
Fred
@Fred You are correct. After I called label->show(), it worked. Thank you man.
prabhakaran