0

我有一个 Qt/OpenGL/OpenCL 应用程序,其 QMainWindow 包含一个 GLWidget 子类小部件。我想从 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 菜单的操作中添加此功能,我创建了信号/插槽 (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 时出现同样的问题,除了在第二次单击时发生段错误。

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

4

2 回答 2

1

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

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

于 2013-09-28T12:09:02.840 回答
0

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

于 2021-06-15T07:21:56.480 回答