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:
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.