Results 1 to 4 of 4

Thread: setting up connections for dynamically created widgets

  1. #1
    Join Date
    May 2011
    Posts
    27
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Question setting up connections for dynamically created widgets

    Hi everyone,
    I have a set of Pushbuttons which are created dynamically in a for() loop.
    The code looks like this:

    Qt Code:
    1. for(int i = 0; i < 32; i++)
    2. {
    3. btn = new MyButton(QString("%1").arg(i+1), i);
    4. //Layout stuff which works...
    5.  
    6. connect(btn, SIGNAL(clicked()), this, SLOT(setLevel()));
    7. }
    To copy to clipboard, switch view to plain text mode 

    The MyButton class just extends the PushButtons class. The first argument is the title, the second is an index variable (just an int). What i want to do is, that when one of the 32 buttons are pressed, the setLevel() functions is being called. I want to use the index to set the level. For example when the user presses button 21, the setLevel() function sets the level to 21.

    It should be something like this, which is not working, i know...
    Qt Code:
    1. connect(btn, SIGNAL(clicked()), this, SLOT(setLevel(btn.index)));
    To copy to clipboard, switch view to plain text mode 

    How can i do this?

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: setting up connections for dynamically created widgets

    Slots need to have the same arguments as signals, so passing it as an argument doesn't work.

    But QObjects have function sender() which returns a pointer to the sending object. Just cast it to your class and check which one it is.

  3. #3
    Join Date
    May 2011
    Posts
    27
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: setting up connections for dynamically created widgets

    I thought more of writing an own signal that sends the index into the slot, but i do not know how. I'll take a look at the sender() function, maybe i can solve it with this.

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: setting up connections for dynamically created widgets

    Just read about QSignalMapper. This is solution for You.

  5. The following user says thank you to Lesiok for this useful post:

    seux (13th August 2011)

Similar Threads

  1. Widgets must be created in the GUI thread.
    By Thành Viên Mới in forum Qt Programming
    Replies: 10
    Last Post: 8th November 2011, 16:53
  2. Dynamically created buttons.
    By Tomasz in forum Newbie
    Replies: 26
    Last Post: 2nd December 2010, 09:40
  3. PaintEvent() in a dynamically created and destroyed objet not working.
    By savaliya_ambani in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 20th October 2010, 08:49
  4. QMainWindow ignores dynamically created, floating QDockWidget
    By stefanadelbert in forum Qt Programming
    Replies: 1
    Last Post: 2nd March 2010, 01:06
  5. Replies: 2
    Last Post: 12th February 2009, 22:23

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.