Results 1 to 9 of 9

Thread: Signal Slot Question

  1. #1
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Signal Slot Question

    Hi,
    This must be a basic question but here it goes.
    Qt Code:
    1. #include "txtedit.h"
    2. #include "ui_txtedit.h"
    3.  
    4. txtEdit::txtEdit(QWidget *parent) :
    5. QGroupBox(parent),
    6. m_ui(new Ui::txtEdit)
    7. {
    8. m_ui->setupUi(this);
    9.  
    10. listStringList = new QStringList;
    11. listModel = new QStringListModel(this);
    12. listModel->setStringList(*listStringList);
    13. m_ui->listView->setModel(listModel);
    14. ...
    15. connect(m_ui->sortAscendingButton, SIGNAL(clicked(bool)), this, SLOT(sortStringList(bool)));
    16. connect(m_ui->sortDescendingButton, SIGNAL(clicked(bool)), this, SLOT(sortStringList(bool)));
    17. }
    18.  
    19. ...
    20.  
    21.  
    22. void txtEdit::sortStringList(bool order)
    23. {
    24. if(order)
    25. listModel->sort(0, Qt::AscendingOrder);
    26. else
    27. listModel->sort(0, Qt::DescendingOrder);
    28. }
    To copy to clipboard, switch view to plain text mode 

    I want the buttons in lines 15 e 16 to send a true or false value to the sortStringList slot.
    How do i manage this?
    Thansks

  2. #2
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signal Slot Question

    Read "Advanced Signals and Slots Usage": http://doc.trolltech.com/4.5/signals...nd-slots-usage

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

    graciano (17th August 2009)

  4. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Signal Slot Question

    In your special case a QButtonGroup with setExclusive(true) would do also a great job.

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

    graciano (17th August 2009)

  6. #4
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Signal Slot Question

    I wasn't able to do it with the QButtonGroup so i did this:
    Qt Code:
    1. sortListSignalMapper = new QSignalMapper(this);
    2. sortListSignalMapper->setMapping(m_ui->sortAscendingButton, QString("Ascending"));
    3. sortListSignalMapper->setMapping(m_ui->sortDescendingButton, QString("Descending"));
    4. connect(m_ui->sortAscendingButton, SIGNAL(clicked()), sortListSignalMapper, SLOT(map()));
    5. connect(m_ui->sortDescendingButton, SIGNAL(clicked()), sortListSignalMapper, SLOT(map()));
    6. connect(sortListSignalMapper, SIGNAL(mapped(QString)), this, SLOT(sortStringList(QString)));
    To copy to clipboard, switch view to plain text mode 

    Looks like i can not use bool in this situation.
    Thanks

  7. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Signal Slot Question

    Ok,

    with the mapper you can use the "int option". 1 = true and 0 = false.


    Qt Code:
    1. //not tested but should work
    2. sortListSignalMapper = new QSignalMapper(this);
    3. sortListSignalMapper->setMapping(m_ui->sortAscendingButton, 1);
    4. sortListSignalMapper->setMapping(m_ui->sortDescendingButton, 0);
    5. connect(m_ui->sortAscendingButton, SIGNAL(clicked()), sortListSignalMapper, SLOT(map()));
    6. connect(m_ui->sortDescendingButton, SIGNAL(clicked()), sortListSignalMapper, SLOT(map()));
    7. connect(sortListSignalMapper, SIGNAL(mapped(int)), this, SLOT(sortStringList(bool)));
    To copy to clipboard, switch view to plain text mode 

    And with the button group you could simply use:
    Qt Code:
    1. // use the buttonClicked(QAbstractButton *button) signal to connect to your slot
    2. void txtEdit::sortStringList(QAbstractButton *button)
    3. {
    4. if(button == m_ui->sortAscendingButton)
    5. listModel->sort(0, Qt::AscendingOrder);
    6. else
    7. listModel->sort(0, Qt::DescendingOrder);
    8. }
    9. // or if you need a bool function beside
    10. void txtEdit::sortStringList(QAbstractButton *button)
    11. {
    12. return sortStringList(button == m_ui->sortAscendingButton);
    13. }
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to Lykurg for this useful post:

    graciano (18th August 2009)

  9. #6
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Signal Slot Question

    Hi,
    I remember trying your first option.
    I geted a ...
    Qt Code:
    1. QObject::connect: Incompatible sender/receiver arguments
    2. QSignalMapper::mapped(int) --> txtEdit::sortStringList(bool)
    To copy to clipboard, switch view to plain text mode 
    But now you gave me an idea...
    Qt Code:
    1. sortListSignalMapper = new QSignalMapper(this);
    2. sortListSignalMapper->setMapping(m_ui->sortAscendingButton, 1);
    3. sortListSignalMapper->setMapping(m_ui->sortDescendingButton, 0);
    4. connect(m_ui->sortAscendingButton, SIGNAL(clicked()), sortListSignalMapper, SLOT(map()));
    5. connect(m_ui->sortDescendingButton, SIGNAL(clicked()), sortListSignalMapper, SLOT(map()));
    6. connect(sortListSignalMapper, SIGNAL(mapped(int)), this, SLOT(sortStringList(int)));
    To copy to clipboard, switch view to plain text mode 
    ...and it worked fine.
    This way order is an integer and it's value is evaluated as an "bool" in the condition.
    Thanks

  10. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Signal Slot Question

    if you want that your code is readable (and understandable) later, you could also use Qt::AscendingOrder and Qt:escendingOrder:

    Qt Code:
    1. sortListSignalMapper = new QSignalMapper(this);
    2. sortListSignalMapper->setMapping(m_ui->sortAscendingButton, Qt::AscendingOrder);
    3. sortListSignalMapper->setMapping(m_ui->sortDescendingButton, Qt::DescendingOrder);
    4. connect(m_ui->sortAscendingButton, SIGNAL(clicked()), sortListSignalMapper, SLOT(map()));
    5. connect(m_ui->sortDescendingButton, SIGNAL(clicked()), sortListSignalMapper, SLOT(map()));
    6. connect(sortListSignalMapper, SIGNAL(mapped(int)), this, SLOT(sortStringList(int)));
    7. //...
    8. void xxx::sortStringList(int order)
    9. {
    10. switch(order)
    11. {
    12. case Qt::DescendingOrder:
    13. //...
    14. break;
    15. case Qt::AscendingOrder:
    16. default:
    17. //...
    18. break;
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    EDIT:
    QObject::connect: Incompatible sender/receiver arguments
    QSignalMapper::mapped(int) --> txtEdit::sortStringList(bool)
    Ok, thought Qt would cast internally.

  11. The following user says thank you to Lykurg for this useful post:

    graciano (18th August 2009)

  12. #8
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: Signal Slot Question

    You can also connect both to a slot and evaluate sender() in that slot to see which button was clicked and therefore which method to execute.
    It's nice to be important but it's more important to be nice.

  13. The following user says thank you to axeljaeger for this useful post:

    graciano (19th August 2009)

  14. #9
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Signal Slot Question

    Looks like there is a warning about this subject( http://doc.trolltech.com/4.5/qobject.html#sender )
    Warning: This function violates the object-oriented principle of modularity. However, getting access to the sender might be useful when many signals are connected to a single slot.
    I will try ti stick to the "principle of modularity" in order not to get lost
    Thanks for the tip.

Similar Threads

  1. pthread instead QThread
    By brevleq in forum Qt Programming
    Replies: 8
    Last Post: 23rd December 2008, 07:16
  2. Connection of custon signals/slots
    By brevleq in forum Qt Programming
    Replies: 2
    Last Post: 23rd December 2008, 07:04
  3. Replies: 12
    Last Post: 18th September 2008, 15:04
  4. Signal & Slot editor
    By Ishark in forum Qt Tools
    Replies: 4
    Last Post: 28th May 2008, 15:20
  5. Another signal and slot question
    By fnmblot in forum Newbie
    Replies: 5
    Last Post: 4th March 2008, 19:50

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.