有時需要點擊某個按鈕后,在該按鈕的下面或者右邊彈出菜單或者彈出框。那么就需要對框框的位置進行定位。
彈出菜單實例
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;
}