![](/img/trans.png)
[英]How to Pass QMainWindow mouse clicked position to QGraphicsView
[英]How to prevent member QWidgets or QDialog objects taking over key events from QMainWindow once the dialog has been clicked by the mouse?
所以我有以下代碼描述的QMainWindow
類型類:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
void closeEvent(QCloseEvent *);\
DimensionDialog *newResolution;
Ui::MainWindow *ui;
ImageInteraction *liveVideo;
ImageInteraction *modifiedVideo;
CameraControl *cameraControl;
QPushButton *pushToTalk;
QPushButton *audioSettingsSetup;
AudioSettings *audioSettings;
QPushButton *numberOfRunningThreads;
protected:
void keyPressEvent(QKeyEvent * event);
void keyReleaseEvent(QKeyEvent * event);
private slots:
void restartVideoWithNewResolution(int, int);
};
從那里您可以看到該類確實處理了一些關鍵事件。
如您所見,該類還具有成員DimensionDialog
和CameraControl
,它們分別是QDialog
和QWigdet
類型類。 現在,這兩個成員也有自己的插槽,當按下某些按鈕時會調用它們。 的問題是,當按下這些按鈕中的一個,相應的類( DimesionDialog
或CameraControl
)接管鍵事件和MainWindow
類不能捕獲任何更多的關鍵事件。
我不明白為什么會這樣。 我該如何預防? 我希望鍵事件僅由MainWindow
處理。
謝謝。
如果要將關鍵事件傳播給父級,則應顯式忽略它。
void DimensionDialog:keyPressEvent(QKeyEvent *event)
{
// ...
// do something then propagate event to parent
event->ignore();
}
這是QEvent的行為。 因此,對於鼠標事件,它也應該起作用。
如果您需要全局檢測整個應用程序中的關鍵事件,則可以在重新實現的QApplication :: notify()中捕獲並處理它們(有關如何使用QCoreApplication :: notify()的更多詳細信息,請參見Qt文檔)。
class KeyEventAwareApp : public QApplication
{
public:
using QApplication::QApplication;
bool notify(QObject* object,QEvent* event)
{
if(event->type() == QEvent::KeyPress)
{
// do any event processing here, for example redirect it to other object
// if swallow event then
return true;
// otherwise send event to the receiver
return object->event(event);
}
// we are interested only in key events, so forward the rest to receivers
return object->event(event);
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.