I have a small dialog like this:
When I move the dialog into another place on the desktop, how can I get the new global coordinate of an element in the dialog (for example, in this case the top left point of the Ok button)? Imagine I have a subclass MyButton for the OK button and I want to use QEvent for this class and I am working in this class, not in 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 );
}
This function is wrong because the relative position of the button with the dialog never changes, but I dont know how to correct it to get the new global coordinate of the button when moving the dialog. I need the new coordinates continously, not after I release the mouse.
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);