• Qt无边框设计


    1. //指定窗口为无边框
    2. this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinMaxButtonsHint);

    重写鼠标事件:

    1. void mousePressEvent(QMouseEvent* event) override;
    2. void mouseMoveEvent(QMouseEvent* event) override;

    定义位置:

    1. QPoint diff_pos;//鼠标和窗口的相对位移
    2. QPoint window_pos;//绝对位置
    3. QPoint mouse_pos;
    1. void QtWidgetsApplication2::mousePressEvent(QMouseEvent * event)
    2. {
    3. mouse_pos = event->globalPos();
    4. window_pos = this->pos();
    5. diff_pos = mouse_pos - window_pos;
    6. }
    7. void QtWidgetsApplication2::mouseMoveEvent(QMouseEvent * event)
    8. {
    9. QPoint pos = event->globalPos();
    10. this->move(pos - diff_pos);
    11. }

    再创建一个widget作为标题栏,并定义好控件:

    1. #include
    2. #include
    3. #include
    4. class CTitleBar : public QWidget
    5. {
    6. Q_OBJECT
    7. public:
    8. CTitleBar(QWidget *parent = nullptr);
    9. ~CTitleBar();
    10. private:
    11. void init();
    12. private:
    13. QLabel* logo;
    14. QLabel* title;
    15. QPushButton* set;
    16. QPushButton* min;
    17. QPushButton* max;
    18. QPushButton* close;
    19. };

    初始化控件:

    1. void CTitleBar::initUI()
    2. {
    3. logo = new QLabel(this);
    4. title = new QLabel(this);
    5. title->setText(u8"标题");
    6. title->setFixedWidth(120);
    7. set = new QPushButton(this);
    8. set->setFixedSize(32, 32);
    9. min = new QPushButton(this);
    10. min->setFixedSize(32, 32);
    11. max = new QPushButton(this);
    12. max->setFixedSize(32, 32);
    13. close = new QPushButton(this);
    14. close->setFixedSize(32, 32);
    15. QHBoxLayout* lay = new QHBoxLayout(this);
    16. lay->addWidget(logo);
    17. lay->addWidget(title);
    18. lay->addWidget(set);
    19. lay->addWidget(min);
    20. lay->addWidget(max);
    21. lay->addWidget(close);
    22. lay->setContentsMargins(5, 5, 5,5);
    23. }

    实现无边窗口的拉伸、改变大小:

    重写nativeEvent:

    1. #include
    2. bool nativeEvent(const QByteArray& eventType, void* message, long* result) override;
    3. bool QtWidgetsApplication2::nativeEvent(const QByteArray & eventType, void * message, long * result)
    4. {
    5. MSG* param = static_cast(message);
    6. switch (param->message)
    7. {
    8. case WM_NCHITTEST:
    9. {
    10. int nX = GET_X_LPARAM(param->lParam) - this->geometry().x();
    11. int nY = GET_Y_LPARAM(param->lParam) - this->geometry().y();
    12. /*if (childAt(nX, nY) != nullptr)
    13. return QWidget::nativeEvent(eventType, message, result);*/
    14. if (nX > m_nBorderWidth && nX < this->width() - m_nBorderWidth &&
    15. nY > m_nBorderWidth && nY < this->height() - m_nBorderWidth)
    16. {
    17. if (childAt(nX, nY) != nullptr)
    18. return QWidget::nativeEvent(eventType, message, result);
    19. }
    20. if ((nX > 0) && (nX < m_nBorderWidth))
    21. *result = HTLEFT;
    22. if ((nX > this->width() - m_nBorderWidth) && (nX < this->width()))
    23. *result = HTRIGHT;
    24. if ((nY > 0) && (nY < m_nBorderWidth))
    25. *result = HTTOP;
    26. if ((nY > this->height() - m_nBorderWidth) && (nY < this->height()))
    27. *result = HTBOTTOM;
    28. if ((nX > 0) && (nX < m_nBorderWidth) && (nY > 0)
    29. && (nY < m_nBorderWidth))
    30. *result = HTTOPLEFT;
    31. if ((nX > this->width() - m_nBorderWidth) && (nX < this->width())
    32. && (nY > 0) && (nY < m_nBorderWidth))
    33. *result = HTTOPRIGHT;
    34. if ((nX > 0) && (nX < m_nBorderWidth)
    35. && (nY > this->height() - m_nBorderWidth) && (nY < this->height()))
    36. *result = HTBOTTOMLEFT;
    37. if ((nX > this->width() - m_nBorderWidth) && (nX < this->width())
    38. && (nY > this->height() - m_nBorderWidth) && (nY < this->height()))
    39. *result = HTBOTTOMRIGHT;
    40. return true;
    41. }
    42. }
    43. return false;
    44. }

    启用鼠标悬停事件:

    setAttribute(Qt::WA_Hover)

  • 相关阅读:
    C中结构体和C++类各自对象的大小——C++
    Oracle使用exp和imp命令实现数据库导出导入
    [信息论]LZW编解码的实现(基于C++&Matlab实现)
    Spring底层
    1065 A+B and C (64bit)
    C++从淬体到元婴day01
    深入理解机器学习——EM算法/最大期望算法(Expectation-Maximization Algorithm, EM)
    【代码随想录Day54】图论Part06
    Sql中having和where的区别
    软件测试学习笔记九
  • 原文地址:https://blog.csdn.net/weixin_38241876/article/details/134544726