Results 1 to 6 of 6

Thread: functions or values in signal-slot connections

  1. #1
    Join Date
    Dec 2009
    Posts
    65
    Thanks
    10
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default functions or values in signal-slot connections

    Hi

    I am trying to create "elegant" way to connect 2 comboboxes.
    If the selected item in first combobox is index = 3 i want to enable second combobox, for other items in first combobox the second combobox should be disabled.
    Of course i could program that manually but... the code is better if there is less code
    The "elegant" way i thought could work is to connect them something like this:

    connect(combo1, SIGNAL(activated(int)), combo2 SLOT(setEnabled(bool)));

    but the bool parameter in SLOT should be function of int from SIGNAL, like (int == 3)
    Is there a way to achieve this

    there is a similar thing on forum but it is for python, i am using c++;
    http://www.qtcentre.org/threads/2382...nctions-(pyqt)

  2. #2
    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: functions or values in signal-slot connections

    Solution is QSignalMapper

  3. #3
    Join Date
    Dec 2009
    Posts
    65
    Thanks
    10
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: functions or values in signal-slot connections

    this is the example from QSignalMapper help:
    Qt Code:
    1. signalMapper = new QSignalMapper(this);
    2.  
    3. QGridLayout *gridLayout = new QGridLayout;
    4. for (int i = 0; i < texts.size(); ++i) {
    5. QPushButton *button = new QPushButton(texts[i]);
    6. connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
    7. signalMapper->setMapping(button, texts[i]);
    8. gridLayout->addWidget(button, i / 3, i % 3);
    9. }
    10.  
    11. connect(signalMapper, SIGNAL(mapped(const QString &)),
    12. this, SIGNAL(clicked(const QString &)));
    To copy to clipboard, switch view to plain text mode 

    I still dont see the solution, i have only one sender - combo1, and only one receiver - combo2.
    The example shows one receiver and multiple senders.
    And after, the last connection should have bool as function parameter, how do i get bool;.

  4. #4
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: functions or values in signal-slot connections

    there would be only two lines of code in the slot connected to the activated() signal.

    if ( index == 3 )
    combo2->setEnabled(true)
    else
    combo2->setEnabled(false);

    i think its not much of a code.

  5. #5
    Join Date
    Dec 2009
    Posts
    65
    Thanks
    10
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: functions or values in signal-slot connections

    there are also slot declaration, and slot definition lines, and the fact that the wrapped definition can be scattered with a lot of code... for this example 5 scattered lines against 1 readable line.
    As i said before, i could do that, and i did it like this (see how i like to compact my code)

    combo2->setEnabled(index == 3);

    but i had a feeling that the it was possible, but not very "famous"
    it would be great to have expressions in connections, like:

    connect(combo1, SIGNAL(activated(int)), combo2 SLOT(setEnabled((bool)(int == 3)));

    the connections are already possible with

    connect(combo1, SIGNAL(activated(int)), combo2 SLOT(setSomethingWithoutArgument()));
    connect(combo1, SIGNAL(activated(int)), combo2 SLOT(setSomething(long)));
    (am i right?)

    Anyway, thanks for your time
    Last edited by davidovv; 20th December 2011 at 21:49.

  6. #6
    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: functions or values in signal-slot connections

    You can't use expressions in slot/signal definition. Just create new class inherited from QComboBox and define in them mySuperSlot(int) and mySuperSignal(bool). Then connect activated(int) to this new slot and in this slot emit mySuperSignal connected to another combo's slot setEnabled(bool).

Similar Threads

  1. duplicate signal/slot connections check
    By positive_4_life in forum Newbie
    Replies: 6
    Last Post: 17th October 2011, 23:15
  2. Passing values to custom 'slot' functions (pyqt)
    By Richie in forum Qt Programming
    Replies: 2
    Last Post: 7th September 2009, 07:05
  3. Queued Signal Slot connections
    By SebastianBecker in forum Qt Programming
    Replies: 2
    Last Post: 24th June 2009, 20:06
  4. Replies: 2
    Last Post: 20th September 2007, 12:27
  5. Signal/slot connections of deleted items
    By Michiel in forum Newbie
    Replies: 2
    Last Post: 24th March 2006, 16:44

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.