简体   繁体   English

qt QWidget点击

[英]qt QWidget click

I have my own class based in QWidget. 我有自己的基于QWidget的类。 I put this widget in QMainWindow and I need catch mouse click on this widget. 我把这个小部件放在QMainWindow中,我需要在这个小部件上点击鼠标。

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. QWidget没有clicked()信号,QMainWindow没有exit()槽。 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. 如果连接成功,则connect的返回值必须为true。 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() 要退出应用程序,必须调用qApp->quit()

Also, as it has been mentioned by others, you will have to install an eventFilter or reimplement the 此外,正如其他人所提到的,您必须安装eventFilter或重新实现

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. 有很多的例子在Qt的官方文档, 这个例如重新实现了mousePressEvent(QMouseEvent *event)的方法。

For the eventFilter option, see this small example. 有关eventFilter选项,请参阅小示例。

Hope this helps. 希望这可以帮助。

A QWidget has no clicked signal. QWidget没有clicked信号。 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 覆盖窗口小部件的event功能(从QWidget派生
  2. Respond to events of type QEvent:: MouseButtonPress 响应QEvent:: MouseButtonPress类型的事件

Alternatively, add a eventFilter method. 或者,添加eventFilter方法。

Google the classes and methods I mentioned for code samples and to get to a complete solution depending on your exact needs. 谷歌我提到的代码示例的类和方法,并根据您的确切需求获得完整的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM