Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 按下按钮期间无事件捕获[QT]_C++_Qt - Fatal编程技术网

C++ 按下按钮期间无事件捕获[QT]

C++ 按下按钮期间无事件捕获[QT],c++,qt,C++,Qt,我必须做一个虚拟键盘 因此,我创建了几个QPushButtons,但是如果我单击其中一个按钮,然后移动到另一个按钮上进行释放,那么另一个按钮不会收到任何鼠标事件 我需要覆盖enterEvent(QEvent*)和leaveEvent(QEvent*),但在单击其他按钮时未成功 有人有主意了吗?在QabStretchButton代码中: void QAbstractButton::mousePressEvent(QMouseEvent *e) { Q_D(QAbstractButton);

我必须做一个虚拟键盘

因此,我创建了几个
QPushButton
s,但是如果我单击其中一个按钮,然后移动到另一个按钮上进行释放,那么另一个按钮不会收到任何鼠标事件

我需要覆盖
enterEvent(QEvent*)
leaveEvent(QEvent*)
,但在单击其他按钮时未成功


有人有主意了吗?

在QabStretchButton代码中:

void QAbstractButton::mousePressEvent(QMouseEvent *e)
{
    Q_D(QAbstractButton);
    if (e->button() != Qt::LeftButton) {
        e->ignore();
        return;
    }
    if (hitButton(e->pos())) {
        setDown(true);
        d->pressed = true;
        repaint(); //flush paint event before invoking potentially expensive operation
        QApplication::flush();
        d->emitPressed();
        e->accept();
    } else {
        e->ignore();
    }
}
当mousePressEvent触发时,QAbstractButton调用
setDown(true)
,QAbstractButtonPrivate(
Q\u D(QAbstractButton)
)调用
D->emitPressed()。之后,其他按钮将不会接收任何事件,它们已在活动按钮中处理

您可以在
mouseReleaseEvent

void QAbstractButton::mouseReleaseEvent(QMouseEvent *e)
{
    Q_D(QAbstractButton);
    d->pressed = false;

    if (e->button() != Qt::LeftButton) {
        e->ignore();
        return;
    }

    if (!d->down) {
        // refresh is required by QMacStyle to resume the default button animation
        d->refresh();
        e->ignore();
        return;
    }

    if (hitButton(e->pos())) {
        d->repeatTimer.stop();
        d->click();
        e->accept();
    } else {
        setDown(false);
        e->ignore();
    }
}
它还检查状态
d->down
。如果鼠标点击该按钮,它将调用
d->click()发射单击事件。若鼠标未点击按钮,则调用
setDown(false)重置状态

我建议从QWidget继承虚拟按钮,以便更轻松地处理鼠标事件