Reputation: 421
I'm trying to build a QLabel
that I'd like to appear when I press a QPushButton
; code is simple:
void Mod28::on_pushButton_clicked()
{
AddItem();
}
void Mod28::AddItem()
{
QLabel *label_n = new QLabel(this);
label_n->setObjectName(QStringLiteral("label_n"));
label_n->setGeometry(QRect(20, DimVert, 25, 17));
label_n->setFrameShape(QFrame::Box);
label_n->setNum(ItemCounter);
}
Nothing appears. Instead, if I put AddItem()
inside the following:
Mod28::Mod28(QWidget *parent) :
QWidget(parent),
ui(new Ui::Mod28)
{
ui->setupUi(this);
AddItem(); // here, for example, is ok !!!!
}
How can I solve? I need a QLabel
appearing when I press the button.
Upvotes: 2
Views: 67
Reputation: 4181
Add your label to the widget that you want to show in it.
for example this line add lable to centralWidget layout:
ui->centralWidget->layout()->addWidget(label_n);
your function:
void Mod28::AddItem()
{
QLabel *label_n = new QLabel(this);
//
//
//
ui->centralWidget->layout()->addWidget(label_n);
}
Upvotes: 3