You are not logged in.

1

Thursday, December 1st 2011, 9:31pm

QMouseEvent handling, obscurs other events

Hello,

I am trying my way with mouse events. I have declared a class named Renderer:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Renderer : public QWidget
{
	Q_OBJECT

public:
	Renderer(QWidget *parent);
	QSize sizeHint() const;
	QSize minimumSizeHint() const;

protected:
	void paintEvent(QPaintEvent *event);
	void mousePressEvent(QMouseEvent *event);
	void mouseReleaseEvent(QMouseEvent *event);
	bool event(QEvent *);
};


Initially I haven't redefined the event() member function and my class did catch the paintEvent and mousePressEvent.
Then I wanted to implement mouseReleaseEvent so I added event() member function. This fact deactivated the other two events. Why is that?
I mean now my paintEvent memebr function is not called any more? How can I overcome this?

Implementation of this class is given below:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
Renderer::Renderer(QWidget *parent)
	:QWidget(parent)
{
	//connect(this, SIGNAL())
}

bool Renderer::event(QEvent *event)
{
	if(event->type() == QEvent::MouseButtonPress)
	{
    	QMouseEvent* mouse_event = (QMouseEvent*)event;
    	if(mouse_event->button() == Qt::LeftButton)
    	{
        	cout << "\nLeft button pressed" << endl;
        	return true;
    	}
    	else if(mouse_event->button() == Qt::RightButton)
    	{
        	cout << "\nRight button pressed" << endl;
        	return true;
    	}

	}
	if(event->type() == QEvent::MouseButtonRelease)
	{
    	QMouseEvent* mouse_event = (QMouseEvent*)event;
    	if(mouse_event->button() == Qt::LeftButton)
    	{
        	cout << "\nLeft button released" << endl;
        	return true;
    	}
    	else if(mouse_event->button() == Qt::RightButton)
    	{
        	cout << "\nRight button released" << endl;
        	return true;
    	}
    	//mouseReleaseEvent(qobject_cast<QMouseEvent*>(event));
    	mouseReleaseEvent(mouse_event);
    	return true;
	}
	return false;
}

void Renderer::paintEvent(QPaintEvent *event)
{
	//cout << "Now in PaintEvent" << endl;
	int side = qMin(width(),height());
	QPainter paint(this);
	paint.setWindow(-50,-50,100,100);
	//paint.setViewport(-50,-50,100,100);
	paint.setViewport(width()-side,height()-side,side,side);
	QRect win = paint.window();
	//paint.drawRect(win.x()+1,win.y()+1,win.width()-win.x()-2,
             	//  win.height()-win.y()-2);
	//paint.drawLine(0,-50,50,50);
	paint.setBrush(Qt::white);
	paint.drawEllipse(QPoint(0,0),49,49);
	paint.drawLine(QPoint(0,0),QPoint(50,50));
	cout << win.width() << " " << win.height() << endl;

}

void Renderer::mousePressEvent(QMouseEvent *event)
{
	if(event->button() == Qt::LeftButton)
	{
    	QPoint pos = event->pos();
    	cout << "\nInside mousePressEvent (Left button)";
    	cout << "\nx,y" << pos.x() << " " << pos.y() << endl;

	}
	else if(event->button() == Qt::RightButton)
	{
    	cout << "\nInside mousePressEvent (Right button)" << endl;
	}
}

void Renderer::mouseReleaseEvent(QMouseEvent *event)
{
	cout << "\nNow inside Mouserealese" << endl;
}

QSize Renderer::sizeHint() const
{
	return QSize(100,100);
}

QSize Renderer::minimumSizeHint() const
{
	return QSize(100,100);
}


If this look intimidating just the event() and mousePressEvent and paintEvent are important here. Also another issue is how do I convert a QEvent* to a QMouseEvent* (since event in event() is of this type anyway). I used a c-style casting since qobject_cast did not work
error message was:

Quoted

no matching function for call to ‘qobject_cast(QEvent*&)’
I have skipped the include file above. I attach all files (there other 2 files in the project). If you guys have any idea that might help I would be grateful.
eypros has attached the following file:

This post has been edited 1 times, last edit by "eypros" (Dec 1st 2011, 9:37pm)


2

Thursday, December 1st 2011, 9:39pm

OK I think I got something. I added

Source code

1
2
3
4
5
6
	if(event->type() == QEvent:Paint)
	{
    	QPaintEvent* paint_event = (QPaintEvent*)event;
    	paintEvent(paint_event);
    	return true;
	}

inside event() so that it calls the paintEvent function. I figure out that event() catches all events and does not allow the other member function to catch them. Why is that?
And my question about casting between event types remain...

3

Sunday, December 4th 2011, 10:12am

Maybe my latest reply confused possible helpers.
I still have 2 questions remaining:
1) why is implementing Myclass::evect() obscurs for example Myclass::mousePressEvent etc

2) How do I cast a pointer to an event object (event *) to a pointer to a derived class e.g. mouseEvent *?

I hope someone would be able to supply some answers.

4

Monday, December 5th 2011, 12:13pm

to stop your problems, just call


QWidget::event(event);

in you renderer event() method.

QWidget would normally forward correct event types to correct handlers, but since you have overriden the qwidget implementation, you no longer hae that functionality unless you call it explicity like above.


To cast pointers like you want, you should use dynamic_cast<>, or the Qt equivalent.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar threads

Used tags

QEvent, QMouseevent