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 PushButtons dinamically to Layout and define functionality ?
Forum Updated to NodeBB v4.3 + New Features

Add PushButtons dinamically to Layout and define functionality ?

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 3 Posters 2.9k Views 2 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.
  • NiagarerN Niagarer

    @mrjj
    The programm just crashes.
    But I think, I got a solution for this some seconds ago:
    In the costructor:

        ui->scrollArea->setLayout(new QVBoxLayout);
    

    In the function:

        QPushButton *newButton = new QPushButton(ui->LineEdit->text(), this);
        ui->scrollArea->layout()->addWidget( newButton );
    

    (problem is, I cant scroll in this scrollArea... :D)

    mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #4

    @Niagarer
    Seems ok.
    You can first scroll when enough buttons are there.

    NiagarerN 1 Reply Last reply
    1
    • mrjjM mrjj

      @Niagarer
      Seems ok.
      You can first scroll when enough buttons are there.

      NiagarerN Offline
      NiagarerN Offline
      Niagarer
      wrote on last edited by Niagarer
      #5

      @mrjj
      I addad about 30 buttons, nothing happens.
      They just became smaller and smaller...

      mrjjM 1 Reply Last reply
      0
      • NiagarerN Niagarer

        @mrjj
        I addad about 30 buttons, nothing happens.
        They just became smaller and smaller...

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #6

        @Niagarer

        Yes, unless u set minimumHeight on them, they will become smaller as its allowed.

        newButton->setMinimumHeight(200);

        NiagarerN 1 Reply Last reply
        1
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #7

          You should not set the layout on the scroll area. You should create a widget that will be the content of the scroll area and add buttons to its layout, i.e.

          QWidget* content = new QWidget();
          content->setLayout(new QVBoxLayout);
          
          content->layout()->addWidget(new QPushButton(ui->LineEdit->text()));
          //add other buttons
          
          ui->scrollArea->setWidget(content);
          
          1 Reply Last reply
          1
          • mrjjM mrjj

            @Niagarer

            Yes, unless u set minimumHeight on them, they will become smaller as its allowed.

            newButton->setMinimumHeight(200);

            NiagarerN Offline
            NiagarerN Offline
            Niagarer
            wrote on last edited by
            #8

            @mrjj
            Hm. When I try this, I get this:
            0_1501326477263_Qt 1.png

            Unfortunately still no scrolling.

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #9

              That's because you're changing the layout of the scroll area itself. You should be changing the layout of the content.

              1 Reply Last reply
              3
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #10

                Hi
                Do as @Chris-Kawa says.
                Im used to do it in Designer. ( so didnt spot it)
                It does make a content widget for you.
                You need it.
                alt text

                NiagarerN 1 Reply Last reply
                3
                • mrjjM mrjj

                  Hi
                  Do as @Chris-Kawa says.
                  Im used to do it in Designer. ( so didnt spot it)
                  It does make a content widget for you.
                  You need it.
                  alt text

                  NiagarerN Offline
                  NiagarerN Offline
                  Niagarer
                  wrote on last edited by
                  #11

                  @mrjj
                  @Chris-Kawa

                  Yes, it works, thanks!
                  Now I can scroll.
                  I think, I got the answer for my second question, I just have to use connect.
                  I will update, when I am finished.
                  Thank you!

                  mrjjM 1 Reply Last reply
                  1
                  • NiagarerN Niagarer

                    @mrjj
                    @Chris-Kawa

                    Yes, it works, thanks!
                    Now I can scroll.
                    I think, I got the answer for my second question, I just have to use connect.
                    I will update, when I am finished.
                    Thank you!

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #12

                    @Niagarer
                    Yes, just connect the buttons clicked or released signal to a slot
                    Note that inside a slot, you can use
                    sender() to know the QObject that sent the signal.

                    But it depends on what the buttons do if each should have own slot or they can share.

                    NiagarerN 1 Reply Last reply
                    1
                    • mrjjM mrjj

                      @Niagarer
                      Yes, just connect the buttons clicked or released signal to a slot
                      Note that inside a slot, you can use
                      sender() to know the QObject that sent the signal.

                      But it depends on what the buttons do if each should have own slot or they can share.

                      NiagarerN Offline
                      NiagarerN Offline
                      Niagarer
                      wrote on last edited by
                      #13

                      @mrjj
                      Ok.
                      Now I have a (little) problem left: I do not get it to give the Object itself to the slot.
                      What I need is the name of the button, because this name is similar to a name in a vector, where I have some objects (when I press the button, I want to print information about the object that has this name).
                      I tried it with sender() but then I can't get the name of it.

                      mrjjM 1 Reply Last reply
                      0
                      • NiagarerN Niagarer

                        @mrjj
                        Ok.
                        Now I have a (little) problem left: I do not get it to give the Object itself to the slot.
                        What I need is the name of the button, because this name is similar to a name in a vector, where I have some objects (when I press the button, I want to print information about the object that has this name).
                        I tried it with sender() but then I can't get the name of it.

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by mrjj
                        #14

                        @Niagarer
                        Hi
                        You must cast it to the expected type.
                        sender() is a baseclass pointer and while it can point to any QObject, you must
                        cast it yourself to have access.

                        QPushButton *mybut=qobject_cast<QPushButton *>( sender() );
                        if (mybut) {
                        QString bname=mybut->objectxxx
                        }

                        NiagarerN 1 Reply Last reply
                        1
                        • mrjjM mrjj

                          @Niagarer
                          Hi
                          You must cast it to the expected type.
                          sender() is a baseclass pointer and while it can point to any QObject, you must
                          cast it yourself to have access.

                          QPushButton *mybut=qobject_cast<QPushButton *>( sender() );
                          if (mybut) {
                          QString bname=mybut->objectxxx
                          }

                          NiagarerN Offline
                          NiagarerN Offline
                          Niagarer
                          wrote on last edited by
                          #15

                          @mrjj
                          Jep, it works.
                          Thanks man, yout are a really good help!

                          mrjjM 1 Reply Last reply
                          0
                          • NiagarerN Niagarer

                            @mrjj
                            Jep, it works.
                            Thanks man, yout are a really good help!

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #16

                            @Niagarer
                            np. Are the buttons names 100% the same as in list ?
                            Im wondering if you could use a QMap for the extra info lookup.

                            NiagarerN 1 Reply Last reply
                            1
                            • mrjjM mrjj

                              @Niagarer
                              np. Are the buttons names 100% the same as in list ?
                              Im wondering if you could use a QMap for the extra info lookup.

                              NiagarerN Offline
                              NiagarerN Offline
                              Niagarer
                              wrote on last edited by
                              #17

                              @mrjj
                              Yes they are, but I choosed this way, because the vector with the objects is inside an object of a custom widget.
                              Here I am in MainWindow. But the objects, I need are in a custom widget class, that is a part of the window.

                              1 Reply Last reply
                              0

                              • Login

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