我有一个像这样的小对话框:
当我将对话框移动到桌面上的另一个位置时,如何获取对话框中元素的新全局坐标(例如,在本例中为 Ok 按钮的左上角)?想象一下,我有一个用于确定按钮的子类 MyButton,我想为这个类使用 QEvent,我正在这个类中工作,而不是在 QMainWindow 中工作。
bool MyButton::eventFilter( QObject *p_obj, QEvent *p_event )
{
if ( p_event->type() == QEvent::Move )
{
QPoint point = this->contentsRect().topLeft();
point = mapToGlobal( point );
qDebug() << point;
}
return QWidget::eventFilter( p_obj, p_event );
}
这个函数是错误的,因为按钮与对话框的相对位置永远不会改变,但我不知道如何纠正它以在移动对话框时获得按钮的新全局坐标。我需要不断的新坐标,而不是在我释放鼠标之后。
gridLayout = new QGridLayout(Form);
gridLayout->setObjectName(QStringLiteral("gridLayout"));
horizontalLayout = new QHBoxLayout();
horizontalLayout->setObjectName(QStringLiteral("horizontalLayout"));
lb_username = new QLabel(Form);
lb_username->setObjectName(QStringLiteral("lb_username"));
horizontalLayout->addWidget(lb_username);
le_username = new QLineEdit(Form);
le_username->setObjectName(QStringLiteral("le_username"));
horizontalLayout->addWidget(le_username);
gridLayout->addLayout(horizontalLayout, 0, 0, 1, 1);
horizontalLayout_2 = new QHBoxLayout();
horizontalLayout_2->setObjectName(QStringLiteral("horizontalLayout_2"));
lb_password = new QLabel(Form);
lb_password->setObjectName(QStringLiteral("lb_password"));
horizontalLayout_2->addWidget(lb_password);
le_password = new QLineEdit(Form);
le_password->setObjectName(QStringLiteral("le_password"));
horizontalLayout_2->addWidget(le_password);
gridLayout->addLayout(horizontalLayout_2, 1, 0, 1, 1);
horizontalLayout_3 = new QHBoxLayout();
horizontalLayout_3->setObjectName(QStringLiteral("horizontalLayout_3"));
horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
horizontalLayout_3->addItem(horizontalSpacer);
btn_ok = new MyButton();
btn_ok->setObjectName(QStringLiteral("btn_ok"));
horizontalLayout_3->addWidget(btn_ok);
btn_cancel = new MyButton();
btn_cancel->setObjectName(QStringLiteral("btn_cancel"));
horizontalLayout_3->addWidget(btn_cancel);
gridLayout->addLayout(horizontalLayout_3, 2, 0, 1, 1);
verticalSpacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
gridLayout->addItem(verticalSpacer, 3, 0, 1, 1);