problem with QMouseEvent in QRect
OS::win_xp_sp3
Qt::4.6
Is it possible to use QMouseEvent inside QRect? I have parent widget and inside is some QLabel with text "status unchanged".
Also , inside parent widget is MyRect which is derived from QRect.
Now I want to MouseEvent act only inside this MyRect. For example , if I act on MyRect , text in parent widget need to be changed.
For example:
class MyRect : public QRect {
public:
MyRect(int x, int y, int w, int h, ParentWidget* parent)
: QRect(x,y,w,h)
{
itsParent = parent;
}
~MyRect() {}
protected:
void mouseMoveEvent(QMouseEvent* event)
private:
ParentWidget* itsParent
};
void MyRect::mouseMoveEvent(QMouseEvent* event)
{
if(event->buttons() == Qt::L开发者_StackOverfloweftButton)
{
itsparent->label->setText("status changed");
}
}
nothing happens
question:: is it possible to use QMouseEvent like this (only on QRect)?
A QRect is neither a QObject nor a QWidget, so it doesn't receive events. It's just four numbers describing a rectangle (and it doesn't make sense to derive from it). You can check if a point is the given rect in the mouse event handler of your widget. Like:
void MyLabel::mouseMoveEvent( QMouseEvent* e ) {
if ( !rect.contains( e->pos() ) )
return;
//... handle mouse move
}
An alternative to subclassing is using an event filter.
is it possible to use QMouseEvent like this (only on QRect)?
No, at least not how you do it. QRect is not a QWidget and therefore doesn't have any mouseEvent
handlers and such. It is just an entity with four coordinates.
You can do what you want by adding a handler to a real QWidget (either by subclassing, or using installEventFilter
) and in the handler check for clicking in your rectangle using QRect::contains(QPoint)
.
精彩评论