PDA

View Full Version : MousePressEvent in a child widget



PatC
7th June 2011, 01:27
Hello everybody,

I created a child widget in a QMainWindow. In this widget, I overloaded the mousePressEvent but that not work.

Here is the code:

widget.h

class myWidget : public QWidget {
Q_OBJECT
private:

protected:
void paintEvent(QPaintEvent *event);
void mousePressEvent(QMouseEvent *event);

public:
BoardWidget(QWidget *parent);
~BoardWidget();
};

widget cpp:

void myWidget::
mousePressEvent(QMouseEvent *event)
{
qDebug()<<"pass";
}

The widget is created in the constructor of my MainWindow:

widget = new myWidget(this);
widget->setGeometry(...);
widget->show();

I am using qt 4.7.3 on a mac.

Thank you,

Pat

DanH
7th June 2011, 02:41
What doesn't work?

Santosh Reddy
7th June 2011, 04:03
As I see, the most obvious reason, why it is not working is you'r not clicking on myWidget. Think again. May be your widget is very small to be under a mouse, or may be your widget is not visible at all. Try making your widget larger / visible enough so that you can click.



widget = new myWidget(this);
widget->setGeometry(...);
widget->show();


not sure why your are using setGeomerty(), instead use size hinting. (this also may be a reason for why your are seeing)

If there are any other widgets on mainwindow? they can overlap with your mywidget, you are just creating the mywidget with mainwindow are parent, not adding it to layout, or not setting it as central widget, hence there is a possibility that your mouse events are consumed by a widget which is over your mywidget, this is applicable if you have some other widgets on main window (as this is not very clear from your code)

PatC
7th June 2011, 04:43
I am clicking on myWidget. I removed from the sample code the paintEvent function in which a shape is drawn. Thus, I am sure that I am clicking on the widget.

In order to resolve the problem, I also have overloaded the mousePressEvent in the main window and the function is called when I click in the widget (a call to myWidget->underMouse() shows that I clicked in the widget).

I only have 1 widget.

I have another program in which I use the same approach. It worked well until I upgraded to 4.7.3 but now I have the same problem (after recompilation). I know that the previous version was 4.x.

Thanks

Santosh Reddy
7th June 2011, 05:25
In order to resolve the problem, I also have overloaded the mousePressEvent in the main window and the function is called when I click in the widget (a call to myWidget->underMouse() shows that I clicked in the widget).
main window mousePressEvent() is called in 2 cases
1. When you click on part of main window which is not occupied by the mywidget
2. When you click on mywidget, and mywidget ignores the mouse click event

which do you think is happening in your case

PatC
7th June 2011, 14:24
It's clear that the event is ignored by the child widget.

I tried to create the widget without parent (widget = new myWidget(0)) and in this case, the event is captured correctly.

Santosh Reddy
7th June 2011, 22:55
How did you set the widget onto the QMainWindow?

Do it this way, it should work.

MainWindow::MainWindow()
{
...
widget = new myWidget(this);
widget->setGeometry(...);
widget->show();// Not required
setCentralWidget(widget);
...
}

PatC
7th June 2011, 23:10
Using setCentralWidget works. But what happen if I want to add another widget (such as list box) in the main window?

EDIT:

I removed the line widget->show() and now it works correctly (without the setCentralWidget). I would like to understand this behavior...

Thank you very much...

Santosh Reddy
7th June 2011, 23:33
QMainWindow can have only one central widget(fixed widget), and multiple docking widgets (movable widgets)

If you want to have multiple widgets in central area, then you need to put all the widgets in another widget (say top widget), and then set this top widget as central widget.

If you want widgets to be movable, then consider using QDockWidget instead of (or along with) central widget.

Your problem:
You just created the widget with mainwindow as parent, but was did not add to the main window layout formally, (either setCentralWidget() or addDockWidget()), hence may it may not have propagated the mouse events properly.

Any ways the correct way to place a widget on main window is either setCentralWidget() or addDockWidget() (I should have posted this statement in my first reply)

Also note that QMainWindow, has special way to handle the layouts, unlike other widgets, Refer QMainWindow Documenations for the exact layout