Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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


  • Lifetime Qt Champion

    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.



  • You right the pointer is null however I applied in Qt creator layout and frame . How to do it correctly?


  • Lifetime Qt Champion

    @Romi11

    Hi
    Go to Designer and look what the Layout really is named.

    alt text

    Then it's available as
    ui->verticalLayout

    and see if that works;

    Also you can avoud casting as its of correct layout type already.



  • now it works how I supposed. I think its difficult to achieve it, without your support I would have spent many hours until find it correctly


  • Lifetime Qt Champion

    @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.



  • @mrjj I think I do it how in example but the philosophy of qt frames and layouts I should to learn more.
    Thanks for quick answers :)


Log in to reply