0

我有像这个问题一样的按钮的无边界表单

问题是我不能点击按钮。它仅在我执行双击而不更改按钮位置时才有效。如何检查我执行单击(而不是拖动)的用户?

代码来了:

bool LoginForm::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QMouseEvent::MouseButtonPress)
    {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
        if (obj == ui.loginButton)
        {
            QPoint absPos = ui.loginButton->mapToParent(QPoint(0, 0));
            m_dragPosition = (mouseEvent->pos() + absPos);
            return true;
        }
    }
    return false;
}

void LoginForm::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {   
        m_dragPosition = event->globalPos() - frameGeometry().topLeft();
        event->accept();
    }
}

void LoginForm::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() & Qt::LeftButton)
    {
        QPoint coord = event->globalPos() - m_dragPosition;
        move(coord);
        event->accept();
    }
}
4

2 回答 2

1

如何检查用户是否正在执行单击(而不是拖动)?

它应该更容易。不要从您的按钮中窃取事件。这种方法是从 Qt 示例中借用的:Music Player。有关详细信息,请参阅musicplayer.cpp

void LoginForm::mousePressEvent(QMouseEvent *event)
{
    m_dragPosition = event->globalPos() - pos();
    event->accept();
}


void LoginForm::mouseMoveEvent(QMouseEvent *event)
{
    move(event->globalPos() - m_dragPosition);
    event->accept();
}


void LoginForm::mouseReleaseEvent(QMouseEvent *event)
{
    m_dragPosition = QPoint();
    event->accept();
}
于 2016-02-27T16:47:11.610 回答
0

最简单的方法是禁用拖动按钮按钮

LoginForm::LoginForm(QWidget *parent)
    : QWidget(parent)
{
    ...
    m_isDragButton = false;
} 

LoginForm::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QMouseEvent::MouseButtonPress)
    {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
        if (obj == ui.loginButton)
            m_isDragButton = true;
    }
    return false;
}
void LoginForm::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {   
        m_dragPosition = event->globalPos() - frameGeometry().topLeft();
        event->accept();
    }
}

void LoginForm::mouseMoveEvent(QMouseEvent *event)
{
    if (!m_isDragButton)
        if (event->buttons() & Qt::LeftButton)
        {
            QPoint coord = event->globalPos() - m_dragPosition;
            move(coord);
            event->accept();
        }
}

void LoginForm::mouseReleaseEvent(QMouseEvent *event)
{
    m_isDragButton = false;
    m_dragPosition = QPoint();
    event->accept();
}
于 2016-02-27T18:28:03.777 回答