Add push buttons dynamically to a widget



  • hi, some one please help me ..i have a widget in setup funtion...i want to add pushbuttons into that widget dynamically i written code like this
    @
    //in setup file
    QWidget *analysis =new QWidget();
    @

    @
    // another file
    for(.....){
    .
    .
    QPushButton *pb=new QPushButtton("button");
    layout->addWidget(pb);
    }
    analysis->setlayout(layout);

    @
    i got seg fault can some one tell me where i done mistake



  • @
    QPushButton *pb=new QPushButtton("button", analysis);
    layout->addWidget(pb);
    pb.show();
    @

    regarding the layout: is it defined else where? or you want to add to parent widget layout() property? it's readonly.



  • Why haven't you set any parent of your QPushButton? Do you delete it manually?

    Please have a look at "this basic tutorial how to use QPushButton":http://developer.qt.nokia.com/wiki/How_to_Use_QPushButton as it might help you.



  • [quote author="leon.anavi" date="1320770572"]Why haven't you set any parent of your QPushButton? Do you delete it manually?[/quote]
    Putting it in a layout takes care of that automatically, as the layout will reparent any widget to its own parent widget.



  • It's okay not to set a parent, when you use a layout as it reparents added widgets. It is more likely that you're using a pointer without creating an object, like layout. P.S. Use a debugger



  • Did you create object layout or just define it?
    Like this:
    @
    //define layout without layout = new QVBoxLayout();
    QLayout *layout;
    //bla bla
    //and then
    layout->addWidget(button);
    @



  • It will be simpler to know where the segfault is generated. Try debugging each line or place some qDebug() in your code to understand at which line it happens. Usually the problem means you are trying to access an object you haven't created or you have already destroyed.



  • [quote author="fluca1978" date="1320838708"]It will be simpler to know where the segfault is generated. Try debugging each line or place some qDebug() in your code to understand at which line it happens. Usually the problem means you are trying to access an object you haven't created or you have already destroyed.[/quote]
    Oh, exactly I've forgot about debug.
    @sri438 just press F5 in QtCreator and you'll see where this error happens.


Log in to reply
 

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