Qt之MoveWindow类编写(可停靠桌面)

前言:

因为项目需求,现在要做一个窗口停靠功能,故集成于MoveWindow类中


功能需求:

  1. 自定义标签,自定义窗口按钮(关闭,缩小)
  2. 窗口停靠

代码:


enum Direction:quint16{
    Left = 0x1,
    Right = 0x2,
    Top = 0x4,
    Bottom =0x8,
    LeftTop = Left|Top,
    LeftBottom = Left|Bottom,
    RightTop = Right|Top,
    RightBottom = Right|Bottom,
    Disappear= 0x20,
     Error = 0x40,
    None = 0x80

};
Q_DECLARE_FLAGS(Directions,Direction)

 Q_DECLARE_OPERATORS_FOR_FLAGS(STOPEDGE::Directions)

}

Direction是方向

/**
 * @brief The MoveWindow class
 * @note 重写事件
 */
class MoveWindow :  public QWidget
{
    Q_OBJECT

public:
    explicit MoveWindow(QWidget *parent = nullptr);
    ~MoveWindow() {}

    /**
     * @brief setStopEdgeDirs
     * @brief 设置可停靠边
     * @param direction @type STOPEDGE::Directions
     * @example setStopEdgeDir(STOPEDGE::Direction::Top|STOPEDGE::Direction::Left);//设置桌面左边和上方停靠
     */
    void setStopEdgeDirs(STOPEDGE::Directions directions);
    /**
     * @brief setOverEdgeDirs
     * @brief 设置不可越界边
     * @param direction
     * @example setStopEdgeDirs(STOPEDGE::Direction::Bottom);//设置桌面下方不可越界
     */
    void setOverEdgeDirs(STOPEDGE::Directions directions);

    /**
     * @static
     *  @brief getStopEdgeDirs
     * @brief 获取停靠边
     * @param widget
     * @return STOPEDGE::Directions
     * @example <example>
     * using STOPEDGE::Directions;
     * Directions _directions = getStopEdgeDirs(this);
     * </example>
     * @author tangyoha
     */
    static STOPEDGE::Directions    getStopEdgeDirs(QWidget * widget);
    /**
     * @brief getOverEdgeDirs
     * @brief 获取越界边
     * @param widget
     * @param point @note 可选参数  @brief 为下一个窗口位置
     * @return
     * @author tangyoha
     * @example <example>
     * using STOPEDGE::Directions;
     * Directions _directions;
     * _directions = getOverEdgeDirs(this);
     * </example>
     */
    static STOPEDGE::Directions   getOverEdgeDirs(QWidget *widget,QPoint nextPos = QPoint(-1,-1));
    /**
     * @brief getScreensAt
     * @brief 获取widget所在屏幕
     * @return QScreen *
     * @example <example>
     * using STOPEDGE::Directions;
     * Directions _directions;
     * _directions = getOverScreensAt(this);
     */
    inline static auto getScreensAt(QWidget *) ->const QScreen*;
    bool isMouseInWidget(QWidget *widget,QPoint MousePos);
    bool getBStopEdge() const;
    void setBStopEdge(bool BStopEdge);

signals:
    void mainFormMoved(QPoint);//窗口移动了
    void overEdge(STOPEDGE::Direction);//窗口靠桌面边缘
    void rePosition(STOPEDGE::Directions);
public slots:
    void onStopEdgeChange(bool bStopEdgeChange);
    /**
     * @brief onMousePress
     * @brief 鼠标按下事件
     * @param event
     */
    void onMousePress(QMouseEvent * event);

    /**
     * @brief onMouseRelease
     * @brief 重写鼠标松开事件
     * @param event
     */
    void onMouseRelease(QMouseEvent *event);

    /**
     * @brief onMouseMove
     * @brief 重写鼠标移动事件
     * @param event
     */
    void  onMouseMove(QMouseEvent *event);


    void onEnter(QEvent *event);
    void onLeave(QEvent *event);
    void onActivationChange(QEvent *event);
    void onShow(QShowEvent *event);
private:
    bool isDragRect(QPoint point);//是否为可拖拽位置
    STOPEDGE::Directions m_OverEdge;
    STOPEDGE::Directions m_DStopEdge;

    //拖拽的坐标
    QPoint m_DragPosition;
    bool m_BStopEdge = false;//未停靠
protected:
    /**
     * @brief getDragWidget
     * @brief 由子类确定可拖拽的窗口位置
     * @return
     */

    /**
     * @brief getDragWidget
     * @return
     */
    virtual QWidget *getDragWidget() = 0;

    //是否可以移动
    bool bMoved;



protected slots:
    virtual void onWidgetStopEdge(STOPEDGE::Directions directions){Q_UNUSED(directions); }
    virtual void onThemeColorChange(QString colorStr){Q_UNUSED(colorStr)}
    virtual void onRePosition(STOPEDGE::Directions &directions){Q_UNUSED(directions);}
};

类的详情

未完待续。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值