void MainWindow::keyPressEvent(QKeyEvent* event){ switch(event->key()) { case Qt::Key_Up: QMessageBox msgBox; msgBox.setWindowTitle(trUtf8("Внимание!")); msgBox.setText(trUtf8("Уведомление:")); msgBox.setInformativeText(trUtf8("Нажата кнопка ВВЕРХ.")); msgBox.setIcon(QMessageBox::Information); msgBox.exec(); break; }}
C++ (Qt)class MainWindow : public QMainWindow{ Q_OBJECT public: MainWindow(); protected: void keyPressEvent( QKeyEvent *event );}; MainWindow::MainWindow() : QMainWindow( 0 ){ QWidget *widget = new QWidget( this ); setCentralWidget( widget );} void MainWindow::keyPressEvent( QKeyEvent *event ){ switch( event->key() ) { case Qt::Key_Up: qDebug() << "Key Up pressed"; break; default: qDebug() << "Key pressed:" << event->key(); break; }}
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); wgOpenGL = new glTest(ui->ViewOpenGl); ui->ViewOpenGl->setFocusPolicy(Qt::StrongFocus); ui->ViewOpenGl->setFocus(Qt::ActiveWindowFocusReason);}
void MainWindow::keyPressEvent( QKeyEvent *event ){ wgOpenGL->keyPressEvent(event);}
void glTest::keyPressEvent(QKeyEvent* event){ switch(event->key()) { case Qt::Key_Up: qDebug() << "Key Up pressed!"; setXRotation(xRot + 5); qDebug() << trUtf8("Объект повернут на 10 градусов по оси Х"); break; case Qt::Key_Down: qDebug() << "Key Down pressed!"; setXRotation(xRot - 5); break; case Qt::Key_Left: qDebug() << "Key Left pressed!"; setYRotation(yRot + 5); break; case Qt::Key_Right: qDebug() << "Key Right pressed!"; setYRotation(yRot - 5); break; } updateGL();}