主要參考網址: http://rritw.com/a/bianchengyuyan/C__/20120802/197558.html
參考網址: http://openhome.cc/Gossip/Qt4Gossip/EventFilter.html
測試程式 :
--------------------- myDialog.h ---------------------
class myDialog:publicQDialog
{
Q_OBJECT
public:
myDialog(QWidget*parent);
// ...省略...
private:
(自行重新定義按鍵事件的接收, 印出訊息)
void keyPressEvent(QKeyEvent*inEvent);
void keyReleaseEvent(QKeyEvent*inEvent);
// ...省略...
};
--------------------- myDialog.cpp ---------------------
// ...建構子等省略...
void myDialog::keyPressEvent(QKeyEvent*inEvent)
{
qDebug("ipcMsgKeyPressEvent!!!!!!!!!!!!");
}
void myDialog::keyReleaseEvent(QKeyEvent*inEvent)
{
qDebug("ipcMsgKeyReleaseEvent!!!!!!!!!!!!");
}
--------------------- MainWindow.h ---------------------
class MainWindow:public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent=0);
// ...省略...
public slots:
int Show_myDialog();
// ...省略...
private:
bool eventFilter(QObject*,QEvent*);
myDialog *m_Msg;
// ...省略...
}
--------------------- MainWindow.cpp ---------------------
// 建構子等省略
int MainWindow::Show_myDialog()
{
m_Msg=new myDialog(0);
m_Msg->installEventFilter(this);
m_Msg->exec();
delete m_Msg;
m_Msg=NULL;
}
== case 1 : 設定如果接收到事件的是 m_Msg, 則會進入 eventFilter 並且傳遞此事件給m_Msg
bool MainWindow::eventFilter(QObject *obj,QEvent *event)
{
if(obj==m_Msg)
{
qDebug("eventFilterreturn0!!!!!!!!!!!!");
return false;
}
}
執行, 按下按鍵印出的結果為:
eventFilter return 0 !!!!!!!!!!!!
eventFilter return 0 !!!!!!!!!!!!
ipcMsg Key Press Event!!!!!!!!!!!!
eventFilter return 0 !!!!!!!!!!!!
ipcMsg Key Release Event!!!!!!!!!!!!
eventFilter return 0 !!!!!!!!!!!!
== case 2 : 設定如果接收到事件的是 m_Msg, 則會進入 eventFilter 並且會擋住事件不傳遞給m_Msg
bool MainWindow::eventFilter(QObject *obj,QEvent *event)
{
if(obj==m_Msg)
{
qDebug("eventFilterreturn1!!!!!!!!!!!!");
return true;
}
else
{
qDebug("eventFilterreturn0!!!!!!!!!!!!");
return false;
}
}
執行, 按下按鍵印出的結果為:
eventFilter return 1 !!!!!!!!!!!!
eventFilter return 1 !!!!!!!!!!!!
eventFilter return 1 !!!!!!!!!!!!
eventFilter return 1 !!!!!!!!!!!!
且發現除非是m_Msg正在執行中, 才會進入MainWindow的eventFilter函式
否則皆不會印出eventFilter裡面的任何訊息.
文章標籤
全站熱搜
留言列表