Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Add push buttons dynamically to a widget
Forum Update on Tuesday, May 27th 2025

Add push buttons dynamically to a widget

Scheduled Pinned Locked Moved General and Desktop
8 Posts 7 Posters 20.9k Views 1 Watching
  • 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 Offline
    S Offline
    sri438
    wrote on 8 Nov 2011, 15:14 last edited by
    #1

    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
    0
    • M Offline
      M Offline
      mohsen
      wrote on 8 Nov 2011, 16:41 last edited by
      #2

      @
      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
      0
      • L Offline
        L Offline
        leon.anavi
        wrote on 8 Nov 2011, 16:42 last edited by
        #3

        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
        0
        • A Offline
          A Offline
          andre
          wrote on 8 Nov 2011, 17:12 last edited by
          #4

          [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
          0
          • K Offline
            K Offline
            Kxyu
            wrote on 8 Nov 2011, 17:17 last edited by
            #5

            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
            0
            • R Offline
              R Offline
              rokemoon
              wrote on 9 Nov 2011, 10:14 last edited by
              #6

              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
              0
              • F Offline
                F Offline
                fluca1978
                wrote on 9 Nov 2011, 11:38 last edited by
                #7

                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
                0
                • R Offline
                  R Offline
                  rokemoon
                  wrote on 9 Nov 2011, 11:53 last edited by
                  #8

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

                  1/8

                  8 Nov 2011, 15:14

                  • Login

                  • Login or register to search.
                  1 out of 8
                  • First post
                    1/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved