Results 1 to 5 of 5

Thread: Using enum as parameter to slot functions

  1. #1
    Join Date
    Nov 2013
    Location
    Kungsbacka, Sweden
    Posts
    13
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Using enum as parameter to slot functions

    I've run into some trouble with signals and slots. I've successfully used them in my application so far but when I wanted to call a slot function with an enum things seemed to get a lot harder. After reading around the net trying to understand what difference an enum parameter make I came up with the following code I thought was gonna work.

    I created a new header file just for my enum:
    Arrows.h
    Qt Code:
    1. #ifndef ARROWS_H
    2. #define ARROWS_H
    3.  
    4. #include <QtCore>
    5.  
    6. enum Arrow
    7. {
    8. NO_ARROW,
    9. DIAG_DOWN_RIGHT,
    10. DIAG_DOWN_LEFT,
    11. DIAG_UP_RIGHT
    12. };
    13.  
    14. Q_DECLARE_METATYPE(Arrow)
    15.  
    16. #endif // ARROWS_H
    To copy to clipboard, switch view to plain text mode 

    The header file which declares the slot function looks like this:
    CrosswordArea.h
    Qt Code:
    1. #ifndef CROSSWORDAREA_H
    2. #define CROSSWORDAREA_H
    3.  
    4. #include "Arrows.h"
    5. (...)
    6.  
    7. class CrosswordArea : public QWidget
    8. {
    9. Q_OBJECT
    10. Q_ENUMS(Arrow)
    11.  
    12. (...)
    13.  
    14. public slots:
    15. (...)
    16. void setArrow(Arrow a);
    17.  
    18. (...)
    19. };
    20.  
    21. #endif // CROSSWORDAREA_H
    To copy to clipboard, switch view to plain text mode 

    Slot implementation (I'm not sure if I need qRegisterMetaType here)
    CrosswordArea.cpp
    Qt Code:
    1. #include "Arrows.h"
    2. #include "CrosswordArea.h"
    3. (...)
    4.  
    5. CrosswordArea::CrosswordArea(QString str)
    6. {
    7. qRegisterMetaType<Arrow>("Arrow");
    8. (...)
    9. }
    10.  
    11. (...)
    12.  
    13. void CrosswordArea::setArrow(Arrow a)
    14. {
    15. marked->setArrow(a);
    16. }
    To copy to clipboard, switch view to plain text mode 

    The slot is connected to a couple of buttons.
    ButtonArea.cpp
    Qt Code:
    1. #include "Arrows.h"
    2. #include "CrosswordArea.h"
    3. (...)
    4.  
    5. ButtonArea::ButtonArea(CrosswordArea *cwArea)
    6. {
    7. qRegisterMetaType<Arrow>("Arrow");
    8. (...)
    9.  
    10. connect(letter2clue, SIGNAL(clicked()), cwArea, SLOT(setClue())); //works
    11. connect(clue2letter, SIGNAL(clicked()), cwArea, SLOT(setLetter())); //works
    12. connect(merge, SIGNAL(clicked()), cwArea, SLOT(merge())); //works
    13. connect(yellowMark, SIGNAL(clicked()), cwArea, SLOT(yellowMark())); //works
    14. connect(diagDRArrow, SIGNAL(clicked()), cwArea, SLOT(setArrow(Arrow))); //doesn't work
    15. connect(diagDLArrow, SIGNAL(clicked()), cwArea, SLOT(setArrow(Arrow))); //doesn't work
    16. connect(diagURArrow, SIGNAL(clicked()), cwArea, SLOT(setArrow(Arrow))); //doesn't work
    17.  
    18. (...)
    19. }
    To copy to clipboard, switch view to plain text mode 

    Running this doesn't cause an error but rather a warning.

    Qt Code:
    1. QObject::connect: Incompatible sender/receiver arguments
    2. QPushButton::clicked() --> CrosswordArea::setArrow(Arrow)
    3. QObject::connect: Incompatible sender/receiver arguments
    4. QPushButton::clicked() --> CrosswordArea::setArrow(Arrow)
    5. QObject::connect: Incompatible sender/receiver arguments
    6. QPushButton::clicked() --> CrosswordArea::setArrow(Arrow)
    To copy to clipboard, switch view to plain text mode 

    I'm doing something terribly wrong. Before I start to guess solutions, messing up my code in hope to get lucky I thought I'd ask you guys first.
    What have I done wrong and how can I make my slot function call correct?

    Thanks!

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

    Default Re: Using enum as parameter to slot functions

    You are trying to connect signal without parameter to slot with parameter. This is impossible. Parameters of signal and slot must be the same type. I think that QSignalMapper is what you need.

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

    Ponnytail (10th November 2013)

  4. #3
    Join Date
    Nov 2013
    Location
    Kungsbacka, Sweden
    Posts
    13
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Using enum as parameter to slot functions

    Thanks Lesiok, a QSignalMapper worked great. I didn't know the parameters of the signal had to be the same as the parameters for the slot. Thanks for the clarification!

    My button connection now looks like this.
    ButtonArea.cpp
    Qt Code:
    1. #include "Arrows.h"
    2. #include "CrosswordArea.h"
    3.  
    4. (...)
    5.  
    6. ButtonArea::ButtonArea(CrosswordArea *cwArea)
    7. {
    8. qRegisterMetaType<Arrow>("Arrow");
    9. sigMapper = new QSignalMapper(this);
    10.  
    11. (...)
    12.  
    13. connect(diagDRArrow, SIGNAL(clicked()), sigMapper, SLOT(map()));
    14. connect(diagDLArrow, SIGNAL(clicked()), sigMapper, SLOT(map()));
    15. connect(diagURArrow, SIGNAL(clicked()), sigMapper, SLOT(map()));
    16.  
    17. sigMapper->setMapping(diagDRArrow, "DIAG_DOWN_RIGHT");
    18. sigMapper->setMapping(diagDLArrow, "DIAG_DOWN_LEFT");
    19. sigMapper->setMapping(diagURArrow, "DIAG_UP_RIGHT");
    20.  
    21. connect(sigMapper, SIGNAL(mapped(QString)), cwArea, SLOT(setArrow(const QString &)));
    22.  
    23. (...)
    24. }
    To copy to clipboard, switch view to plain text mode 

    It feels a bit weird to use enums now though because in my CrosswordArea.cpp I have to map strings to their corresponding enum.
    CrosswordArea.cpp
    Qt Code:
    1. (...)
    2.  
    3. CrosswordArea::CrosswordArea(QString str)
    4. {
    5. qRegisterMetaType<Arrow>("Arrow");
    6. arrowMap = new QMap<QString, Arrow>;
    7. arrowMap->insert("DIAG_DOWN_RIGHT", DIAG_DOWN_RIGHT);
    8. arrowMap->insert("DIAG_DOWN_LEFT", DIAG_DOWN_LEFT);
    9. arrowMap->insert("DIAG_UP_RIGHT", DIAG_UP_RIGHT);
    10.  
    11. (...)
    12. }
    13.  
    14. void CrosswordArea::setArrow(const QString &str)
    15. {
    16. marked->setArrow(arrowMap->take(str));
    17. }
    To copy to clipboard, switch view to plain text mode 

    This works perfectly fine.

    Problem SOLVED!

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using enum as parameter to slot functions

    You could also use setMapping(QObject*, int) using the enum values for the int argument and connect to the mapped(int) signal

    Cheers,
    _

  6. #5
    Join Date
    Nov 2013
    Location
    Kungsbacka, Sweden
    Posts
    13
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Using enum as parameter to slot functions

    Oh of course! That saves a lot of code. Thanks anda_skoa!

Similar Threads

  1. How to declare SLOT as a parameter to member function?
    By QPlace in forum Qt Programming
    Replies: 2
    Last Post: 17th July 2018, 00:41
  2. Slot - Signal with parameter
    By Lodhart in forum Newbie
    Replies: 8
    Last Post: 10th April 2013, 10:08
  3. Replies: 3
    Last Post: 26th August 2011, 07:17
  4. Problem with connect() (enum-type as parameter)
    By alu23 in forum Qt Programming
    Replies: 3
    Last Post: 9th May 2008, 07:46
  5. How to add a parameter to a slot
    By Potch in forum Newbie
    Replies: 4
    Last Post: 5th May 2008, 23:09

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.