qt QWidget click

I have my own class based in QWidget. I put this widget in QMainWindow and I need catch mouse click on this widget.

I tried:

connect(my_widget, SIGNAL(clicked()), this, SLOT(exit(0)));

But nothing is happening. How can I do it?


QWidget does not have a clicked() signal, and QMainWindow does not have an exit() slot. It is impossible to connect to an unexisting signal and unexisting slot. The return value of the connect must be true if the connect is successful. Check this value when you make connections to be sure that your code will work correctly.

To exit your application, you must call qApp->quit()

Also, as it has been mentioned by others, you will have to install an eventFilter or reimplement the

void QWidget::mousePressEvent ( QMouseEvent * event )   [virtual protected]


void QWidget::mouseReleaseEvent ( QMouseEvent * event )   [virtual protected]


There are plenty of examples in the official doc of Qt, this for example reimplements the mousePressEvent(QMouseEvent *event) method.

For the eventFilter option, see this small example.

Hope this helps.

A QWidget has no clicked signal. To make this work, use events. All widgets support events, so there's some manual work to do, but not much:

  1. Override the event function for your widget (which you derive from QWidget
  2. Respond to events of type QEvent:: MouseButtonPress

Alternatively, add a eventFilter method.

Google the classes and methods I mentioned for code samples and to get to a complete solution depending on your exact needs.

Need Your Help

How to call the 'time_diff' filter in Twig

php html html5 symfony twig

How can I call the 'time_diff' function In Twig

Right click to select row in dataGridView

c# select datagridview row right-click

I need to select a row in dataGridView with right click before ContextMenu shown because contextMenu is row-dependendt.