此外,您还应按如下所述调用QWidget :: keyPressEvent的基本实现这里.是否有另一个具有焦点但未传递KeyEvent的Widget?I am writing on QT. OpenGL is connected, work properly, but the events and keyPressEvent mousePressEvent not respond to keystrokes.class MainWindow : public QGLWidget{ Q_OBJECT protected: void initializeGL(); // Метод для инициализирования opengl void resizeGL(int nWidth, int nHeight); // Метод вызываемый после каждого изменения размера окна void paintGL(); // Метод для вывода изображения на экран void paintSun(); void mousePressEvent(QMouseEvent *mouse); void keyPressEvent(QKeyEvent *ke);public: MainWindow(int x, int y,QWidget *parent = 0); ~MainWindow();};void MainWindow::keyPressEvent(QKeyEvent *ke){ switch (ke->key()) { case Qt::Key_Space://NO REACTION View += 1; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); paintPlanet(); break; } updateGL();} 解决方案 Make sure that the MainWindow accepts Focus by settingQWidget::setFocusPolicy()Also you should call the base implementation of QWidget::keyPressEvent as stated here. Is there another Widget which has the focus and does not pass the KeyEvent? 这篇关于keyPressEvent没有反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-05 10:38