Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Add push buttons dynamically to a widget

    General and Desktop
    7
    8
    20062
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      sri438 last edited by

      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

      1 Reply Last reply Reply Quote 0
      • M
        mohsen last edited by

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

        1 Reply Last reply Reply Quote 0
        • L
          leon.anavi last edited by

          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.

          http://anavi.org/

          1 Reply Last reply Reply Quote 0
          • A
            andre last edited by

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

            1 Reply Last reply Reply Quote 0
            • K
              Kxyu last edited by

              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

              1 Reply Last reply Reply Quote 0
              • R
                rokemoon last edited by

                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);
                @

                1 Reply Last reply Reply Quote 0
                • F
                  fluca1978 last edited by

                  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.

                  1 Reply Last reply Reply Quote 0
                  • R
                    rokemoon last edited by

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

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post