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



  • 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



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

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



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



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



  • [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()));
    }
    }
    }@



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


  • Moderators

    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 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!



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.