Qt - 从QMainWindow向GLWidget发送按键事件

时间:2013-09-28 11:12:07

标签: qt events opengl keyboard-events qaction

我有一个Qt / OpenGL / OpenCL应用程序,其中包含一个包含GLWidget子类窗口小部件的QMainWindow。我想从QMainWindow的菜单中在GLWidget上添加缩放功能(按键)。

最初,我直接从GLWidget放大/缩小如下(GLWidget.cpp):

void GLWidget::keyPressEvent(QKeyEvent* event) 
{
  switch(event->key()) {
    case Qt::Key_W:
      zoomScale(zoomFactor_in);
      zoomGlobal = 1.0f / zoomFactor_in;
      break;
    case Qt::Key_X:
      zoomScale(zoomFactor_out);
      zoomGlobal = 1.0f / zoomFactor_out;
      break;        
    default:
      event->ignore();
      break;
  }
}

所以,为了从QMainWindow菜单的动作中添加这个功能,我创建了signal / slot(MainWindow.cpp):

void Ui_MainWindow::createSignals()
{ 
...
  connect(actionZoom_In, SIGNAL(triggered()), this, SLOT(zoomIn()));     
  connect(actionZoom_Out, SIGNAL(triggered()), this, SLOT(zoomOut()));  

}

使用以下插槽(我尝试这种方式将按键信号发送到GLwidget“widget_2”):

void Ui_MainWindow::zoomIn()
{
  QKeyEvent event1 = QKeyEvent(QEvent::KeyPress, Qt::Key_W, Qt::NoModifier);
  QApplication::sendEvent(widget_2, &event1);  
}

void Ui_MainWindow::zoomOut()
{
  QKeyEvent event2 = QKeyEvent(QEvent::KeyPress, Qt::Key_X, Qt::NoModifier);
  QApplication::sendEvent(widget_2, &event2);
}

widget_2是我发送KeyPress“X”或“W”的GLWidget_2。

不幸的是,这不起作用。当我按'X'或'W'时,我有一个段错误,我不明白为什么?

这是回溯:

(gdb) bt
#0  0x00007ffff6c70e80 in ?? () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#1  0x00007ffff6c7127f in QMainWindow::event(QEvent*) () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#2  0x00007ffff685d70c in QApplicationPrivate::notify_helper(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#3  0x00007ffff6862704 in QApplication::notify(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#4  0x00007ffff6328b5e in QCoreApplication::notifyInternal(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQtCore.so.4
#5  0x00007ffff688f8cc in ?? () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#6  0x00007ffff6863869 in QApplication::notify(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#7  0x00007ffff6328b5e in QCoreApplication::notifyInternal(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQtCore.so.4
#8  0x000000000047af4e in Ui_MainWindow::zoomIn() ()
#9  0x00007ffff633e54f in QMetaObject::activate(QObject*, QMetaObject const*, int, void**) () from /usr/lib/x86_64-linux-gnu/libQtCore.so.4
#10 0x00007ffff6857502 in QAction::triggered(bool) () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#11 0x00007ffff68576f0 in QAction::activate(QAction::ActionEvent) () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#12 0x00007ffff6857857 in QAction::event(QEvent*) () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#13 0x00007ffff685d70c in QApplicationPrivate::notify_helper(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#14 0x00007ffff6861b8a in QApplication::notify(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#15 0x00007ffff6328b5e in QCoreApplication::notifyInternal(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQtCore.so.4
#16 0x00007ffff688e474 in ?? () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#17 0x00007ffff688f983 in ?? () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#18 0x00007ffff6863869 in QApplication::notify(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#19 0x00007ffff6328b5e in QCoreApplication::notifyInternal(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQtCore.so.4
#20 0x000000000047af4e in Ui_MainWindow::zoomIn() ()
#21 0x00007ffff633e54f in QMetaObject::activate(QObject*, QMetaObject const*, int, void**) () from /usr/lib/x86_64-linux-gnu/libQtCore.so.4
#22 0x00007ffff6857502 in QAction::triggered(bool) () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#23 0x00007ffff68576f0 in QAction::activate(QAction::ActionEvent) () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#24 0x00007ffff6857857 in QAction::event(QEvent*) () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#25 0x00007ffff685d70c in QApplicationPrivate::notify_helper(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#26 0x00007ffff6861b8a in QApplication::notify(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#27 0x00007ffff6328b5e in QCoreApplication::notifyInternal(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQtCore.so.4
#28 0x00007ffff688e474 in ?? () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4

...

当我从MainWindow菜单中单击actionZoom_In / Out时出现同样的问题,只是在第二次单击时发生了segfault。

任何人都可以看到什么问题?

2 个答案:

答案 0 :(得分:0)

不要自己发送关键事件;让系统完成它的工作。

创建两个插槽:一个用于放大,一个用于输出。通过主窗口的连接直接从keyPressEvent调用它们。

答案 1 :(得分:0)

始终检查事件(任何指针 tbh)是否为 nullptr。甚至 event->ignore() 也会导致 nullptr 出现段错误。 (是的,它确实发生了。Qt 中的函数有时会被 nullptr 事件调用)