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. [Solved]Access to QPushButton who are created in a loop
QtWS25 Last Chance

[Solved]Access to QPushButton who are created in a loop

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 4.5k Views
  • 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
    starFire
    wrote on last edited by
    #1

    Hello
    When I create a QPushBotton in the constructor of my window in a for loop and want to show a label if there is clicked, how can I do this?
    I mean it like this:
    @
    for (int i {0}; i != 10; ++i)
    {
    QPushButton *button = new QPushButton;
    QLabel *label = new QLabel;
    label->hide();
    // when button is clicked i want to show the label
    }
    @

    This is a very simplified version of my code.
    In my code I have a vector with a class type that includes things like name and so on.
    And I create in the loop Layouts that I add to a Scroll Area Widget.
    thanks

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Try adding this statement.
      connect(button,SIGNAL(clicked(),label,slot(show()));

      Please not with above example, same pointer variable is continously used.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • S Offline
        S Offline
        starFire
        wrote on last edited by
        #3

        thanks for your response.
        When I make it so the widgets show when the button is clicked.
        But now I want that the widgets hide when I click again.

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          Please look at how the show and hide works. You need to put your own logic make this kind of behaviour you are asking.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MuldeR
            wrote on last edited by
            #5

            [quote author="starFire" date="1406400950"]thanks for your response.
            When I make it so the widgets show when the button is clicked.
            But now I want that the widgets hide when I click again.[/quote]

            I think you'll need a custom slot for that:

            @//This is a helper class
            class ButtonUserData : public QObjectUserData
            {
            public:
            ButtonUserData(QLabel *label)
            {
            m_label = label;
            }

            QLabel *getLabel(void)
            {
            return m_label;
            }

            private:
            QLabel *m_label;
            };@

            @//Setup function
            MyClass::someFunction(void)
            {
            for (int i = 0; i < 10; ++i)
            {
            QPushButton *button = new QPushButton(this);
            QLabel *label = new QLabel(this);
            button->setUserData(42, new ButtonUserData(button));
            connect(button, SIGNAL(clicked()), this SLOT(buttonWasClicked()));
            label->hide();
            }
            }

            //Slot function
            MyClass::buttonWasClicked(void)
            {
            //Get specific button that was clicked:
            QPushButton button = dynamic_cast<QPushButton>(QObject::sender());
            if(button)
            {
            //Get the corresponding QLabel:
            ButtonUserData data = dynamic_cast<ButtonUserData>(button->userData(42));
            if(data)
            {
            //Toggle label visibility:
            QLabel *label= data->getLabel();
            label->setVisible(!(label->isVisible()));
            }
            }
            }@

            My OpenSource software at: http://muldersoft.com/

            Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

            Go visit the coop: http://youtu.be/Jay...

            1 Reply Last reply
            0
            • S Offline
              S Offline
              starFire
              wrote on last edited by
              #6

              Yes you are right, what I need was a inherated class and my own SLOT.
              Thank you !

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

                Or you could just use a lambda:
                @
                for (int i {0}; i != 10; ++i)
                {
                //you should give a parent to these widgets or they'll leak
                QPushButton *button = new QPushButton;
                QLabel *label = new QLabel;
                label->hide();

                connect(button, &QPushButton::clicked, ={
                label->setVisible(!label->isVisible());
                });
                }
                @

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  MuldeR
                  wrote on last edited by
                  #8

                  [quote author="Chris Kawa" date="1406481750"]Or you could just use a lambda:
                  @
                  for (int i {0}; i != 10; ++i)
                  {
                  //you should give a parent to these widgets or they'll leak
                  QPushButton *button = new QPushButton;
                  QLabel *label = new QLabel;
                  label->hide();

                  connect(button, &QPushButton::clicked, ={
                  label->setVisible(!label->isVisible());
                  });
                  }
                  @[/quote]

                  Nifty!

                  My OpenSource software at: http://muldersoft.com/

                  Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                  Go visit the coop: http://youtu.be/Jay...

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    starFire
                    wrote on last edited by
                    #9

                    [quote author="Chris Kawa" date="1406481750"]Or you could just use a lambda:
                    @
                    for (int i {0}; i != 10; ++i)
                    {
                    //you should give a parent to these widgets or they'll leak
                    QPushButton *button = new QPushButton;
                    QLabel *label = new QLabel;
                    label->hide();

                    connect(button, &QPushButton::clicked, ={
                    label->setVisible(!label->isVisible());
                    });
                    }
                    @

                    [/quote]

                    Can I use a lambda by using this form, too?

                    @
                    connect(PushButton, SIGNAL(The signal), label, SLOT(The Slot))
                    @
                    When I make it like this the compiler says "No such slot".
                    EDIT:
                    Ok I have to do it with the new Syntax.

                    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