5

I want to implement a widget (with some edit boxes and sliders) that would open beneath or next to a button ("Opener") when I hover it. The key is it's a temporary widget - as soon as it loses focus, I want it gone. Also, I want it to pop up right next to the Opener, ideally pointing an arrow to Opener.

So, it's basically a tooltip. But it needs to be a widget with buttons and sliders and stuff like that. Is there a clever way to implement it without making a custom widget and writing handlers for all the mouse and focus events and recomputing its ideal position every time I open it or the Opener moves?

4

1 回答 1

3
class OpenerButton : public QPushButton
{
public:
    OpenerButton(QWidget * parent = 0);
protected:
    void enterEvent(QEvent *e);
    void leaveEvent(QEvent *e);
};

OpenerButton::OpenerButton(QWidget * parent)
      : QPushButton(parent)
{
   //Do necessary initializations For ex:set a menu for opener button 
}

void OpenerButton::leaveEvent(QEvent * e)
{
          //hide the popup_menu
}

void OpenerButton::enterEvent(QEvent * e)
{
    //Show the menu
    //You can use animation for ex:
     Popup_menu=new Popup_Dialog(this);//Popup_Dialog is a dialog containing all your widgets
     QPropertyAnimation *animation = new QPropertyAnimation(Popup_menu,"geometry");
     animation->setDuration(500);
     animation->setDirection(QAbstractAnimation::Forward);
     QRect startRect(Rect_Relative_to_Opener_Button);
     QRect endRect(Shifted_Rect_Relative_to_Opener_Button);
     animation->setStartValue(startRect);
     animation->setEndValue(endRect);
     animation->start();

}

Enterevent当鼠标光标进入小部件时调用。同样leaveevent,当鼠标光标离开小部件时。

于 2012-06-04T04:36:14.797 回答