Results 1 to 20 of 22

Thread: Struggling with signal which has a default parameter.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: Struggling with signal which has a default parameter.

    Ok, I've made the slot public and rebuilt. Still I get a null everytime.

    Quote Originally Posted by high_flyer View Post
    From a brief look I see you are emitting layoutChanged() from your slot, and that one is with a default value, which would explain why in the debugger you see first the one with a non NULL object, and then one with NULL.
    You have a recursion there.
    I did mention it but probably not clear enough, the signal and slot are on different classes. Thus the layoutChanged() I emit in the slot is not the same, it is layoutChanged() on QAbstractItemModel.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Struggling with signal which has a default parameter.

    the signal and slot are on different classes.
    And the classes are also not related (through derivation) (just making sure)
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: Struggling with signal which has a default parameter.

    Yes they are not related. I've checked that it only breaks once in that function as well.

    As I said in my first post, it looks like the wrong signal is emitted by the meta object system.

    Qt Code:
    1. int Qtilities::Core::Observer::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
    2. {
    3. _id = QObject::qt_metacall(_c, _id, _a);
    4. if (_id < 0)
    5. return _id;
    6. if (_c == QMetaObject::InvokeMetaMethod) {
    7. switch (_id) {
    8. case 0: modificationStateChanged((*reinterpret_cast< bool(*)>(_a[1]))); break;
    9. case 1: monitoredPropertyChanged((*reinterpret_cast< const char*(*)>(_a[1])),(*reinterpret_cast< QList<QObject*>(*)>(_a[2]))); break;
    10. case 2: monitoredPropertyChanged((*reinterpret_cast< const char*(*)>(_a[1]))); break;
    11. case 3: propertyChangeFiltered((*reinterpret_cast< const char*(*)>(_a[1])),(*reinterpret_cast< QList<QObject*>(*)>(_a[2]))); break;
    12. case 4: propertyChangeFiltered((*reinterpret_cast< const char*(*)>(_a[1]))); break;
    13. case 5: numberOfSubjectsChanged((*reinterpret_cast< Observer::SubjectChangeIndication(*)>(_a[1])),(*reinterpret_cast< QList<QObject*>(*)>(_a[2]))); break;
    14. case 6: numberOfSubjectsChanged((*reinterpret_cast< Observer::SubjectChangeIndication(*)>(_a[1]))); break;
    15. case 7: layoutChanged((*reinterpret_cast< QObject*(*)>(_a[1]))); break;
    16. case 8: layoutChanged(); break;
    17. default: ;
    18. }
    To copy to clipboard, switch view to plain text mode 

    As I said, the debugger steps into case 8 in the above code so I'm pretty sure the problem is with the delivery of the signal, rather than the receiving side. At least it looks like that.

    One thing I don't understand is the following:
    The class emitting the signal is called Observer. The instance on which the signal is emitted is called TreeNode which inherits Observer. Stepping through the debugger the signal emission goes into the qt_metacall function of TreeNode and then basically calls qt_metacall on the Observer base class. This is the piece of code I'm referring to:

    Qt Code:
    1. int Qtilities::CoreGui::TreeNode::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
    2. {
    3. _id = Observer::qt_metacall(_c, _id, _a);
    4. if (_id < 0)
    5. return _id;
    To copy to clipboard, switch view to plain text mode 

    The above funciton is entered with _id = 12, however as soon as it steps into the base class observer the base class gets _id = 8 as the parameter which is the signal with the default argument. Maybe I don't know the moc system that well and its correct but it does not make sense to me.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Struggling with signal which has a default parameter.

    The class emitting the signal is called Observer. The instance on which the signal is emitted is called TreeNode which inherits Observer.
    I don't understand this - the instance should BE Objserver instance, not inherit it - which would mean its an instance of a class that inherits Observer - can you clarify this - since if this is confused, it can well be the root of the problem!

    Lets try to do this in order:
    What object (which class is it, what is its instance (variable/obejct) name?
    What object declares and implements the slot?

    based on your original post:
    Qt Code:
    1. connect(d_observer,SIGNAL(layoutChanged(QObject*)),SLOT(rebuildTreeStructure(QObject*)));
    To copy to clipboard, switch view to plain text mode 
    The sender class and instance is also the slot class and instance, and in the slot you are emitting the same signal again, even though you said its another class...
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Struggling with signal which has a default parameter.

    Change the name of the signal and the slot to something not clashing with existing signals in Qt. You can also try declaring two separate slots - one with the QObject param and the other without it and see if it changes anything. It could be that moc has a bug somewhere that makes it work incorrectly with slots that have default params.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: Struggling with signal which has a default parameter.

    Ok no problem let me explain in more detail:

    Qt Code:
    1. // This is a simplified view of the observer class which contains the signal. The signal is also emitted in one of this class's functions.
    2. class Observer : public QObject
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. Observer();
    8. virtual ~Observer();
    9.  
    10. void someFunctionEmittingTheSignal() {
    11. QObject* new_obj = new QObject();
    12. emit layoutChanged(new_obj);
    13. }
    14.  
    15. signals:
    16. void layoutChanged(QObject* added_object = 0);
    17.  
    18. }
    19.  
    20. // This is the class of which the instance is created:
    21. class TreeNode: public Observer
    22. {
    23. Q_OBJECT
    24.  
    25. public:
    26. TreeNode();
    27. virtual ~TreeNode();
    28. }
    To copy to clipboard, switch view to plain text mode 

    I have another class with the slot (again simplified):
    Qt Code:
    1. // This is the class of which the instance is created:
    2. class ObserverTreeModel: public QAbstractItemModel
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. ObserverTreeModel();
    8. virtual ~ObserverTreeModel();
    9. setObserverContext(Observer* obs) {
    10. d_observer = obs;
    11. connect(d_observer,SIGNAL(layoutChanged(QObject*)),SLOT(rebuildTreeStructure(QObject*)));
    12. }
    13.  
    14. private slots:
    15. void rebuildTreeStructure(Object* obj = 0) {
    16. // Do some stuff that is going to change the layout of the tree.
    17. emit layoutAboutToChange(); // Emits QAbstractItemModel::layoutAboutToChange();
    18. emit layoutChanged(); // Emits QAbstractItemModel::layoutChanged();
    19. }
    20.  
    21. private:
    22. Observer* d_observer;
    23. }
    To copy to clipboard, switch view to plain text mode 

    I do something like this then:
    Qt Code:
    1. ObserverTreeModel model;
    2. TreeNode tree_node;
    3. model.setObserverContext(&tree_node);
    4. tree_node.someFunctionEmittingTheSignal();
    To copy to clipboard, switch view to plain text mode 

    I hope that explains it better.

    Quote Originally Posted by wysota View Post
    You can also try declaring two separate slots - one with the QObject param and the other without it and see if it changes anything. It could be that moc has a bug somewhere that makes it work incorrectly with slots that have default params.
    I've removed the default parameter on the slots side, but it did not make a difference.
    Last edited by JPNaude; 20th January 2011 at 12:07.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Struggling with signal which has a default parameter.

    So the only explanation I can see is that your parameter has been deleted somewhere in the meantime. By the way, is line #15 of your last snippet a typo or a real code?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: Struggling with signal which has a default parameter.

    Quote Originally Posted by wysota View Post
    So the only explanation I can see is that your parameter has been deleted somewhere in the meantime. By the way, is line #15 of your last snippet a typo or a real code?
    I'm very sure that its not deleted because I can access it in the tree view directly after this function. Also if it was a QPointer and it was deleted it would make sense but if it was deleted in my case I should get an invalid pointer.

    Yes that was a typo, thanks.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Struggling with signal which has a default parameter.

    The problem is what you said in post #13 - QMetaObject is stepping into a wrong case. It should go into 7 and not 8. Can you show us all places where you emit this signal? In post #6 there is an emit layoutChanged() but it seems this is a different "layoutChanged()".
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: Struggling with signal which has a default parameter.

    I've updated my example in post #16 to show what the other layoutChanged() signal is. It will make more sense now, they should be independent. I can try to change the signal names as you said to see if it makes any difference.

  11. #11
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: Struggling with signal which has a default parameter.

    I figured it out...

    Was my mistake. The observer class I mentioned is able to observe other observers creating a tree structure. The problem was that I emitted the signal in an observer low down in the tree and as the signal propagated through to the top observer (connected to the view) the parameter got lost since those connections were made without the parameters. I forgot that this is happening and thought I emitted the signal on the top level observer. My bad.

    Thanks for your help and time again.
    Cheers,
    Jaco

Similar Threads

  1. Slot - Signal with parameter
    By Lodhart in forum Newbie
    Replies: 8
    Last Post: 10th April 2013, 10:08
  2. Replies: 2
    Last Post: 6th July 2009, 12:53
  3. Replies: 3
    Last Post: 3rd May 2009, 14:15
  4. Struggling with QThread...
    By TemporalBeing in forum Qt Programming
    Replies: 2
    Last Post: 23rd March 2009, 20:46
  5. Struggling with value space
    By UnicycleBloke in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 28th May 2008, 23:26

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.