Results 1 to 9 of 9

Thread: multiple slots with one signal

  1. #1
    Join Date
    Oct 2010
    Posts
    58
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default multiple slots with one signal

    Hi,

    I have a signal that is connected to three slots, I was wondering if I could make it so that if slot 1 is wrong (like the user put in the wrong date) the other two would not be called.

    Is it possible to have a slot emit a signal that could then be connected to another slot? For example if my slot 1 works it will emit a signal, but if it fails the signal will not be emitted?

    What I am trying to do is have a user enter a file path and start and end dates and then plot them. I want to check if the dates are valid and also if the file path is valid before I call the plotting function.

    Any ideas? Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: multiple slots with one signal

    Quote Originally Posted by kja View Post
    Is it possible to have a slot emit a signal that could then be connected to another slot? For example if my slot 1 works it will emit a signal, but if it fails the signal will not be emitted?
    Yes, that's the correct way.

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: multiple slots with one signal

    Sure, you can have a slot that emits another signal that is connected to another slot.

    Or you can just call that other slot directly from your first one.

  4. #4
    Join Date
    Aug 2009
    Posts
    140
    Thanks
    22
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: multiple slots with one signal

    This should be pretty easy. Slots are executed in the order they are connected. So one way to do this would be to create a flag e.g. 'input_is_ok' which is accessible from all the slots, starts out ==true, gets set to false on bad input, and causes your subsequent plotter slot to abort ('if (!input_ok) return;'). If all your slots are methods of a single object, this would just be a boolean member. A much more complicated way to do it would be with event filters and custom events, but I can't see any reason to do this except perhaps aesthetics (in this way you would block the events from ever reaching the slots at all).

    Matt

  5. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: multiple slots with one signal

    Quote Originally Posted by MattPhillips View Post
    This should be pretty easy. Slots are executed in the order they are connected. So one way to do this would be to create a flag e.g. 'input_is_ok' which is accessible from all the slots, starts out ==true, gets set to false on bad input, and causes your subsequent plotter slot to abort ('if (!input_ok) return;'). If all your slots are methods of a single object, this would just be a boolean member.
    In my personal opinion that would complicate the code. Well, at least for me it would be more difficult to read the code and the flow of the code. While technically ok of course.

  6. #6
    Join Date
    Oct 2010
    Posts
    58
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: multiple slots with one signal

    So I'm trying to have my slot emit a signal but I am getting the error " C2065 emit undeclared identifier." I think I have all the includes I need. I'm probably just doing something stupid, here is my code:

    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. Q_OBJECT
    4. .....
    5.  
    6. Q_SIGNALS:
    7. void validFile();
    8.  
    9. public Q_SLOTS:
    10. void enterFile();
    11. void getDates();
    12. void slotButtonLoads();
    13. };
    14.  
    15. MyWidget::MyWidget(QWidget * parent):QWidget(parent){
    16. ...
    17. push = new QPushButton("Plot", this);
    18. connect(push, SIGNAL(clicked()), this, SLOT(enterFile()));
    19. connect(push, SIGNAL(validFile()),this, SLOT(getDates())); ///also I don't know what my sender should be for this new signal, 'push' is probably not right
    20. }
    21.  
    22. void MyWidget::enterFile()
    23. {
    24. QString textEntered = lineedit->text();
    25. FileName = qstrdup( textEntered.toLatin1() );
    26. if (fopen (FileName, "rb") == NULL){
    27. emit validFile();
    28. }else{
    29. //something } //// a third question.. how can I allow the user to try again with the filename. I don't think I should
    30. ////// push->disconnect() because it will break my connection
    31. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for all the help

  7. #7
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: multiple slots with one signal

    emit() is declared in an include file, so you do not have all the appropriate include files, though it is only syntactic sugar.

  8. #8
    Join Date
    Oct 2010
    Posts
    58
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: multiple slots with one signal

    What include could it be, I included <QtGui> just to make sure but I still get the error.

    Also does anyone know what the sender would be for my signal when I connect it?

    Qt Code:
    1. connect(push, SIGNAL(validFile()),this, SLOT(getDates())); ///'push' is probably not right
    To copy to clipboard, switch view to plain text mode 

    would I have to create a new qobject to be the sender?

    Thanks

  9. #9
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: multiple slots with one signal

    It's defined in qobjectdefs.h, but you shouldn't normally include that file directly. Most files (such as qobject.h) will include it for you. Or you can just "#define emit" which just exactly the same thing.

    Yes, if you want to use signals and slots, the class should derive from QObject and have the Q_OBJECT macro.

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

    kja (23rd November 2010)

Similar Threads

  1. Signal/Slots, Where can i use it?
    By Tio in forum Newbie
    Replies: 2
    Last Post: 25th May 2010, 01:36
  2. Signal and Slots
    By waynew in forum Newbie
    Replies: 3
    Last Post: 20th November 2009, 03:50
  3. create multiple buttons with signal and slots
    By mohanakrishnan in forum Qt Programming
    Replies: 8
    Last Post: 14th November 2009, 12:03
  4. signal and slots
    By vermarajeev in forum Qt Programming
    Replies: 4
    Last Post: 16th October 2007, 08:31
  5. Signal and slots
    By villy in forum Qt Programming
    Replies: 1
    Last Post: 12th January 2007, 10:10

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.