Results 1 to 10 of 10

Thread: Signal defined in "a.h" can not emit in "b.cpp"

  1. #1
    Join Date
    May 2007
    Posts
    91
    Thanks
    60
    Qt products
    Qt4
    Platforms
    Windows

    Default Signal defined in "a.h" can not emit in "b.cpp"

    I defined a siganl in "a.h" like this:
    signals:
    void Sgl(int);

    when I want to emit it in "b.cpp"
    A a;
    emit a.Sgl(int);

    error C2248: 'A::Sgl' : cannot access protected member declared in class 'A'

    this is a very common problem if u want to emit signal in class while it is defined in another class. Any one can help me ?

  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: Signal defined in "a.h" can not emit in "b.cpp"

    this is a very common problem if u want to emit signal in class while it is defined in another class. Any one can help me ?
    What do you mean?
    could you post your code?
    ==========================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
    May 2007
    Posts
    91
    Thanks
    60
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signal defined in "a.h" can not emit in "b.cpp"

    Quote Originally Posted by high_flyer View Post
    What do you mean?
    could you post your code?
    "a.h":
    signals:
    void Sgl(int);

    "b.cpp"
    A a;
    emit a.Sgl(int);

    Is this clear ?

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

    Default Re: Signal defined in "a.h" can not emit in "b.cpp"

    Create a public method in A that will emit the signal.

    Qt Code:
    1. class A : public QObject {
    2. Q_OBJECT
    3. public:
    4. A(QObject *parent=0) : QObject(parent){}
    5. void emitSig(){ emit sig(); }
    6. signals:
    7. void sig();
    8. };
    To copy to clipboard, switch view to plain text mode 

    BTW. You should avoid emitting signals on account of other objects behind their back. If you do that, it's likely that your design is incorrect.

  5. #5
    Join Date
    May 2007
    Posts
    91
    Thanks
    60
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signal defined in "a.h" can not emit in "b.cpp"

    can i do it like this?
    A a;
    B b;
    connect (&a, Sgl_a(),&b, Sgl_b());

    I tried, but still:
    error C3861: 'connect': identifier not found

    I am totally confused

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

    Default Re: Signal defined in "a.h" can not emit in "b.cpp"

    What is Sgl_a() ? Remember about SIGNAL() and SLOT() macros.

  7. #7
    Join Date
    May 2007
    Posts
    91
    Thanks
    60
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signal defined in "a.h" can not emit in "b.cpp"

    Quote Originally Posted by wysota View Post
    What is Sgl_a() ? Remember about SIGNAL() and SLOT() macros.
    of course i won't make that mistake
    that is just an example

  8. #8
    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: Signal defined in "a.h" can not emit in "b.cpp"

    can i do it like this?
    A a;
    B b;
    connect (&a, Sgl_a(),&b, Sgl_b());

    I tried, but still:
    error C3861: 'connect': identifier not found
    of course i won't make that mistake
    that is just an example
    We can't know what you are thinking.
    And we don't know which mistakes you might make or not - and judging from your post, you are not a very experienced Qt programmer, so you might be doing all kinds of mistakes you are not aware of (and neither are we, if we don't see the code).
    And when dealing with a problem, anything can be a cause, so if you want us to be able to help you, should should deliver as accurate information as you can.
    Why don't you just post the code?
    (And I don't mean just a signal definition line - but your header and the implementation files that are in question).

    Oh - and when posting code, please, use code tags.
    ==========================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.

  9. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Signal defined in "a.h" can not emit in "b.cpp"

    Quote Originally Posted by Shawn View Post
    error C3861: 'connect': identifier not found
    QObject::connect() is a static method of QObject. Sounds like you try to connect outside a QObject, so try:
    Qt Code:
    1. QObject::connect(...);
    To copy to clipboard, switch view to plain text mode 
    QObject header should already be included at least indirectly as both A and B are QObjects.
    J-P Nurmi

  10. #10
    freeskydiver Guest

    Default Re: Signal defined in "a.h" can not emit in "b.cpp"

    Quote Originally Posted by Shawn View Post
    I defined a siganl in "a.h" like this:
    signals:
    void Sgl(int);

    when I want to emit it in "b.cpp"
    A a;
    emit a.Sgl(int);

    error C2248: 'A::Sgl' : cannot access protected member declared in class 'A'

    this is a very common problem if u want to emit signal in class while it is defined in another class. Any one can help me ?
    When the signal is private then give your class a nice public function. Which emit your signal.

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.