有时需要点击某个按钮后,在该按钮的下面或者右边弹出菜单或者弹出框。那么就需要对框框的位置进行定位。
弹出菜单实例
QPushButton *btnMenu = new QPushButton(tr("Menu"), this);
btnMenu->setGeometry(QRect(50, 10, 60, 30));
connect(btnMenu, SIGNAL(clicked()), this, SLOT(menuClickedSlot()));

//menuClickedSlot()中代码
QPoint point;
point.setX(btnMenu->x());
point.setY(btnMenu->y() + btnMenu->height());
point = mapToGlobal(point);

QAction *action = menu->exec(point);
//
.
如果是弹出框QDialog,则可以用setGeometry(point.x(), point.y(), width, height)来确定位置,并show()和hide()操作。
延伸
当鼠标事件操作不在该QDialog上时,该QDialog消失。需要重写event事件:
bool CompoundingDialog::event(QEvent *e)


{
if (e->type() == QEvent::WindowDeactivate)
this->hide();
QDialog::event(e);

return true;
}