[solved] Something about listener...



  • Dear Qters...
    Hi,... I need many suggestion and help. A friend who built an application using Java suggest me a method, I am using Qt. He suggest me about listeners, like this
    " You need to register your code as a listener so that when a laser event happens, it tells your listener to handle it "
    What is listener in Qt? or may be there is another opinion...
    Thank you in advance,
    romeo-


  • Moderators

    Listener is a Java concept.



  • Still do not understand...Can we make it in Qt?


  • Moderators

    Sure, you can do almost everything with Qt. The question is - what do you want to do? In your post you have said "when a laser event happens" - I assume this is some device you have connected to your machine? Or is it a laser pointer and you detect it with a camera? There are just too many possibilities to answer your question without more info.



  • What you are refering to is the Observer pattern. Google for it, or better yet, get yourself a copy of the book Design Patterns, Elements of Reusable Object-Oriented Software by E. Gamma et.al. (a.k.a. the Gang of Four or 'GoF').

    Can you do that with C++ and Qt? Absolutely. Do you want to? Depends... If you are using Qt throughout your application, I'd probably stick with signals & slots instead.



  • [quote author="sierdzio" date="1341381788"]Listener is a Java concept.[/quote]

    Listener (AKA Observer) is not a java concept, it's a design pattern which is build in in Java :-)

    "See here on wikipedia":http://en.wikipedia.org/wiki/Observer_pattern

    The description in "GOF4":http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612/ref=sr_1_1?ie=UTF8&qid=1341383858&sr=8-1&keywords=design+patterns (Design pattern by Erich Gamma and friends) is also very good



  • Sorry for my lack of information, actually laser is a spot light from laser pointer. I got the x,y coordinate of this spotlight. I want to make some event for replacing a mouse action, such as: laser ON, laser OFF, laser enter an area, laser leaving an area, etc. Are there any suggestion for those from a scratch in Qt, so I hope I can make some application after that. Thanks in advance.



  • I would use signal/slot as Andre suggested. It's the typical way to do such things in Qt and it is in fact something like the obeserver :-)


  • Moderators

    [quote author="Gerolf" date="1341383882"][quote author="sierdzio" date="1341381788"]Listener is a Java concept.[/quote]

    Listener (AKA Observer) is not a java concept, it's a design pattern which is build in in Java :-)

    "See here on wikipedia":http://en.wikipedia.org/wiki/Observer_pattern

    The description in "GOF4":http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612/ref=sr_1_1?ie=UTF8&qid=1341383858&sr=8-1&keywords=design+patterns (Design pattern by Erich Gamma and friends) is also very good[/quote]

    Well, true. Bad choice of words on my part, made mostly to keep it short. Sorry! :)



  • bq. I want to make some event for replacing a mouse action, such as: laser ON, laser OFF, laser enter an area, laser leaving an area, etc. Are there any suggestion for those from a scratch in Qt, so I hope I can make some application after that. Thanks in advance.

    From what you're saying ("make some event for replacing a mouse action") maybe (just maybe) it's better to handle it with events ("QEvent":http://qt-project.org/doc/qt-4.8/QEvent.html).



  • [quote author="David_Gil" date="1341389798"]bq. I want to make some event for replacing a mouse action, such as: laser ON, laser OFF, laser enter an area, laser leaving an area, etc. Are there any suggestion for those from a scratch in Qt, so I hope I can make some application after that. Thanks in advance.

    From what you're saying ("make some event for replacing a mouse action") maybe (just maybe) it's better to handle it with events ("QEvent":http://qt-project.org/doc/qt-4.8/QEvent.html).
    [/quote]

    But events are no listener / observer pattern. And events are typically not for inter object communication with loose coupling. For events, both must know the event.

    The event can be the solution for synchronizing things to the event loop, but for the observer, it would be signal/slot for me.



  • Thank you for every suggestion, I found in QEvent type manual:

    QEvent::Enter --- Mouse enters widget's boundaries.
    QEvent::DragEnter --- The cursor enters a widget during a drag and drop operation (QDragEnterEvent).
    QEvent::DragLeave --- The cursor leaves a widget during a drag and drop operation (QDragLeaveEvent).

    May be

    • after the observer (signal-slot) recognize the area where the spotlight is, I can use QEvent like above, and

    • using signal-slot to trigger the timer event to count how long a spotlight stay in one location for recognizing the click action,

    • the spotlight can replace the mouse cursor using 'setpos' operation.

    I need some suggestion about that, many thanks...



  • If you want it work like a mouse, you might try sending self-created mouse events to the widget...



  • Thanks Mr Andre, from your idea and I combine it with the chapter 6 of the Foundations of Qt Development by J.Thelin from Apress book, now I have the description of my program structure. Many thanks.



  • I am sorry, but I am not familiar with that book, and I don't have time to go searching for it and read the chapter you reference.


  • Moderators

    Andre, he is not asking you to read it, so don't worry too much :)



  • You're right, actually. It was a statement, not a question.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.