Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 927
0xAX Avatar asked Oct 26 '10 14:10

0xAX


People also ask

What is QWidget in Qt?

Widgets are the primary elements for creating user interfaces in Qt. Widgets can display data and status information, receive user input, and provide a container for other widgets that should be grouped together. A widget that is not embedded in a parent widget is called a window.

What is QWidget * parent?

The tree-like organization of QWidgets (and in fact of any QObjects ) is part of the memory management strategy used within the Qt-Framework. Assigning a QObject to a parent means that ownership of the child object is transferred to the parent object. If the parent is deleted, all its children are also deleted.

Is QWidget a QObject?

All Qt widgets inherit QObject. The convenience function isWidgetType() returns whether an object is actually a widget. It is much faster than qobject_cast<QWidget *>(obj) or obj->inherits("QWidget"). Some QObject functions, e.g. children(), return a QObjectList.

How do I change the size of a widget in Qt?

If the width won't change afterwards, you can use setFixedWidth : widget->setFixedWidth(165); Similarly, to change the height without changing the width, there is setFixedHeight . Never use fixed width!


1 Answers

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] 

or

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

methods.

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.

like image 168
Live Avatar answered Sep 17 '22 06:09

Live