[Qt-interest] how to create custom signal and slot?

Sean Harmer sean.harmer at maps-technology.com
Wed Sep 23 15:06:00 CEST 2009


On Wednesday 23 Sep 2009 13:57:26 Riccardo Roasio wrote:
> mmmmmmmm....
>
> I'm a little confused....
>
> I have a class called RenderArea that extend QWidget and a class
> called Rectangle that extend QObject.
> An object of type REctangle is created into the RenderArea object.
> Now....
>
> i have to reimplement mousePressEvent into the Rectangle class? Or in
> the in the Rectangle class?
> For the clicked() signal i think i need to create it into the REctangle
> class.
In that case it needs to go into the RenderArea class as QObject's do not 
receive mousePressEvents, only QWidgets do.

You will need to do some hit testing in the mousePressEvent() to see if your 
click hits the rectangle object. Do you have a single Rectangle or many?

>From the little I know of your problem I would make the following signal in 
RenderArea:

void rectangleClicked( Rectangle* rect );

and emit this from within RenderArea::mousePressEvent() with a pointer to the 
Rectangle that was clicked.

HTH,

Sean

>
> Correct?
>
> Thanks for the Expalnations,
> Riccardo
>
> 2009/9/23 Sean Harmer <sean.harmer at maps-technology.com>:
> > On Wednesday 23 Sep 2009 13:27:37 Riccardo Roasio wrote:
> >> So you mean that in the rectangle class i need to create a slignal
> >> named mousePressEvent and connect it to a slot?
> >
> > No. You need to reimplement virtual void mousePressEvent from QWidget.
> > You also need to declare a new signal called clicked() or whatever you
> > wish to call it. Then in your mousePressEvent() function do whatever
> > checks you need to do to make sure the mouse was actually pressed withing
> > your rectangle (if it only occupies a subset of the widget for example).
> > If you are happy, emit your signal from within mousePressEvent().
> >
> > Then somewhere else in your code connect up your clicked() signal to a
> > slot of your choice to do whetever processing is needed.
> >
> > Cheers,
> >
> > Sean
> > _______________________________________________
> > Qt-interest mailing list
> > Qt-interest at trolltech.com
> > http://lists.trolltech.com/mailman/listinfo/qt-interest
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest




More information about the Qt-interest-old mailing list