How to alocate a double pointer to QPushbutton



  • Hey guys,

    I need to create a number of buttons from user input. So i have a class with a member

    QPushButton** buttons;
    

    And then i have a function

    void networkDivide::generateLabelForNetwork(int value)
    {
        networkDivide::numberOfWidgets = value;
        QHBoxLayout* layout = new QHBoxLayout();
        *buttons= new QPushButton[value]();
        for(int i=0;i<value;i++)
        {
            buttons[i] = new QPushButton("Butt");
            layout->addWidget(buttons[i]);
        }
        m_networkGroup->setLayout(layout);
    }
    

    Dosen't give any error but it fails when alocate memory.
    How should I allocate a double pointer of QPushButton type?


  • Moderators

    @Adrian.Aioanei
    most easiest would be to store the pointers in one of Qt's container classes, like QList for example.
    Unless you are dependent on a pointer-to-pointer list?



  • I am not dependent of this. All i try to do is to create a number of buttons after a user input.
    I have a label how takes that value and after this, i create a number of buttons.
    How does QList help me in this case? :)



  • @Adrian.Aioanei
    e.g:

    QList<QPushButton *> btnList;
    
    for(int i = 0; i < 10; i++){
        QPushButton *btn = new QPushButton();
        QString text = QString::number(i);
        btn->setText(text);
        btn->setObjectName("MyButton"+text);
       .....
       
        btnList.append(btn);
    }
    


  • This looks ok I will try it.
    One more question...the button is created in for loop. This pointer will not be deleted after the function reach "}" ?



  • @Adrian-Aioanei
    QPushButton **buttons;

    void networkDivide::generateLabelForNetwork(int value)
    {
    networkDivide::numberOfWidgets = value;
    QHBoxLayout* layout = new QHBoxLayout();
    buttons= new QPushButton *[value];
    for(int i = 0; i < value; i++) {
    buttons[i] = NULL;
    }
    for(int i=0;i<value;i++)
    {
    buttons[i] = new QPushButton("Butt");
    layout->addWidget(buttons[i]);
    }
    m_networkGroup->setLayout(layout);
    }


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.