我有无边框形式的按钮,如question
问题是我无法点击按钮。仅当我执行双击而不改变按钮上的位置时,它才有效。如何检查用户我执行点击(而非拖动)?
这里是代码:
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();
}
}
答案 0 :(得分: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();
}
答案 1 :(得分: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();
}