Results 1 to 11 of 11

Thread: Connect with ArrayButton. Need an ID.

  1. #1
    Join Date
    Jan 2007
    Posts
    209
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Connect with ArrayButton. Need an ID.

    Ok,
    My main class:

    QList<QPushButton*> t_buttons;
    t_buttons << new QPushButton("Host");
    // Maybe can somehow use i in AddBox?
    connect(t_buttons[i], SIGNAL(clicked()), this, SLOT(AddBox()));

    void AddBox(){
    // now how do I know which button was clicked???
    }


    I'm having trouble figuring out a way to connect them, emit won't work I dont think, and I don't know a way to override clicked(), or a way to give AddBox the correct ID, and I don't wanna really make like 50 functions lol.
    Any Ideas?

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 541 Times in 521 Posts

    Default Re: Connect with ArrayButton. Need an ID.

    Use QButtonGroup instead of QList. Like this:

    Qt Code:
    1. QButtonGroup btnGroup;
    2.  
    3. btnGroup.addButton( new QPushButton( "Host" ), 0 );
    4. connect( btnGroup, SIGNAL( clicked( int ) ), this, SLOT( AddBox( int ) ) );
    To copy to clipboard, switch view to plain text mode 

    Here I set an ID of 0 for the first button but you can use anything you want. The buttons are added with addButton.

    When a button is clicked, it's id is passed to AddBox.

    Marcel

  3. The following user says thank you to marcel for this useful post:

    VireX (7th April 2007)

  4. #3
    Join Date
    Jan 2007
    Posts
    209
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: Connect with ArrayButton. Need an ID.

    You're aaaammaaazing...

  5. #4
    Join Date
    Jan 2007
    Posts
    209
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: Connect with ArrayButton. Need an ID.

    Wait, there is still a problem.
    t_buttons.addButton(new QPushButton("Box"), i);
    connect(&t_buttons, SIGNAL(buttonClicked(int)), this, SLOT(AddBox(int)));

    But now, whenever someone clicks a button.... It launches AddBox like 50 times. Because I have 50 buttons. BTW it's buttonClicked not clicked().

  6. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 541 Times in 521 Posts

    Default Re: Connect with ArrayButton. Need an ID.

    No, when someone clicks a button, let's say the btn with the id 5, then buttonClicked( 5 ) will be emitted and AddBox( 5 ) will be called.

    Depends on the implementation of your AddBox function. It should be something like this:

    Qt Code:
    1. void Class::AddBox( int btnIndex )
    2. {
    3. switch( btnIndex )
    4. {
    5. ...
    6. case 5:
    7. // Do something...
    8. break;
    9. ...
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    The slot will be called only once for each click.

    Marcel.

  7. #6
    Join Date
    Jan 2007
    Posts
    209
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: Connect with ArrayButton. Need an ID.

    Qt Code:
    1. ....
    2. t_buttons.addButton(new QPushButton("Box"), i);
    3. connect(&t_buttons, SIGNAL(buttonClicked(int)), this, SLOT(AddBox(int)));
    4. }
    5. void CBox::AddBox(int i){
    6. COBox = new COBox(i);
    7. COBox->show();
    8. }
    To copy to clipboard, switch view to plain text mode 
    Here's how I did it. But whenever I click any of the buttons, so let's say I click on button 34, and it will pop up about 55 (the total amount of buttons) new Windows of COBox!!! Like as if it's stuck in a loop somehow. The constructor of COBox changes the Windowtitle to the number i, and seems like they're all the correct one, but one CLICK on ANY button creates 55 new windows... can't figure out why.

  8. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 541 Times in 521 Posts

    Default Re: Connect with ArrayButton. Need an ID.

    Is connect(&t_buttons, SIGNAL(buttonClicked(int)), this, SLOT(AddBox(int))); in a for loop?

    If it is ( most probably is ), then take it out of there! That is why you get so many slot calls for a single emit.

  9. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 541 Times in 521 Posts

    Default Re: Connect with ArrayButton. Need an ID.

    Put connect outside the for loop.
    Connecting a signal to a slot 55 times has the effect you mentioned. The slot will get called 55 times the signal is emitted.

    Regards,
    Marcel.

  10. The following user says thank you to marcel for this useful post:

    VireX (7th April 2007)

  11. #9
    Join Date
    Jan 2007
    Posts
    209
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: Connect with ArrayButton. Need an ID.

    Sorry, I'm just being very newb today... What a dumb mistake of me... thanks. (Forgot I was connecting the group rather than each button separately in a loop).

  12. #10
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 541 Times in 521 Posts

    Default Re: Connect with ArrayButton. Need an ID.

    No problem. Could've happened to anyone...

  13. #11
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 334 Times in 317 Posts

    Default Re: Connect with ArrayButton. Need an ID.

    One more thing... if u have similar kind of functionality for the buttons, u can let the button handle that functionality instead of catching the event and then handling it.

    For example I had a case where I wanted to change colors(by popping color dialog) for some data in list using buttons. Instead of using one slot and taking action based on which button was clicked, I put the color changing functionality in the button itself. Later I would retrieve the color from the button.
    May be if u have similar situation u can use the technique....

    But if u want different buttons perform different actions, above methods are fine...

Similar Threads

  1. Replies: 5
    Last Post: 28th August 2006, 15:36
  2. many connect
    By mickey in forum Qt Programming
    Replies: 3
    Last Post: 29th May 2006, 13:55
  3. [QT4 & XP] connect on QtreeView
    By incapacitant in forum Newbie
    Replies: 1
    Last Post: 2nd March 2006, 12:08
  4. connect
    By mickey in forum Newbie
    Replies: 1
    Last Post: 25th February 2006, 19:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.