Opening dynamically created buttons
-
I tried to make some dynamically created buttons but the appllication closed when I try to do it or make every buttons in one position in a frame (when a I do it in new window it works but I need in a special created frame several buttons)
void MainWindow::createButtons(){
QVBoxLayoutlayout=qobject_cast<QVBoxLayout>(ui->frame->layout());
//this line makes a problem// QVBoxLayout *layout=new QVBoxLayout(); //this works
QPushButton *przyc =new QPushButton(buttontext, ui->frame);
layout->insertWidget(1,przyc);
QPushButton *przyc2 =new QPushButton(buttontext, ui->frame);
layout->insertWidget(2,przyc2)
przyc->show();
przyc2->show();}
My QT creator 2.7.2 on base qt 5.1.0 version under linux mint
-
Hi and welcome to devnet,
@Romi11 said in Opening dynamically created buttons:
QVBoxLayoutlayout=qobject_cast<QVBoxLayout>(ui->frame->layout());
You do not check whether the value is null.
I guess that you did not apply a layout on your frame in designer.
-
@Romi11
Hi
Well, Its unclear why layout() didn't work.
It does return the layout for me in a small example.So I think maybe you changed the type of the layout
and hence when you cast it becomes zero as it was not the right type.Or something like that.