Insert element per line of code in a Qt C++ form

Asked

Viewed 20 times

0

I want to insert an element in a Qt form, but I can’t use the designer because the element should only appear when a button is clicked. Is there anything like a ui->addWidget method or some other way to do this?

1 answer

-2

all right?

The direct answer is yes, there is an addWidget method in the layouts, so you add the layout in Designer, and via code vc adds the desired field.

ui->layout->addWidget(lineEdit);

adição dinâmica

there is another way that can be useful and you can do using the designer, which is using the setVisible(false) function, you can group several widgets within a layout, and promote this layout to the Widget

Promovendo Layout

After that you can make this layout invisible soon in the constructor, and use the button to change the visibility when you want

Usando controle de Visibilidade

TelaAddWidget::TelaAddWidget(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::TelaAddWidget)
{
    ui->setupUi(this);
    ui->layout_widget->setVisible(false);
    mostrar = false;
}

void TelaAddWidget::on_ocultar_clicked()
{
    mostrar = !mostrar;
    ui->layout_widget->setVisible(mostrar);
}

Note: when promoting a layout a widget vc actually generates a new widget that will not have the same name as the previous one, so you need to be aware of the new name generated to access via code, in my example I rename it to layout_widget.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.