0

I'm trying to trigger fake click events on QPushButtons.

ButtonsMap::ButtonsMap()
{

    m_b1 = new QPushButton("push me 1", this);
    m_b2 = new QPushButton("push me 2", this);
    m_b3 = new QPushButton("push me 3", this);
    m_b1->move(0,0);
    m_b2->move(0,40);
    m_b3->move(0,80);
    connect(m_b1, SIGNAL(clicked()), this, SLOT(setclicked1()));
    connect(m_b2, SIGNAL(clicked()), this, SLOT(setclicked2()));
    connect(m_b3, SIGNAL(clicked()), this, SLOT(setclicked3()));
}

And then I use this slot to triggers them using a custum TouchEvent class (not QTouchEvent, something from scratch)

void TouchToMouse::NewTouchEvent(const TouchEvent& e ) {
...

    QWidget *target = clickTarget(m_mw, pos);
    switch (e.m_type) {
        case APPEAR:
            event = new QMouseEvent(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
            break;
        case MOVE:
            event = new QMouseEvent(QEvent::MouseMove, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
            break;
        case DISAPPEAR:
            event = new QMouseEvent(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
            break;
    }

    QApplication::postEvent(target, event);*

}

My problem is: it works, but only for the first button m_b1, even though QWidget *target points to the right widget (I checked the adresses). It seems like m_b2 and m_b3 never do receive those events. Any idea on what might be going wrong?

4

1 回答 1

1

使用postEvent()发送的事件将被发送到事件队列并在事件循环下次运行时分派。是否有可能在第一个事件之后以某种方式停止事件循环?尝试调用QApplication::processEvents()以确保当前队列中的所有事件都得到处理。

于 2013-06-13T18:26:53.553 回答