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. Opening dynamically created buttons
Forum Updated to NodeBB v4.3 + New Features

Opening dynamically created buttons

Scheduled Pinned Locked Moved Solved General and Desktop
dynamicbuttonclickqstringlist
11 Posts 4 Posters 13.2k 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.
  • F Offline
    F Offline
    Fuel
    wrote on 3 Dec 2017, 14:25 last edited by
    #2

    You need to connect each Button to a Slot, as Example. The Slot opens a new Window, if you mean that with Interface.

    connect(button, SIGNAL(clicked()), this, SLOT(mySlot()));
    

    Is that what you mean?

    K 1 Reply Last reply 3 Dec 2017, 14:56
    2
    • F Fuel
      3 Dec 2017, 14:25

      You need to connect each Button to a Slot, as Example. The Slot opens a new Window, if you mean that with Interface.

      connect(button, SIGNAL(clicked()), this, SLOT(mySlot()));
      

      Is that what you mean?

      K Offline
      K Offline
      Kushan
      wrote on 3 Dec 2017, 14:56 last edited by
      #3

      @Fuel I want a way to distinguish the buttons that I created dynamically from one another! Since they are created dynamically I cant right click the buttons and select goto slot option like normal ordinary buttons that we add statically

      M 1 Reply Last reply 3 Dec 2017, 15:35
      0
      • K Kushan
        3 Dec 2017, 14:56

        @Fuel I want a way to distinguish the buttons that I created dynamically from one another! Since they are created dynamically I cant right click the buttons and select goto slot option like normal ordinary buttons that we add statically

        M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 3 Dec 2017, 15:35 last edited by
        #4

        @Kushan
        The connect statement @Fuel shows is how you would
        make the same as right clicking in Designer.
        ( sort of, as Designer makes a special name and its found at runtime and hooked up)

        So you hook up each button to different slots and hence different things will happen when you click it.

        If you have many , it might be awkward creating that many slots.

        You can also use
        sender() inside a slot to know whom sent it. But the answer is just some Buttons so
        in what way do you need to distinguish the buttons ?

        K 1 Reply Last reply 3 Dec 2017, 15:55
        2
        • M mrjj
          3 Dec 2017, 15:35

          @Kushan
          The connect statement @Fuel shows is how you would
          make the same as right clicking in Designer.
          ( sort of, as Designer makes a special name and its found at runtime and hooked up)

          So you hook up each button to different slots and hence different things will happen when you click it.

          If you have many , it might be awkward creating that many slots.

          You can also use
          sender() inside a slot to know whom sent it. But the answer is just some Buttons so
          in what way do you need to distinguish the buttons ?

          K Offline
          K Offline
          Kushan
          wrote on 3 Dec 2017, 15:55 last edited by
          #5

          @mrjj Thanx where should I place

          connect(button, SIGNAL(clicked()), this, SLOT(mySlot()));

          this code?

          M 1 Reply Last reply 3 Dec 2017, 16:31
          0
          • K Kushan
            3 Dec 2017, 15:55

            @mrjj Thanx where should I place

            connect(button, SIGNAL(clicked()), this, SLOT(mySlot()));

            this code?

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 3 Dec 2017, 16:31 last edited by mrjj 12 Mar 2017, 16:32
            #6

            @Kushan

            Just after
            a= new QPushButton();
            connect(a, SIGNAL(clicked()), this, SLOT(mySlot()));

            Note that you should create a slot in Dialog .h
            called
            void mySlot();
            and in Dialog.cpp

            void mySlot() {
            }

            Just like when you rightclick,
            Designer adds such function to the class.h and in .cpp

            K 1 Reply Last reply 3 Dec 2017, 16:51
            1
            • M mrjj
              3 Dec 2017, 16:31

              @Kushan

              Just after
              a= new QPushButton();
              connect(a, SIGNAL(clicked()), this, SLOT(mySlot()));

              Note that you should create a slot in Dialog .h
              called
              void mySlot();
              and in Dialog.cpp

              void mySlot() {
              }

              Just like when you rightclick,
              Designer adds such function to the class.h and in .cpp

              K Offline
              K Offline
              Kushan
              wrote on 3 Dec 2017, 16:51 last edited by
              #7

              @mrjj Thanx! But the problem I face is if 2 buttons that are dynamically created and I want one to perform a specific function when it is clicked and the other button to perform another action when its clicked! how can I achieve it? Here "a"is cmmon to both in such a scenario

              M 1 Reply Last reply 3 Dec 2017, 21:22
              0
              • K Kushan
                3 Dec 2017, 16:51

                @mrjj Thanx! But the problem I face is if 2 buttons that are dynamically created and I want one to perform a specific function when it is clicked and the other button to perform another action when its clicked! how can I achieve it? Here "a"is cmmon to both in such a scenario

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 3 Dec 2017, 21:22 last edited by
                #8

                @Kushan
                Hi
                you could simply make another slot
                and connect to that instead.
                connect(a, SIGNAL(clicked()), this, SLOT(mySlot2()));

                But how do you know what the button you are creating should do ?
                its it part of the data you read in ?

                K 1 Reply Last reply 4 Dec 2017, 02:03
                1
                • M mrjj
                  3 Dec 2017, 21:22

                  @Kushan
                  Hi
                  you could simply make another slot
                  and connect to that instead.
                  connect(a, SIGNAL(clicked()), this, SLOT(mySlot2()));

                  But how do you know what the button you are creating should do ?
                  its it part of the data you read in ?

                  K Offline
                  K Offline
                  Kushan
                  wrote on 4 Dec 2017, 02:03 last edited by Kushan 12 Apr 2017, 02:03
                  #9

                  @mrjj The buttons are displaying the names of elements in the qstringlist! Each element has a method name! so when I click a button a method resembling that method name shouldget executed!

                  eg-List<<"Run"<< "Stop"

                  so 2 buttons are created displaying Run and Stop! so when I click the button displaying with the word "Run" the Run() method should execute and when I click the button displaying the word "Stop" the Stop() method should execute.

                  J 1 Reply Last reply 4 Dec 2017, 07:48
                  0
                  • K Kushan
                    4 Dec 2017, 02:03

                    @mrjj The buttons are displaying the names of elements in the qstringlist! Each element has a method name! so when I click a button a method resembling that method name shouldget executed!

                    eg-List<<"Run"<< "Stop"

                    so 2 buttons are created displaying Run and Stop! so when I click the button displaying with the word "Run" the Run() method should execute and when I click the button displaying the word "Stop" the Stop() method should execute.

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 4 Dec 2017, 07:48 last edited by
                    #10

                    @Kushan You should use http://doc.qt.io/qt-5/qsignalmapper.html

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    K 1 Reply Last reply 4 Dec 2017, 08:51
                    1
                    • J jsulm
                      4 Dec 2017, 07:48

                      @Kushan You should use http://doc.qt.io/qt-5/qsignalmapper.html

                      K Offline
                      K Offline
                      Kushan
                      wrote on 4 Dec 2017, 08:51 last edited by
                      #11

                      @jsulm thanx bro :)

                      1 Reply Last reply
                      0

                      11/11

                      4 Dec 2017, 08:51

                      • Login

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