The core code looks like this, but the returned click coordinates is incorrect
....
// mouse press event
void mousePressEvent(QMouseEvent *e)
{
w_pos = e->globalPos(); // get current position
qDebug("Click pos - X: %d ,Y: %d", e->x(), e->pos().ry());
qDebug("Click globel pos - X: %d ,Y: %d", e->globalPos().rx(), e->globalPos().ry());
qDebug("Widget size - X: %d ,Y: %d", this->size().width(), this->height());
qDebug("w_pos value - X: %d, Y: %d", w_pos.rx(), w_pos.ry());
// change mouse cursor
if(this->hasFocus())
{
qDebug("Cal X-: %d, X+: %d, Y-: %d, Y+: %d",
this->width() -BLOCK_SIZE,
this->width(),
this->height()/2-BLOCK_SIZE,
this->height()/2+BLOCK_SIZE);
qDebug("Widget frame pos X: %d, Y: %d", this->frameGeometry().x(),this->frameGeometry().y());
qDebug("Widget pos X: %d, Y: %d", this->mapToGlobal(this->pos()).x(),this->pos().y());
qDebug("Widget Geometry X: %d, Y: %d", this->geometry().right(), this->geometry().left());
if(e->pos().rx() >= this->width() -BLOCK_SIZE &&
e->pos().rx() <= this->width() &&
e->pos().ry() >= this->height()/2-BLOCK_SIZE &&
e->pos().ry() <= this->height()/2+BLOCK_SIZE)
{
qDebug("click right block");
// change cursor
this->setCursor(Qt::SizeHorCursor);
block_pos = "right";
}
}
// redraw
this->update();
}
// mouse move event
void mouseMoveEvent(QMouseEvent *e)
{
if(block_pos == "right")
{
// left drag
// button
QRect rect = m_pb->geometry();
rect.setRight(rect.right() + (e->globalPos().rx() - w_pos.rx()));
w_pos = e->globalPos(); /
m_pb->setGeometry(rect);
// widget
QRect rectW = this->geometry();
rectW.setRight(rectW.right() + (e->globalPos().rx() - w_pos.rx())+BLOCK_SIZE);
this->setGeometry(rectW);
}
// redraw
this->update();
}
// mouse release event
void mouseReleaseEvent(QMouseEvent *e)
{
// switch mouse cursor
this->setCursor(Qt::ArrowCursor);
}
....