Is it possible to add ui objects (buttons, text boxes etc) to a visible qt window, i.e. I have a window that has a QTextEdit where the user can add a number, then there is a button to add another QTetEdit for another number and a button to remove it. This means that i can add an arbitrary number of fields and later on i can perform some sort of calculation on the all the elements, because I need to be able to add an arbitrary number of extra objects so showing and hiding elements would probably be inefficient. This is a made up example but it demonstrates the purpose of what I’m after.
Has anyone done anything similar?
— update —
Heres some code I am trying thats not working for me, the window is already visible when the code runs:
void MainWindow::addArgument()
{
QPushButton button;
ui->addArgRowHorizontalLayout->addWidget(&button);
button.show();
this->show();
}
anybody know what im doing wrong, everything autocompletes in qtcreator and compiles with no errors so i dont know where to go next.
j
I’m refering to contents of update.
Have you moved to C++ from C#/Java?
This instruction creates QPushButton object on stack, and objects on stack are destroyed when their scope ends, which in this case is function’s closing bracket. To make an object persistant you should allocate it on heap:
Of course, you have to use pointer semantics from now on.
Typically in C++ you have to manually delete objects from heap when your application’s logic don’t need them any longer. Thankfully, Qt keeps track of QObject, so you rarely have to dispose them by yourself. A good place to start C++ is C++ FAQ. Also, you should go through Qt manual, it’s really easy to read and get Qt’s concepts.