程序目前是这么设计的:
按住按钮,一直不断的做一件事
松开按钮时,做一次另一件事
正常情况下,没有问题,在QPushButton的发射的信号pressed和released对应的槽中各自处理事务。现在的问题是,当程序内部因为其他原因弹出一个对话框时,按钮恢复弹起状态,但是似乎不发送released信号,所以就出问题了
请教,如何解决这一问题?需要派生QPushButton并处理eventfilter吗?是的话,具体如何做?谢谢
解决办法:
正常情况release signal会收到,您可以在qapplication 中添加 eventfilter 来看一下事件。
参考代码如下
Installs an event filter filterObj on this object. For example:
monitoredObj->installEventFilter(filterObj);
An event filter is an object that receives all events that are sent to this object. The filter can either stop the event or forward it to this object. The event filter filterObj receives events via its eventFilter() function. The eventFilter() function must return true if the event should be filtered, (i.e. stopped); otherwise it must return false.
If multiple event filters are installed on a single object, the filter that was installed last is activated first.
Here's a KeyPressEater class that eats the key presses of its monitored objects:
class KeyPressEater : public QObject
{
Q_OBJECT
...
protected:
bool eventFilter(QObject *obj, QEvent *event);
};
bool KeyPressEater::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
qDebug("Ate key press %d", keyEvent->key());
return true;
} else {
// standard event processing
return QObject::eventFilter(obj, event);
}
}