MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); wgOpenGL = new glTest(ui->ViewOpenGl); qDebug() << "ViewOpenGl have focus - " << ui->ViewOpenGl->hasFocus(); ui->ViewOpenGl->setFocusPolicy(Qt::StrongFocus); ui->ViewOpenGl->setFocus(Qt::MouseFocusReason); qDebug() << "ViewOpenGl have focus - " << ui->ViewOpenGl->hasFocus();}void MainWindow::keyPressEvent( QKeyEvent *event ){ qDebug() << "************************************************************"; qDebug() << "ViewOpenGl have focus - " << ui->ViewOpenGl->hasFocus(); qDebug() << "centralWidget have focus - " << ui->centralWidget->hasFocus(); qDebug() << "Exit have focus - " << ui->Exit->hasFocus(); qDebug() << "mainToolBar have focus - " << ui->mainToolBar->hasFocus(); qDebug() << "menuBar have focus - " << ui->menuBar->hasFocus(); qDebug() << "statusBar have focus - " << ui->statusBar->hasFocus(); qDebug() << "************************************************************"; wgOpenGL->keyPressEvent(event);}
ViewOpenGl have focus - false ViewOpenGl have focus - false ************************************************************ ViewOpenGl have focus - true centralWidget have focus - false Exit have focus - false mainToolBar have focus - false menuBar have focus - false statusBar have focus - false ************************************************************ Key Up pressed!
qDebug() << "Widget have focus - " << qApp->focusWidget()->objectName();
void MainWindow::init(){ QObject::connect(qApp, SIGNAL(focusChanged()), this, SLOT(findFW()));}void MainWindow::findFW(){ qDebug() << "Widget have focus - " << qApp->focusWidget()->objectName();}class MainWindow : public QMainWindow{ Q_OBJECTpublic: MainWindow(QWidget *parent = 0); ~MainWindow(); void init();public slots: void findFW();protected: //void keyPressEvent( QKeyEvent *event );private: glTest *wgOpenGL; Ui::MainWindow *ui;};
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); wgOpenGL = new glTest(ui->ViewOpenGl); qDebug() << "ViewOpenGl have focus - " << ui->ViewOpenGl->hasFocus(); ui->ViewOpenGl->setFocusPolicy(Qt::TabFocus); ui->ViewOpenGl->setFocus(Qt::TabFocusReason); qDebug() << "ViewOpenGl have focus - " << ui->ViewOpenGl->hasFocus();}void MainWindow::mousePressEvent(QMouseEvent *event){ qDebug() << "*****************************************************"; qDebug() << "ViewOpenGl have focus - " << ui->ViewOpenGl->hasFocus(); qDebug() << "Widget have focus - " << qApp->focusWidget()->objectName(); qDebug() << "*****************************************************";}
ViewOpenGl have focus - false ViewOpenGl have focus - false ***************************************************** ViewOpenGl have focus - true Widget have focus - "ViewOpenGl" *****************************************************