2

我在 Linux 上使用 Qt 4。

我正在为触摸屏显示器实现应用程序,并且还插入了鼠标和键盘。

我可以实现鼠标或触摸屏,但不能同时实现,因为它们之间的交互很糟糕。主要问题是,当我单击鼠标或按下触摸屏时,会触发以下事件。

(注意:我重新实现了这个功能,以便我可以做我需要的事情)

MainWindow::mousePressEvent(QMouseEvent *ev)
{
    // Handle mouse specific events
}

但是,当我按下触摸屏时,我也会触发相同的功能:(

因此,我无法区分鼠标点击和触摸屏按下!– 我该如何解决这个问题?如何确定是鼠标单击还是触摸屏按下?

我试过查看事件类型,但它总是 = 2(mouseClickEvent,但从来没有 TouchBegin 等)。目前我被困在这里:(

编辑:

我对此进行了更深入的研究并实施了这样的事情:

MainWindow::MainWindow(QWidget *parent)
{
...
    qApp->installEventFilter(this);
    setAttribute(Qt::WA_AcceptTouchEvents);
    setAttribute(Qt::WA_TouchPadAcceptSingleTouchEvents);
}
bool MainWindow::eventFilter(QObject *, QEvent * pEvent)
{
    switch(pEvent->type())
    {
    case QEvent::TouchBegin:
        qDebug("BEGIN");
        break;
    case QEvent::TouchUpdate:
        qDebug("UPDATE");
        break;
    case QEvent::TouchEnd:
        qDebug("END");
        break;
    case QEvent::MouseMove:
        qDebug("MOUSE");
        break;
   };
   return false;
}

我发现无论是触摸屏输入还是鼠标输入,我总是会收到 MouseMove 事件。

我决定我的硬件设置必须使我的触摸屏像鼠标而不是触摸屏(如果可能的话)。所以我打印了我的 /dev/inputs/ 来查看:

root@dm814x-evm:/dev/input# ls -l
drwxr-xr-x    2 root     root          180 Jan  1  2000 by-id
drwxr-xr-x    2 root     root          180 Jan  1  2000 by-path
crw-r-----    1 root     root      13,  64 Jan  1  2000 event0
crw-r-----    1 root     root      13,  65 Jan  1  2000 event1
crw-r-----    1 root     root      13,  66 Jan  1  2000 event2
crw-r-----    1 root     root      13,  67 Jan  1  2000 event3
crw-r-----    1 root     root      13,  68 Jan  1  2000 event4
crw-r-----    1 root     root      13,  69 Jan  1  2000 event5
crw-r-----    1 root     root      13,  70 Jan  1  2000 event6
crw-r-----    1 root     root      13,  71 Jan  1  2000 event7
lrwxrwxrwx    1 root     root           69 May  1 08:11 keyboard -> /dev/input/by-id/usb-Cypress_WinCE_USB_Keyboard___PS2_Mouse-event-kbd
lrwxrwxrwx    1 root     root            6 Jan  1  2000 keypad -> event2
crw-r-----    1 root     root      13,  63 Jan  1  2000 mice
crw-r-----    1 root     root      13,  32 Jan  1  2000 mouse0
crw-r-----    1 root     root      13,  33 Jan  1  2000 mouse1
crw-r-----    1 root     root      13,  34 Jan  1  2000 mouse2
crw-r-----    1 root     root      13,  35 Jan  1  2000 mouse3
lrwxrwxrwx    1 root     root           67 May  1 08:11 touchscreen -> /dev/input/by-id/usb-eGalax_Inc._USB_TouchController-event-joystick

root@dm814x-evm:/dev/input/by-id# ls -l
lrwxrwxrwx    1 root     root            9 Jan  1  2000 usb-Cypress_WinCE_USB_Keyboard___PS2_Mouse-event-kbd -> ../event0
lrwxrwxrwx    1 root     root            9 Jan  1  2000 usb-Cypress_WinCE_USB_Keyboard___PS2_Mouse-event-mouse -> ../event1
lrwxrwxrwx    1 root     root            9 Jan  1  2000 usb-Cypress_WinCE_USB_Keyboard___PS2_Mouse-mouse -> ../mouse0
lrwxrwxrwx    1 root     root            9 Jan  1  2000 usb-eGalax_Inc._USB_TouchController-event-joystick -> ../event3
lrwxrwxrwx    1 root     root            9 Jan  1  2000 usb-eGalax_Inc._USB_TouchController-event-mouse -> ../event6
lrwxrwxrwx    1 root     root            9 Jan  1  2000 usb-eGalax_Inc._USB_TouchController-joystick -> ../mouse1
lrwxrwxrwx    1 root     root            9 Jan  1  2000 usb-eGalax_Inc._USB_TouchController-mouse -> ../mouse3

在我看来,触摸屏充当鼠标,因此只会产生鼠标事件。

现在这可能超出了本论坛的范围?

4

2 回答 2

2
bool eventFilter(QObject * p_obj, QEvent * pevent)
{
  if (pevent->type() == QEvent::MouseButtonDblClick ||
   pevent->type() == QEvent::MouseButtonPress ||
   pevent->type() == QEvent::MouseButtonRelease ||
   pevent->type() == QEvent::Wheel)
  {
     QMouseEvent* pMouseEvent = dynamic_cast<QMouseEvent*>(pevent);
     if ((pMouseEvent != NULL) && (pMouseEvent->source() == Qt::MouseEventSource::MouseEventSynthesizedBySystem))
     {
         p_event->ignore();
         return true;
     }
  }
  return false;
 }

这肯定会解决你的问题。干杯干杯!

于 2015-02-20T08:08:09.710 回答
0
if( mouseEvent->source() == Qt::MouseEventNotSynthesized )
    ; // MouseButton was pressed
else if( mouseEvent->source() == Qt::MouseEventSynthesizedBySystem )
    ; // touch-event

请注意,它们也是Qt::MouseEventSynthesizedByQtQt::MouseEventSynthesizedByApplication。就我而言,它适用于上述两个。

于 2020-10-28T14:35:56.177 回答