zoukankan      html  css  js  c++  java
  • QT 继承QWidget && 继承QDialog

    工作项目中,利用到Qt对话框,场景需求:

    1. 一部分窗体需要继承自QWidget

    2. 一部分窗体需要继承自QDialog

    3. 两者均需要去掉标题栏图标,同时能够自由拖动。

    如果两者分开继承实现,会造成设置windowFlags、拖动功能部分代码重复冗余,为了达到代码复用的目的,利用模板类进行了封装。

    本地使用效果正常,特此备录。

    【1】头文件

     1 #ifndef _UPC_DIALOG_BASE_H
     2 #define _UPC_DIALOG_BASE_H
     3 
     4 #include <QDialog>
     5 #include <QWidget>
     6 #include <QPoint>
     7 #include <QMouseEvent>
     8 
     9 template <typename T>
    10 class UPCBaseWidget : public T
    11 {
    12 public:
    13     UPCBaseWidget(QWidget *parent = Q_NULLPTR);
    14     virtual ~UPCBaseWidget();
    15 
    16     virtual int initialze() = 0;
    17 
    18 protected:
    19     void mousePressEvent(QMouseEvent *event);
    20     void mouseMoveEvent(QMouseEvent *event);
    21     void mouseReleaseEvent(QMouseEvent *event);
    22 
    23 private:
    24     bool m_bMoving;         // 移动标志
    25     QPoint m_pointPosition; // 移动位置
    26 };
    27 
    28 /**
    29 * @brief: 窗体基类。主要用于统一窗体风格和操作
    30 * @author: Liuy
    31 */
    32 
    33 class UPCCommonWidget : public UPCBaseWidget<QWidget>
    34 {
    35     Q_OBJECT
    36 
    37 public:
    38     UPCCommonWidget(QWidget *parent = Q_NULLPTR);
    39     ~UPCCommonWidget();
    40 };
    41 
    42 
    43 /**
    44 * @brief: 对话框基类。主要用于统一对话框风格和操作
    45 * @author: Liuy
    46 */
    47 class UPCCommonDialog : public UPCBaseWidget<QDialog>
    48 {
    49     Q_OBJECT
    50 
    51 public:
    52     UPCCommonDialog(QWidget *parent = Q_NULLPTR);
    53     ~UPCCommonDialog();
    54 };
    55 
    56 #endif

    【2】实现文件

      1 #include "UPCDialogBase.h"
      2 #include "UPCSoftphoneConst.h"
      3 
      4 #include <QIcon>
      5 #include <QApplication>
      6 
      7 /*
      8 * @brief: 构造函数
      9 * @argument: QWidget * parent
     10 */
     11 template <typename T>
     12 UPCBaseWidget<T>::UPCBaseWidget(QWidget *parent)
     13     : T(parent)
     14     , m_bMoving(false)
     15 {
     16     // 设置系统图标
     17     setWindowIcon(QIcon(SYSTEM_ICON));
     18     // 设置关闭摧毁
     19     setAttribute(Qt::WA_DeleteOnClose);
     20     // 设置窗体背景为白色
     21     QPalette palette;
     22     palette.setColor(QPalette::Background, Qt::white);
     23     setPalette(palette);
     24 
     25     // 为了解决弹窗关闭后整个应用程序退出问题
     26     QApplication::setQuitOnLastWindowClosed(false);
     27 }
     28 
     29 template <typename T>
     30 UPCBaseWidget<T>::~UPCBaseWidget()
     31 {}
     32 
     33 /*
     34 * @brief: 重写鼠标按下事件处理过程
     35 * @argument: QMouseEvent * event
     36 * @return:void
     37 */
     38 template <typename T>
     39 void UPCBaseWidget<T>::mousePressEvent(QMouseEvent *event)
     40 {
     41     m_bMoving = true;
     42     // 记录下鼠标相对于窗口的位置
     43     // event->globalPos() 鼠标按下时,鼠标相对于整个屏幕位置
     44     // pos() 即:this->pos() 鼠标按下时,窗口相对于整个屏幕位置
     45     m_pointPosition = event->globalPos() - pos();
     46     return T::mousePressEvent(event);
     47 }
     48 
     49 /*
     50 * @brief: 重写鼠标移动事件处理过程
     51 * @argument: QMouseEvent * event
     52 * @return:void
     53 */
     54 template <typename T>
     55 void UPCBaseWidget<T>::mouseMoveEvent(QMouseEvent *event)
     56 {
     57     // (event->buttons() && Qt::LeftButton)按下是左键
     58     // 鼠标移动事件需要移动窗口,窗口移动到哪里呢?就是要获取鼠标移动中,窗口在整个屏幕的坐标,然后move到这个坐标,怎么获取坐标?
     59     // 通过事件event->globalPos()知道鼠标坐标,鼠标坐标减去鼠标相对于窗口位置,就是窗口在整个屏幕的坐标
     60     if (m_bMoving &&
     61         (event->buttons() && Qt::LeftButton) &&
     62         (event->globalPos() - m_pointPosition).manhattanLength() > QApplication::startDragDistance())
     63     {
     64         move(event->globalPos() - m_pointPosition);
     65         m_pointPosition = event->globalPos() - pos();
     66     }
     67 
     68     return T::mouseMoveEvent(event);
     69 }
     70 
     71 /*
     72 * @brief: 重写鼠标释放事件处理过程
     73 * @argument: QMouseEvent * event
     74 * @return:void
     75 */
     76 template <typename T>
     77 void UPCBaseWidget<T>::mouseReleaseEvent(QMouseEvent *event)
     78 {
     79     m_bMoving = false;
     80     return T::mouseReleaseEvent(event);
     81 }
     82 
     83 /*
     84 * @brief: 构造函数
     85 * @argument: QWidget * parent
     86 */
     87 UPCCommonWidget::UPCCommonWidget(QWidget *parent)
     88     : UPCBaseWidget<QWidget>(parent)
     89 {
     90     // 去掉任务栏图标 && 去掉标题栏 && 最上层显示
     91     setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint | Qt::WindowStaysOnTopHint);
     92 }
     93 
     94 UPCCommonWidget::~UPCCommonWidget()
     95 {}
     96 
     97 /*
     98 * @brief: 构造函数
     99 * @argument: QWidget * parent
    100 */
    101 UPCCommonDialog::UPCCommonDialog(QWidget *parent)
    102     : UPCBaseWidget<QDialog>(parent)
    103 {
    104     // 去掉标题栏 && 最上层显示
    105     setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint | Qt::WindowStaysOnTopHint);
    106 }
    107 
    108 UPCCommonDialog::~UPCCommonDialog()
    109 {}

    Good Good Study, Day Day Up.

    顺序 选择 循环 总结

  • 相关阅读:
    在bindingNavigator1中加入具有更好体验性的DateTimePicker
    static的初始化顺序 (转)
    C#数据结构求最大公约数和最小公倍数[辗转相除法]
    DataGridView控件显示行号
    C# 小票打印机 直接打印 无需驱动[转]
    Core Data 中遇到的一些问题
    字符指针不分配存储区,字符常量存储于静态数据区
    传送门
    Error Set
    实现类似iPhone通讯录新增名片,保存,之后可进行编辑操作的功能
  • 原文地址:https://www.cnblogs.com/Braveliu/p/9779679.html
Copyright © 2011-2022 走看看